1

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.

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.

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.

Courtesy: http://www.devx.com/cplus/Article/9857

Kindly help me in rectifying the issue.

Batch Script:

batscr.bat


ECHO Error Log Open >C:\MyServices\ERR.LOG
ECHO Error 1 >>C:\MyServices\ERR.LOG
ECHO Message 1 >>C:\MyServices\ERR.LOG

================================================

Standalone C program to execute the batch script

BatchExe.cpp


#include 
#include 

void main()
{
    system("C:\\MyServices\\batscr.bat");
}

=======================

Program for the service

StartScript.cpp



#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;
}

Thanks and Regards ...

vcosk
  • 2,894
  • 2
  • 23
  • 23
  • Windows Vista and up contain security features explicitly preventing this kind of behavior. Namely, all services run in session 0, which is no longer allowed to display to the screen. It seems you've got an overkill problem here -- why not use the Windows Task Scheduler to accomplish this? – Billy ONeal Mar 03 '10 at 21:10
  • I cannot use "Windows Task Scheduler" as I want to trigger the app start-up and stop remotely using sc command of windows. – vcosk Mar 03 '10 at 21:19

1 Answers1

1

this may help you a little bit Link here

... it's quite a common problem.

phatmanace
  • 4,671
  • 3
  • 24
  • 29
  • Thank you for the suggestion, I tried this first, but start up of the application has to happen through batch file and the java program cannot be invoked directly as some initial configurations have to be done :(. – vcosk Mar 03 '10 at 21:11