We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

CreateService and StartService

TungVan
TungVan asked
on
Medium Priority
1,532 Views
Last Modified: 2013-12-03

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


Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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 '\\'

Author

Commented:

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

but schService is still equal to NULL
CERTIFIED EXPERT
Top Expert 2012
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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?

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>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.

Author

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
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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")
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.