c_cpp 带有c的Windows服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 带有c的Windows服务相关的知识,希望对你有一定的参考价值。

#include <windows.h>
#include <stdio.h>

#define SLEEP_TIME 5000
#define LOGFILE "C:\\memstatus.txt"

int WriteToLog(char* str) {
    FILE* log;
    log = fopen(LOGFILE, "a+");
    if (log == NULL)
        return -1;
    fprintf(log, "%s\n", str);
    fclose(log);
    return 0;
}

SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE ServiceStatusHandle;

// Service initialization
int InitService() {
    int result;
    result = WriteToLog("Monitoring started.");
    return(result);
}

// Control handler function
void ServiceControlHandler(DWORD request) {
    switch (request) {
    case SERVICE_CONTROL_STOP:
    case SERVICE_CONTROL_SHUTDOWN:
        WriteToLog("Monitoring stopped.");
        ServiceStatus.dwWin32ExitCode = 0;
        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        break;
    default:
        break;
    }

    // Report current status
    SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

    return;
}

void ServiceMain(int argc, char** argv) {
    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;

    ServiceStatusHandle = RegisterServiceCtrlHandler(
        L"MemoryStatus",
        (LPHANDLER_FUNCTION)ServiceControlHandler
        );
    if (ServiceStatusHandle == NULL) {
        // Registering Control Handler failed
        return;
    }
    // Initialize Service 
    int error = InitService();
    if (error) {
        // Initialization failed
        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
        ServiceStatus.dwWin32ExitCode = -1;
        SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
        return;
    }
    // We report the running status to SCM. 
    ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

    MEMORYSTATUS memory;
    // The worker loop of a service
    while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
        char buffer[16];
        GlobalMemoryStatus(&memory);
        sprintf(buffer, "%d", memory.dwAvailPhys);
        int result = WriteToLog(buffer);
        if (result) {
            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
            ServiceStatus.dwWin32ExitCode = -1;
            SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
            return;
        }

        Sleep(SLEEP_TIME);
    }
    return;
}

int main(int argc, char** argv) {
    SERVICE_TABLE_ENTRY entry;
    entry.lpServiceName = L"MemoryStatus";
    entry.lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;

    // Start the control dispatcher thread for our service
    StartServiceCtrlDispatcher(&entry);
    return 0;
}

以上是关于c_cpp 带有c的Windows服务的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 带有内存分配的C中的3D矩阵

c_cpp 带有epoll的fifos

c_cpp 带有旋转逻辑的C语言记录器实现

c_cpp 带有重载运算符的模板

c_cpp 带有putc打印的小型trie框架

c_cpp C中的哈希映射实现,带有djb2哈希函数