如何修复服务控制按钮对于自定义服务已禁用

时间:2011-11-15 07:59:38

标签: winapi windows-services

我有一项服务,我开始&它工作正常(我通过查看它发布的EventLog消息验证它正在运行)。出于某种原因,services.msc显示停止按钮灰显&我无法弄清楚原因。

static SERVICE_STATUS_HANDLE gServiceStatusHandle = NULL;

static DWORD WINAPI DaemonServiceHandler(DWORD control, DWORD eventType, LPVOID eventData, LPVOID context)
{
    HANDLE stopEvent = reinterpret_cast<HANDLE>(context);

    switch (control) {
    case SERVICE_CONTROL_STOP:
        ReportServiceStatus(SERVICE_STOP_PENDING, 100, 3000);
        // notify main loop to stop
        return NO_ERROR;
    case SERVICE_CONTROL_INTERROGATE:
        return NO_ERROR;
    }
    return ERROR_CALL_NOT_IMPLEMENTED;
}

void WINAPI DaemonMain(DWORD argc, LPWSTR *argv)
{
    gServiceStatusHandle = RegisterServiceCtrlHandlerEx(WIN_UTF16(gServiceName), DaemonServiceHandler, NULL);
    if (gServiceStatusHandle == NULL) {
        ReportService(SERVICE_STOPPED);
        return;
    }
    ReportService(SERVICE_RUNNING);
    // do main loop
    ReportService(SERVICE_STOPPED);
}

int tmain(int argc, tchar **argv)
{
    const SERVICE_TABLE_ENTRYW DispatchTable[] =
    {
        {(L"MyService", DaemonMain},
        {NULL, NULL}
    };
    if (!StartServiceCtrlDispatcherW(DispatchTable)) {
        return 1;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我的ReportServiceStatus调用中的错误。

当我要报告的当前状态不是SERVICE_START_PENDING时,我总是将SERVICE_STATUS_HANDLE的dwControlsAccepted设置为0,而不是启用SERVICE_ACCEPT_STOP。