• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1504
  • Last Modified:

CreateService and StartService


Hi,

Here are some of my code:

#define LOGGER_SERVICENAME "Logger"
#define LOGGER_FILE "C:\Program Files\Logger\Logger.exe"

Function X()
{

                szServiceName = LOGGER_SERVICENAME;            //Service name
      ptrServiceName = &szServiceName;

      szDisplayName = LOGGER_SERVICENAME;            //Service display name
      ptrServiceName = &szDisplayName;

      szServiceExe  = LOGGER_FILE;                          //Service executable      
      ptrServiceExe = &szServiceExe;

      ptrLOGroup      = NULL;        //The service does not belong to a group
      ptrTagID        = NULL;        //No tag requested
      ptrDependencies = NULL;        //No dependencies
      ptrSStartName   = NULL;        //Log service as 'local system' account
      ptrPassword     = NULL;        //Since 'local system' account, no password

               schService = CreateServiceA( schSCManager,                         // OpenSCManagerA()
                                    ptrServiceName,                 //
                                    ptrDisplayName,                 //
                                    SERVICE_ALL_ACCESS,             //
                                    SERVICE_WIN32_OWN_PROCESS,      //
                                    SERVICE_AUTO_START,             //
                                    SERVICE_ERROR_NORMAL,           //
                                    ptrServiceExe,                  //
                                    ptrLOGroup ,                    // NULL
                                    ptrTagID ,                      // NULL
                                    ptrDependencies,                // NULL
                                    ptrSStartName,                  // NULL
                                    ptrPassword );                  // NULL


      if( schService != NULL ) then  
            
            //START SERVICE    
            nReturn = StartServiceA( schService, NULL, NULL );  
         
            if( nReturn = 0 )then ;      
         
                  ISHandleServiceError(GetLastError());    
           
            endif;
      
      endif;

}


I always get schService equal to NULL and it terminates!! Does anyone see any error in my code?


Thanks


0
TungVan
Asked:
TungVan
  • 4
  • 3
1 Solution
 
jkrCommented:
Sinple error, big effect - change

#define LOGGER_FILE "C:\Program Files\Logger\Logger.exe"

to read

#define LOGGER_FILE "C:\\Program Files\\Logger\\Logger.exe"

The backslash is the escape character in C/C++ (i.e. in '\t' for 'TAB'), so if you want to use a backslash in a string literal, you need to double it like '\\'
0
 
TungVanAuthor Commented:

I changed it to #define LOGGER_FILE "C:\\Program Files\\Logger\\Logger.exe"

but schService is still equal to NULL
0
 
jkrCommented:
I just see that there's a lot more going wrong. See e.g. http://msdn.microsoft.com/library/en-us/dllproc/base/installing_a_service.asp ("Installing a Service"), that should be

               schService = CreateServiceA( schSCManager,                       // OpenSCManagerA()
                                    szServiceName,                //
                                    szDisplayName,                //
                                    SERVICE_ALL_ACCESS,            //
                                    SERVICE_WIN32_OWN_PROCESS,     //
                                    SERVICE_AUTO_START,            //
                                    SERVICE_ERROR_NORMAL,          //
                                    szServiceExe,                 //
                                    NULL,                   // NULL
                                    NULL,                     // NULL
                                    NULL,               // NULL
                                    NULL,                 // NULL
                                    NULL);                 // NULL

Passing the addresses of the strings will cause this function to fail. The strings themselves are what is needed.
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
TungVanAuthor Commented:

Thanks..

After changing it to string, I get schService != NULL

but when i run this code again....i get schService = NULL again.

Is it because the service already exists after i run it the first time?

0
 
jkrCommented:
>>Is it because the service already exists after i run it the first time?

Yes. Once a service is installed, you will have to 'DeleteService()' before running that again. BTW, 'GetLastError()' is quite useful if such a function fails.
0
 
TungVanAuthor Commented:

Thanks a lot. But my code is still not working (i will increase this question to 300pts since I asked too much questions)

This is my code for deleteservice:


    schService = OpenServiceA(
        schSCManager,             // SCManager database
        szServiceName,             // name of service
        DELETE);                           // only need DELETE access
 
    if (schService != NULL) then
     
              nReturn = DeleteService(schService);
              
              if (nReturn = 0) then  
                    
                    nError = GetLastError();
              
                  ISHandleServiceError(nError);  
                    
              endif;
   
    endif;


I always get nReturn = 0 after calling DeleteService
0
 
jkrCommented:
Is your service running at that moment? If so, you'll need to stop it first using something similat to the code provided in http://msdn.microsoft.com/library/en-us/dllproc/base/stopping_a_service.asp ("Stopping a Service")
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now