title: Running batch script as windows service
body: <p>We have a java application which run as server running on a remote windows system which is started though a batch script which includes some initialization configurations. </p>
  
  <p>To avoid login into the system every time and starting / stopping the service I planned to add that batch script as a "Windows Service" and use it remotely through command prompt. After number of failed attempts I came to know there is no simple way of doing it without using third party software which I am not allowed to to use due software usage restrictions.</p>
  
  <p>As a solution I have written a C / C++ program which can be added as a service and used. The program works file. Now I am trying to run a batch script [using system() method ] using this code but the batch script is not getting executed. Where as it works fine in stand alone mode.</p>
  
  <p>Courtesy: <a href="http://www.devx.com/cplus/Article/9857" rel="nofollow noreferrer">http://www.devx.com/cplus/Article/9857</a> </p>
  
  <p>Kindly help me in rectifying the issue.</p>
  
  <p>Batch Script:</p>
  
  <h1>batscr.bat</h1>
  
  <pre><code>
  ECHO Error Log Open >C:\MyServices\ERR.LOG
  ECHO Error 1 >>C:\MyServices\ERR.LOG
  ECHO Message 1 >>C:\MyServices\ERR.LOG
  </code></pre>
  
  <p>================================================</p>
  
  <p>Standalone C program to execute the batch script</p>
  
  <h1>BatchExe.cpp</h1>
  
  <pre><code>
  #include 
  #include 
  
  void main()
  {
      system("C:\\MyServices\\batscr.bat");
  }
  </code></pre>
  
  <p>=======================</p>
  
  <p>Program for the service</p>
  
  <h1>StartScript.cpp</h1>
  
  <pre><code>
  
  #include 
  #include 
  #include 
  
  #define SLEEP_TIME 5000
  #define LOGFILE "C:\\MyServices\\memstatus.txt"
  
  SERVICE_STATUS ServiceStatus;
  SERVICE_STATUS_HANDLE hStatus;
  
  void  ServiceMain(int argc, char** argv);
  void  ControlHandler(DWORD request);
  void InitService();
  void main()
  {
      SERVICE_TABLE_ENTRY ServiceTable[2];
      ServiceTable[0].lpServiceName = "StartScript";
      ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  
      ServiceTable[1].lpServiceName = NULL;
      ServiceTable[1].lpServiceProc = NULL;
      // Start the control dispatcher thread for our service
      StartServiceCtrlDispatcher(ServiceTable);
  }
  
  
  void ServiceMain(int argc, char** argv)
  {
      int error;
  
      ServiceStatus.dwServiceType        = SERVICE_WIN32;
      ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
      ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
      ServiceStatus.dwWin32ExitCode      = 0;
      ServiceStatus.dwServiceSpecificExitCode = 0;
      ServiceStatus.dwCheckPoint         = 0;
      ServiceStatus.dwWaitHint           = 0;
  
      hStatus = RegisterServiceCtrlHandler(
          "StartScript",
          (LPHANDLER_FUNCTION)ControlHandler);
      if (hStatus == (SERVICE_STATUS_HANDLE)0)
      {
          // Registering Control Handler failed
          return;
      }
      // Initialize Service
      InitService();
  
      // We report the running status to SCM.
      ServiceStatus.dwCurrentState = SERVICE_RUNNING;
      SetServiceStatus (hStatus, &ServiceStatus);
  
  
  
      return;
  }
  
  // Service initialization
  void InitService()
  {
      system("C:\\MyServices\\batscr.bat");
      return;
  }
  
  // Control handler function
  void ControlHandler(DWORD request)
  {
      switch(request)
      {
          case SERVICE_CONTROL_STOP:
              ServiceStatus.dwWin32ExitCode = 0;
              ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
              SetServiceStatus (hStatus, &ServiceStatus);
              return;
  
          case SERVICE_CONTROL_SHUTDOWN:
              ServiceStatus.dwWin32ExitCode = 0;
              ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
              SetServiceStatus (hStatus, &ServiceStatus);
              return;
  
          default:
              break;
      }
  
      // Report current status
      SetServiceStatus (hStatus,  &ServiceStatus);
  
      return;
  }
  
  </code></pre>
  
  <p>Thanks and Regards ...</p>