美文网首页
开发windows service程序例子

开发windows service程序例子

作者: CodingCode | 来源:发表于2023-01-25 08:44 被阅读0次
    1. service程序
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <tchar.h>
    
    #define SERVICE_NAME "myservice"
    
    SERVICE_STATUS          g_ServiceStatus = {0};
    SERVICE_STATUS_HANDLE   g_StatusHandle = NULL;
    HANDLE                  g_ServiceStopEvent = INVALID_HANDLE_VALUE;
    
    void WINAPI ServiceMain(int argc, char** argv);
    void WINAPI ServiceHandler(DWORD CtrlCode);
    DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
    
    
    void outputlog(char * input)
    {
        FILE *f = fopen("c:\\myservice.log", "a+");
        fprintf(f, "%s\n", input);
        fclose(f);
    }
    
    DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
    {
        outputlog("entry of ServiceWorkerThread");
    
        int i = 0;
        while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
        {
            // do-your-jobs-here
            outputlog("Writing...#");
            Sleep(3000);
            i++;
        }
    
        outputlog("exit of ServiceWorkerThread");
        return NULL;
    }
    
    void WINAPI ServiceHandler(DWORD CtrlCode)
    {
        outputlog("Entry of ServiceHandler");
    
        switch (CtrlCode)
        {
        case SERVICE_CONTROL_STOP:
        case SERVICE_CONTROL_SHUTDOWN:
            outputlog("ServiceHandler: SERVICE_CONTROL_STOP Request");
            g_ServiceStatus.dwControlsAccepted = 0;
            g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
            g_ServiceStatus.dwWin32ExitCode = 0;
            g_ServiceStatus.dwCheckPoint = 4;
            if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
            {
                outputlog("SetServiceStatus STOP_PENDING failed");
                return;
            }
    
            // This will signal the worker thread to start shutting down
            SetEvent (g_ServiceStopEvent);
    
            break;
        default:
            break;
        }
    
        outputlog("exit of ServiceHandler");
    }
    
    
    void WINAPI ServiceMain(int argc, char** argv)
    {
        outputlog("entry of ServiceMain");
    
        g_StatusHandle = RegisterServiceCtrlHandler(_T(SERVICE_NAME), ServiceHandler);
        if (g_StatusHandle == NULL)
        {
            outputlog("RegisterServiceCtrlHandler failed");
            return;
        }
    
        ZeroMemory (&g_ServiceStatus, sizeof (g_ServiceStatus));
        g_ServiceStatus.dwServiceType = SERVICE_WIN32;
        g_ServiceStatus.dwControlsAccepted = 0;
        g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
        g_ServiceStatus.dwWin32ExitCode = 0;
        g_ServiceStatus.dwServiceSpecificExitCode = 0;
        g_ServiceStatus.dwCheckPoint = 0;
        g_ServiceStatus.dwWaitHint = 0;
        if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
        {
            outputlog("SetServiceStatus START_PENDING failed");
            return;
        }
    
        // Create stop event to wait on later.
        g_ServiceStopEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
        if (g_ServiceStopEvent == NULL)
        {
            outputlog(_T("CreateEvent failed"));
    
            g_ServiceStatus.dwControlsAccepted = 0;
            g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
            g_ServiceStatus.dwWin32ExitCode = GetLastError();
            g_ServiceStatus.dwCheckPoint = 1;
            SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
            return;
        }
    
        g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
        g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
        g_ServiceStatus.dwWin32ExitCode = 0;
        g_ServiceStatus.dwCheckPoint = 0;
        g_ServiceStatus.dwWaitHint = 9000;
        if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
        {
            outputlog("SetServiceStatus SERVICE_RUNNING failed");
            return;
        }
    
        // Start the thread that will perform the main task of the service
        HANDLE hThread = CreateThread(NULL, NULL, ServiceWorkerThread, NULL, NULL, NULL);
        if (hThread == NULL)
        {
            outputlog("CreateThread failed");
        }
        // Wait until our worker thread exits effectively signaling that the service needs to stop
        WaitForSingleObject (hThread, INFINITE);
    
        // clean up
        CloseHandle (g_ServiceStopEvent);
    
        g_ServiceStatus.dwControlsAccepted = 0;
        g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        g_ServiceStatus.dwWin32ExitCode = 0;
        g_ServiceStatus.dwCheckPoint = 3;
        if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
        {
            outputlog("SetServiceStatus STOPPED failed");
            return;
        }
    
        return;
    }
    
    int main(int argc, const char* argv[])
    {
        outputlog("entry of main");
    
        SERVICE_TABLE_ENTRY ServiceTable[] =
        {
            {SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
            {NULL, NULL}
        };
    
        if (StartServiceCtrlDispatcher (ServiceTable) == FALSE)
        {
           outputlog(_T("StartServiceCtrlDispatcher failed"));
           return GetLastError ();
        }
    
        outputlog("exit of main");
        return 0;
    }
    
    1. 编译,visual studio命令行
    cl.exe -Femyservice.exe myservice.c advapi32.lib
    
    1. 安装和启动service
    sc create myservice binpath=c:\path\to\myservice.exe start=auto
    
    sc start myservice
    

    相关文章

      网友评论

          本文标题:开发windows service程序例子

          本文链接:https://www.haomeiwen.com/subject/bibdhdtx.html