CreateService and StartService

Posted on 2006-03-26
Last Modified: 2013-12-03


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?


Question by:TungVan
    LVL 86

    Expert Comment

    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 Comment


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

    but schService is still equal to NULL
    LVL 86

    Accepted Solution

    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.

    Author Comment



    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?

    LVL 86

    Expert Comment

    >>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 Comment


    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
    LVL 86

    Expert Comment

    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")

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now