谈天说地

C++ 服务项入口函数

Windows中的服务项也有启动入口 程序main启动 增加结构体 CMD创建服务项 <注意事项: 每个="号后必须空格" 否则出错=""> sc create 服务项名 binpath= "C:\1.exe" start= auto DisplayName= "服务显示名称" sc description 服务项名 "服务描述" 更多参数可以看一下 cmd /c sc create

SERVICE_TABLE_ENTRY ServiceTable[2];
? ? ServiceTable[0].lpServiceName = "服务项名";
? ? ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain; //服务项入口函数
? ??
? ? ServiceTable[1].lpServiceName = NULL;
? ? ServiceTable[1].lpServiceProc = NULL;

StartServiceCtrlDispatcher(ServiceTable);

全局定义一下

SERVICE_STATUS ServiceStatus;?
SERVICE_STATUS_HANDLE hStatus;?


void ServiceMain(int argc, char** argv); ?//服务项启动入口函数
void ControlHandler(DWORD request);?
int InitService();?
int WriteToLog(char* str);
?
void ServiceMain(int argc, char** argv)?
{?
? ?int error;?
?
? ?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;?
?
? ?hStatus = RegisterServiceCtrlHandler(
? ? ? "MemoryStatus",?
? ? ? (LPHANDLER_FUNCTION)ControlHandler);?
? ?if (hStatus == (SERVICE_STATUS_HANDLE)0)?
? ?{?
? ? ? // Registering Control Handler failed
? ? ? return;?
? ?} ?
? ?// Initialize Service?
? ?error = InitService();?
? ?if (!error)?
? ?{
? ? ? // Initialization failed
? ? ? ServiceStatus.dwCurrentState =?
? ? ? ? ?SERVICE_STOPPED;?
? ? ? ServiceStatus.dwWin32ExitCode = -1;?
? ? ? SetServiceStatus(hStatus, &ServiceStatus);?
? ? ? return;?
? ?}?
? ?// We report the running status to SCM.?
? ?ServiceStatus.dwCurrentState =?
? ? ? SERVICE_RUNNING;?
? ?SetServiceStatus (hStatus, &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(hStatus,?
? ? ? ? ? ? ? ? ? ? ? ? ? &ServiceStatus);
? ? ? ? ?return;
? ? ? }
? ? ? Sleep(SLEEP_TIME);
? ?}
? ?return;?
}

void ControlHandler(DWORD request)?
{?
? ?switch(request)?
? ?{?
? ? ? case SERVICE_CONTROL_STOP:?
? ? ? ? ?WriteToLog("Monitoring stopped.");
? ? ? ? ?ServiceStatus.dwWin32ExitCode = 0;?
? ? ? ? ?ServiceStatus.dwCurrentState = SERVICE_STOPPED;?
? ? ? ? ?SetServiceStatus (hStatus, &ServiceStatus);
? ? ? ? ?return;?
?
? ? ? case SERVICE_CONTROL_SHUTDOWN:?
? ? ? ? ?WriteToLog("Monitoring stopped.");
? ? ? ? ?ServiceStatus.dwWin32ExitCode = 0;?
? ? ? ? ?ServiceStatus.dwCurrentState = SERVICE_STOPPED;?
? ? ? ? ?SetServiceStatus (hStatus, &ServiceStatus);
? ? ? ? ?return;?
? ? ? ??
? ? ? default:
? ? ? ? ?break;
? ? }?
?
? ? // Report current status
? ? SetServiceStatus (hStatus, &ServiceStatus);
?
? ? return;?
}

int WriteToLog(char* str)
{
? ? FILE* log;
? ? log = fopen(LOGFILE, "a+");
? ? if (log == NULL)
? ? return -1;
? ? fprintf(log, "%s ", str);
? ? fclose(log);
? ? return 0;
}
int InitService(){
? ? WriteToLog("Monitoring started.");
? ? return true;
}

Image

0 条评论

发表评论

你需要登录后才可进行发表