title: ChangeServiceConfig2 using GetProcAddress to support multiple version of windows
body: <p>I'm using ChangeServiceConfig2 API of windows to change the service description. Since the same api is not supported in windows 98, ME I have used LoadLibraray and GetProcAddress to prevent static linking of the API in the exe. Please refer the code for more details:</p>
  
  <pre><code>typedef BOOL  (*ChgSvcDesc) (SC_HANDLE hService, DWORD dwInfoLevel, LPVOID lpInfo);
  
  eBool ServiceConfigNT::Install(IN tServiceDesc * pServiceDesc){
  
  
      SC_HANDLE           hservice;
      SC_HANDLE           hservicemgr;
      SERVICE_DESCRIPTION desc;
      ULong             starttype;
      HMODULE           hmod;
      ChgSvcDesc        fpsvcdesc;
  
  // Opens the Service Control Manager
  hservicemgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  if(!hservicemgr){
      vPrepareError(_TEXT("Failed to open service control manager!!"));
      goto err;
  }
  
  // Set start method of service.
  starttype   = (pServiceDesc-&gt;uAutoStart  == TRUE)? SERVICE_AUTO_START : SERVICE_DEMAND_START;
  
  // Create the service
  hservice =  CreateService(hservicemgr, vServiceName, pServiceDesc-&gt;uDisplayName, SERVICE_ALL_ACCESS, 
                  pServiceDesc-&gt;uServiceType, starttype, SERVICE_ERROR_NORMAL,pServiceDesc-&gt;uExePath,
                  NULL, NULL, NULL, NULL, NULL);
  
  if(!hservice) {
      vPrepareError(_TEXT("Failed to create service.!!"));
      goto err;        
  }
  
  // Set the description string
  if(pServiceDesc-&gt;uServiceDescription &amp;&amp; *pServiceDesc-&gt;uServiceDescription) {     
      desc.lpDescription = pServiceDesc-&gt;uServiceDescription;
      // This cannot be executed since it is not supported in Win98 and ME OS
      //(Void)ChangeServiceConfig2 (hservice, SERVICE_CONFIG_DESCRIPTION, &amp;desc);
      hmod = LoadLibrary(_TEXT("Advapi32.dll"));
      if(hmod) {
            // _UNICODE macro is set, hence im using the "W" version of the api
            fpsvcdesc = (ChgSvcDesc)GetProcAddress(hmod, "ChangeServiceConfig2W");      
            if(fpsvcdesc)
                 // On execution of the below statement, I get the error handle is invalid
                 fpsvcdesc(hservice, SERVICE_CONFIG_DESCRIPTION, &amp;desc);
  }
  
  CloseServiceHandle(hservice);
  CloseServiceHandle(hservicemgr);    
  return TRUE;
  
  err:
      if(hservicemgr) 
         CloseServiceHandle(hservicemgr);
  return FALSE;
  }
  </code></pre>
  
  <p>I debugged the code many times to find out why I am getting handle is invalid error ? On calling the API directly the description of service is changing but using the function pointer it gives the error.</p>
  
  <p>I think that the API is writing something to the service handle of the SCM, but I have no clues as to why ?</p>
  
  <p>Can somebody help me with this ?</p>