Windows服务安装返回错误1053

时间:2013-12-05 10:34:28

标签: c++ winapi windows-services nsis

我创建了一个Http Server(C ++ Win 32控制台应用程序),我想运行它是服务。 这个服务器有主线程和监听器线程,它将监听传入的流量。主线程将永远阻塞。

我创建了一个NSIS安装程序,它使用SimpleSC插件来安装和运行服务器

SimpleSC::InstallService "HttpServer" "HttpServer" "16" "2" "$INSTDIR\Server.exe" "" "" ""

SimpleSC::StartService "HttpServer" "" 30

我能够安装服务,但它没有启动并返回1053错误。这是因为主线程块?请帮帮我。

1 个答案:

答案 0 :(得分:1)

问题必须出在您的服务代码中。在您对RegisterServiceCtrlHandler()的调用中声明的服务控制处理程序中,您需要处理多个请求类型并根据反馈返回给系统服务管理器。它让系统知道您的服务正常运行以及它的当前状态。

如果您没有回答所有请求类型(特别是SERVICE_CONTROL_INTERROGATE),或者您没有在限定时间内回答,系统将推断您的服务已经失败/停止。

这是我在我的代码中使用的控制处理程序的一个例子:

//Feedback() is a custom function to put log into the system events and / or OutputDebugString()
//mSrvStatus is a global SERVICE_STATUS

void WINAPI SrvControlHandler(DWORD Opcode) {
    DWORD state;

    switch (Opcode) {
        case SERVICE_CONTROL_PAUSE:
            Feedback(FEED_EVENTS|FEED_ODS, "Pausing %s", SRVNAME);
            bActive = false;
            state = SERVICE_PAUSED;
            break;
        case SERVICE_CONTROL_CONTINUE:
            //refresh our settings from registry before continuing
            GetRegistrySettings();
            Feedback(FEED_EVENTS|FEED_ODS, "Continuing %s with refresh=%d", SRVNAME, dwRefresh);
            bActive = true;
            state = SERVICE_RUNNING;
            break;
        case SERVICE_CONTROL_STOP:
        case SERVICE_CONTROL_SHUTDOWN:
            Feedback(FEED_EVENTS|FEED_ODS, "Stopping %s", SRVNAME);
            ReportSrvStatus(SERVICE_STOP_PENDING, NO_ERROR, 0); //ok, we begin to stop
            //The final ReportSrvStatus(SERVICE_STOPPED, NO_ERROR, 0);
            //is sent from the function that started the service
            //that is waiting forever on the hSrvStopEvt event
            bActive = false;                                    //we tell the thread to stop fetching
            SetEvent(hSrvStopEvt);                              //and we signal the final event
            return;
            break;
        case SERVICE_CONTROL_INTERROGATE:
            state = mSrvStatus.dwCurrentState;
            Feedback(FEED_ODS, "%s interrogated by SCM, returned %d", SRVNAME, state);
            break;
        default:
            Feedback(FEED_ODS, "other control resquest ?? - %d", Opcode);
            state = mSrvStatus.dwCurrentState;
    }
    ReportSrvStatus(state, NO_ERROR, 0);
}

/* Sets the current service status and reports it to the SCM.

 Parameters:
   dwCurrentState - The current state (see SERVICE_STATUS)
   dwWin32ExitCode - The system error code
   dwWaitHint - Estimated time for pending operation, in milliseconds
*/ 
void ReportSrvStatus( DWORD dwCurrentState,
                      DWORD dwWin32ExitCode,
                      DWORD dwWaitHint) {
    static DWORD dwCheckPoint = 1;

    mSrvStatus.dwCurrentState = dwCurrentState;
    mSrvStatus.dwWin32ExitCode = dwWin32ExitCode;
    mSrvStatus.dwWaitHint = dwWaitHint;

    if (dwCurrentState == SERVICE_START_PENDING)
        mSrvStatus.dwControlsAccepted = 0;
    else mSrvStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN;

    if ( (dwCurrentState == SERVICE_RUNNING) ||
           (dwCurrentState == SERVICE_STOPPED) )
        mSrvStatus.dwCheckPoint = 0;
    else mSrvStatus.dwCheckPoint = dwCheckPoint++;

    SetServiceStatus( hSrvStatus, &mSrvStatus );
}
相关问题