title: create service on windows
body: <p>I am having trouble starting my service on my pc. My code is based on this article <a href="http://www.gamedev.net/reference/articles/article1899.asp" rel="nofollow noreferrer">http://www.gamedev.net/reference/articles/article1899.asp</a></p>
  
  <p>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)</p>
  
  <pre><code>#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, &amp;status );
  }
  void WINAPI ServiceDispatch( DWORD numArgs, char **args )
  {
      // we have to initialize the service-specific stuff
      memset( &amp;status, 0, sizeof(SERVICE_STATUS) );
      status.dwServiceType = SERVICE_WIN32;
      status.dwCurrentState = SERVICE_START_PENDING;
      status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  
      hStatus = ::RegisterServiceCtrlHandler( srvName, &amp;ServiceCtrlHandler );
  
      // more initialization stuff here
      FILE *f = fopen("c:/testSrv.bin", "wb");
      ::SetServiceStatus( hStatus, &amp;status );
  }
  SERVICE_TABLE_ENTRY dispatchTable[] =
  {
      { srvName, &amp;ServiceDispatch },
      { NULL, NULL }
  };
  </code></pre>