views:

631

answers:

3

I am having trouble starting my service on my pc. My code is based on this article http://www.gamedev.net/reference/articles/article1899.asp

When i call installService from my int main(int argc, char *argv[]), it is registered successfully (i can see it in msconfig and services.msc). However it has not started. I manually start the service via services.msv and i get the error "Error 2: system cannot find the file specified". Why is this? i registered the services no more then a min ago, my external HD is still on (where this is currently stored. i'll move a nondev version to c:/ when its ready) What am i doing wrong and is there another tutorial i can look at (i only found the one linked via google)

#define srvName "MyTestService_01312009"

void installService(char*path)
{
    SC_HANDLE handle = ::OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
    SC_HANDLE service = ::CreateService(
     handle,
     srvName,
     "MyTestService_01312009b",
     GENERIC_READ | GENERIC_EXECUTE,
     SERVICE_WIN32_OWN_PROCESS,
     SERVICE_AUTO_START,
     SERVICE_ERROR_IGNORE,
     path,
     NULL,
     NULL,
     NULL,
     NULL,
     NULL
    );
}
void uninstallService()
{
    SC_HANDLE handle = ::OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );//?
    SC_HANDLE service = ::OpenService( handle, srvName, DELETE );
    if( service != NULL )
    {
     // remove the service!
     ::DeleteService( service );
    }
}

SERVICE_STATUS_HANDLE hStatus;
SERVICE_STATUS status;

/*
if( ::StartServiceCtrlDispatcher( dispatchTable ) == 0 )
{
    // if this fails, it's probably because someone started us from
    // the command line.  Print a message telling them the "usage"
}
*/
void WINAPI ServiceCtrlHandler( DWORD control )
{
    switch( control )
    {
    case SERVICE_CONTROL_SHUTDOWN:
    case SERVICE_CONTROL_STOP:
        // do shutdown stuff here

        status.dwCurrentState = SERVICE_STOPPED;
        status.dwWin32ExitCode = 0;
        status.dwCheckPoint = 0;
        status.dwWaitHint = 0;
        break;
    case SERVICE_CONTROL_INTERROGATE:
        // just set the current state to whatever it is...
        break;
    }

    ::SetServiceStatus( hStatus, &status );
}
void WINAPI ServiceDispatch( DWORD numArgs, char **args )
{
    // we have to initialize the service-specific stuff
    memset( &status, 0, sizeof(SERVICE_STATUS) );
    status.dwServiceType = SERVICE_WIN32;
    status.dwCurrentState = SERVICE_START_PENDING;
    status.dwControlsAccepted = SERVICE_ACCEPT_STOP;

    hStatus = ::RegisterServiceCtrlHandler( srvName, &ServiceCtrlHandler );

    // more initialization stuff here
    FILE *f = fopen("c:/testSrv.bin", "wb");
    ::SetServiceStatus( hStatus, &status );
}
SERVICE_TABLE_ENTRY dispatchTable[] =
{
    { srvName, &ServiceDispatch },
    { NULL, NULL }
};
+1  A: 

Are you calling StartServiceCtrlDispatcher, RegisterServiceCtrlHandler, and SetServiceStatus, from the service when it is started?

dalle
i am not sure what you mean
acidzombie24
The code in your question only registers an executable with the specified as a service with the name `MyTestService_01312009b`. When this executable starts it must call the above functions in order to comply with the Win32 rules for services. So are you calling the above functions from the service?
dalle
Could you please post more code?
dalle
A: 

Maybe you can use Process Monitor to find out what's wrong.

Start it, and look for NAME NOT FOUND results that occur in connection with the service start.

Tomalak
+1  A: 

Is the path the full path to the exe as it cant be relative.

Lodle
yes i have the full path
acidzombie24