2 回答
TA贡献1801条经验 获得超8个赞
// NT服务程序的入口函数
void WINAPI ServiceMain(DWORD argc, LPSTR *argv)
{
//DWORD status;
//DWORD specificError;
// 注册服务控制
SKServer*NTSObj;
NTSObj = (SKServer *)SKServerID;
ServiceStatusHandle = RegisterServiceCtrlHandlerA(NTSObj->NTSName, ServiceCtrlHandler);
ServiceStatus.dwServiceType= SERVICE_WIN32_OWN_PROCESS;
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;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
if(NTSObj->OnStart!=NULL){
NTSObj->OnStart();
}
ServiceStatus.dwCurrentState= SERVICE_RUNNING;
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
NTSObj->IsExit = FALSE;
while(!NTSObj->IsExit){
Sleep(100);
}
return;
}
// NT服务程序的服务控制处理函数
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
SKServer*NTSObj;
NTSObj = (SKServer *)SKServerID;
switch(Opcode){
case SERVICE_CONTROL_PAUSE:
if(NTSObj->OnPause!=NULL){
NTSObj->OnPause();
}
ServiceStatus.dwCurrentState = SERVICE_PAUSED;
SetServiceStatus(ServiceStatusHandle,&ServiceStatus);
break;
case SERVICE_CONTROL_CONTINUE:
if(NTSObj->OnStart!=NULL){
NTSObj->OnStart();
}
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(ServiceStatusHandle,&ServiceStatus);
break;
case SERVICE_CONTROL_STOP:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
SetServiceStatus (ServiceStatusHandle,&ServiceStatus);
if(NTSObj->OnStop!=NULL){
NTSObj->OnStop();
}
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(ServiceStatusHandle,&ServiceStatus);
NTSObj->IsExit=TRUE;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
- 2 回答
- 0 关注
- 98 浏览
添加回答
举报