C ++让服务做点什么

时间:2015-10-20 15:19:13

标签: c++ service

我使用教程制作了一项服务。我怎样才能让它真正做到某些事情?这是代码:

#include <windows.h>

SERVICE_STATUS serviceStatus = { 0 };
SERVICE_STATUS_HANDLE statusHandle;
HANDLE serviceStopEvent = INVALID_HANDLE_VALUE;

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD);
DWORD WINAPI ServiceWorkerThread(LPVOID lParam);

#define SERVICE_NAME "Cool Service"

int main(int argc, char **argv)
{
    SERVICE_TABLE_ENTRY serviceTable[] = 
    {
        {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
        {NULL, NULL}
    };

    if (!StartServiceCtrlDispatcher(serviceTable))
        return GetLastError();

    return 0;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
    DWORD status = E_FAIL;

    statusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
    if (statusHandle == NULL)
        goto EXIT;

    ZeroMemory(&serviceStatus, sizeof(serviceStatus));
    serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    serviceStatus.dwControlsAccepted = 0;
    serviceStatus.dwCurrentState = SERVICE_START_PENDING;
    serviceStatus.dwWin32ExitCode = 0;
    serviceStatus.dwServiceSpecificExitCode = 0;
    serviceStatus.dwCheckPoint = 0;

    if (!SetServiceStatus(statusHandle, &serviceStatus))
        OutputDebugString("Cool Service: Service Main: SetServiceStatus returned error!");

    serviceStopEvent = CreateEvent(NULL, true, false, NULL);
    if (serviceStopEvent == NULL)
    {
        serviceStatus.dwControlsAccepted = 0;
        serviceStatus.dwCurrentState = SERVICE_STOPPED;
        serviceStatus.dwWin32ExitCode = GetLastError();
        serviceStatus.dwCheckPoint = 1;

        if (!SetServiceStatus(statusHandle, &serviceStatus))
            OutputDebugString("Cool Service: Service Main: SetServiceStatus returned error!");

        goto EXIT;
    }

    serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    serviceStatus.dwCurrentState = SERVICE_RUNNING;
    serviceStatus.dwWin32ExitCode = 0;
    serviceStatus.dwCheckPoint = 0;

    if (!SetServiceStatus(statusHandle, &serviceStatus))
        OutputDebugString("Cool Service: Service Main: SetServiceStatus returned error!");

    HANDLE thread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
    WaitForSingleObject(thread, INFINITE);

    CloseHandle(serviceStopEvent);

    serviceStatus.dwControlsAccepted = 0;
    serviceStatus.dwCurrentState = SERVICE_STOPPED;
    serviceStatus.dwWin32ExitCode = 0;
    serviceStatus.dwCheckPoint = 3;

    if (!SetServiceStatus(statusHandle, &serviceStatus))
        OutputDebugString("Cool Service: Service Main: SetServiceStatus returned error!");

EXIT:
    return;
}

void WINAPI ServiceCtrlHandler(DWORD ctrlCode)
{
    switch (ctrlCode)
    {
        case SERVICE_CONTROL_STOP:
        {
            if (serviceStatus.dwCurrentState != SERVICE_RUNNING)
                break;

            serviceStatus.dwControlsAccepted = 0;
            serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
            serviceStatus.dwWin32ExitCode = 0;
            serviceStatus.dwCheckPoint = 4;

            if (!SetServiceStatus(statusHandle, &serviceStatus))
                OutputDebugString("Cool Service: Service Handler: SetServiceStatus returned error!");

            SetEvent(serviceStopEvent);
            break;
        }

        default: break;
    }
}

void Function()
{
    Beep(7000, 1000);
}

DWORD WINAPI ServiceWorkerThread(LPVOID lParam)
{
    while (WaitForSingleObject(serviceStopEvent, 0) != WAIT_OBJECT_0)
    {
        Function();
        Sleep(3000);
    }

    return ERROR_SUCCESS;
}

我安装并在“services.msc”中启动它后服务什么也没做,它应该发出哔声,对吧?或者我的想法在这里。感谢您的帮助:)

0 个答案:

没有答案