将批处理脚本作为Windows服务运行

时间:2010-03-03 21:03:52

标签: c++ windows-services batch-file

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

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

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

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

请帮助我纠正这个问题。

批处理脚本:

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

谢谢和问候......

1 个答案:

答案 0 :(得分:1)

这可能对你有所帮助 Link here

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