1、源代码
//服务器状态切换响应函数
void WINAPI ServiceHandler(DWORD fdwControl)
{
FILE* log = NULL;
switch (fdwControl)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
//add you quit code here
log = fopen("D:\\log.txt", "a+");
if (log)
{
fprintf(log, "ServiceHandler: %s\n", "service stop");
fclose(log);
}
break;
default:
return;
};
if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus))
{
DWORD nError = GetLastError();
}
}
//自定义工作线程回调函数,服务器启动前,调试附加进程->选择服务进程名称,并在此函数中加断点
void MyFunction()
{
FILE* fp = fopen("D:\\log.txt", "a+");
if (fp)
{
fputs("\n", fp);
fputs("work thread start!", fp);
fputs("\n", fp);
fclose(fp);
}
while(true)
{
Sleep(1);
}
}
//启动服务响应函数
void WINAPI service_main(int argc, char** argv)
{
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceHandler);
if (hServiceStatusHandle == 0)
{
DWORD nError = GetLastError();
}
std::cout << "my service start!" << std::endl;
//add your init code here
FILE* log = fopen("D:\\log.txt", "a+");
for (int i = 0; i < argc; ++i)
{
fprintf(log, "service_main: %s\n", argv[i]);
}
fclose(log);
//add your service thread here
std::thread wnd_thread(MyFunction);
wnd_thread.detach();
// Initialization complete - report running status
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 9000;
if (!SetServiceStatus(hServiceStatusHandle, &ServiceStatus))
{
DWORD nError = GetLastError();
}
}
int main(int argc, const char* argv[])
posted on 2022-04-09 01:10
小王 阅读(142)
评论(0) 编辑 收藏 引用 所属分类:
VC 、
Win32