CreateService and StartService


Here are some of my code:

#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 ;      


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


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

but schService is still equal to NULL
I just see that there's a lot more going wrong. See e.g. ("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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

TungVanAuthor Commented:


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?

>>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.
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();

I always get nReturn = 0 after calling DeleteService
Is your service running at that moment? If so, you'll need to stop it first using something similat to the code provided in ("Stopping a Service")
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.