Thread: What is the equivalent of main() for a Service?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    What is the equivalent of main() for a Service?

    I need to debug a service, but there's no main() function. So I'm wondering, what is the first function to get called when a Service starts? i.e. what's the Service equivalent of main()?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Thanks.
    Now, if I wanted to run this service as a regular program to make it easier to debug, could I just do this:
    Code:
    #ifdef DEBUGGING_SERVICE
       int main( int argc, char* argv[] )
    #else
       void ServiceMain( DWORD dwNumArgs, LPTSTR* lpServiceArgs )
    #endif  // DEBUGGING_SERVICE
    {
       ...
    }
    Right now I see some things like:
    Code:
    theApp.RegisterScmCtrlHandler(*lpServiceArgs, (LPHANDLER_FUNCTION)ControlHandler);
    in the ServiceMain() function. If I can rename it to main(), I'm assuming I'd have to do something else with the lines like that?

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by MSDN
    ServiceMain is a placeholder for an application-defined function name.
    It can actually be anything you want. It is defined when you install the service. A given executable can have multiple services with different ServiceMain function entry points. Look for an entry like this -

    Code:
        SERVICE_TABLE_ENTRY DispatchTable[] = 
        { 
            { SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain }, 
            { NULL, NULL } 
        };

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by abachler View Post
    It can actually be anything you want. It is defined when you install the service. A given executable can have multiple services with different ServiceMain function entry points. Look for an entry like this -

    Code:
        SERVICE_TABLE_ENTRY DispatchTable[] = 
        { 
            { SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain }, 
            { NULL, NULL } 
        };
    OK thanks, I found that, and the only function it lists is ServiceMain.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running my program as service
    By daher in forum Windows Programming
    Replies: 5
    Last Post: 09-05-2008, 12:30 PM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  4. Another windows service question... starting/stopping
    By BrianK in forum Windows Programming
    Replies: 1
    Last Post: 03-20-2003, 12:22 AM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM