我们有一个Java应用程序,它作为服务器运行在远程Windows系统上,该系统通过包含某些初始化配置的批处理脚本启动。

为了避免每次都登录到系统并启动/停止服务,我计划将该批处理脚本添加为“Windows服务”,并通过命令提示符远程使用它。经过多次失败的尝试,我才知道没有使用第三方软件是没有简单的方法,我不能使用第三方软件来限制软件的使用。

作为解决方案,我编写了一个C / C++程序,可以将其添加为服务并使用。程序工作文件。现在,我尝试使用此代码运行批处理脚本[使用system()方法],但批处理脚本未得到执行。在独立模式下可以正常工作的地方。

礼貌:http://www.devx.com/cplus/Article/9857

请帮助我纠正问题。

批处理脚本:

bat


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

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

独立的C程序执行批处理脚本

BatchExe.cpp

#include
#include

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

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

服务程序

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


感谢致敬 ...

最佳答案

这可能会对您有所帮助
Link here

...这是一个普遍的问题。

08-04 18:39