App to run as service and normal process

This is a discussion on App to run as service and normal process within the Windows Programming forums, part of the Platform Specific Boards category; I have a win32 app which i want to run as a service. I ha e implemented the servicemain/controlhandler, but ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    App to run as service and normal process

    I have a win32 app which i want to run as a service. I ha e implemented the servicemain/controlhandler, but the servicemain never triggers.. is there anything special i have to do?
    Winmain is triggering, and the app loads, but the service receives an error status after what i guess is a timeout period..

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    but the service receives an error status after what i guess is a timeout period..
    Can you provide more detail on the error status? If it's something along the lines of "The service is not responding to the control function" which is primarily caused by not calling the RegisterServiceCtrlHandler function fast enough in ServiceMain. It should be called immediately on entering ServiceMain.

    This is just a "shot in the dark" guess on my part without more detailed info.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    The servicemain function never triggers.
    I tried to add a messagebox in the beginning of servicemain, but it never displays.
    So i tried to call servicemain manually from winmain, and it then displays the messagebox:
    Code:
    void WINAPI ServiceMain(DWORD argc, TCHAR* argv[]){
    MessageBox(0, "ServiceMain", "testsvc", MB_OK);
    	// initialize service status
    I was wondering if the servicemain not triggering was because the app has a winmain..?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I was wondering if the servicemain not triggering was because the app has a winmain..?
    WinMain or main entrypoints are not relevant (either can be used) if your service does not have a user interface. Normally, services do not have a user interface.

    In your main entryproint, you should only have two items. One is a pointer to an array of type SERVICE_TABLE_ENTRY. The SERVICE_TABLE_ENTRY should have the service name as its first parm, the second parm is a function pointer to the named service's implementation. In this case it would be ServiceMain. The second and last entry in SERVICE_ENTRY_TYPE should be set to NULL, NULL for service name and function pointer to indicate the end of the table.

    The second item in the main entrypoint would be a call to StartServiceCtrlDispatcher;

    An example of what your main entrypoint should look like:

    Code:
    int main(void)
    {
    	SERVICE_TABLE_ENTRY MyServiceTable[]=
    	{
    		{"MyService",ServiceMain},
    		{NULL,NULL}
    	};
    	if(!StartServiceCtrlDispatcher(MyServiceTable))
    		handlerror();
    	else
    		printf("Service started successsfully\n");
    	return 0;
     }

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Heh! The service table entry was the missing part...
    Couldn't find that in any of the examples in msdn, or maybe i don't read it thoroughly enough?

    Anyway:
    Thanks alot!!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21