void CXXXDlg::ServiceRun()
{
SERVICE_STATUS ssStatus;
//获得ServiceControl Manager的句柄
SC_HANDLE shServiceManager=OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
//然后获得用户希望检查的服务的句柄
SC_HANDLE shDefineService=OpenService(shServiceManager,"W3SVC",SERVICE_ALL_ACCESS);
if((QueryServiceStatus(shDefineService,&ssStatus))==0)
AfxMessageBox("无法确定服务状态",MB_ICONINFORMATION);
//下面的代码用于检查服务是否被停止,如果停止则打开此服务
SC_HANDLE stService;
if(ssStatus.dwCurrentState==SERVICE_STOPPED)
{
//AfxMessageBox("启动IIS",MB_ICONINFORMATION);
stService=OpenService(shServiceManager,"W3SVC",SERVICE_START|GENERIC_EXECUTE);
StartService(stService,0,NULL);
CloseServiceHandle(stService);
}
else
{
//AfxMessageBox("停止IIS",MB_ICONINFORMATION);
stService=OpenService(shServiceManager,"W3SVC",SERVICE_STOP|GENERIC_EXECUTE);
ControlService(stService,SERVICE_CONTROL_STOP,&ssStatus);
CloseServiceHandle(stService);
}
CloseServiceHandle(shDefineService);
CloseServiceHandle(shServiceManager);
}
{
SERVICE_STATUS ssStatus;
//获得ServiceControl Manager的句柄
SC_HANDLE shServiceManager=OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
//然后获得用户希望检查的服务的句柄
SC_HANDLE shDefineService=OpenService(shServiceManager,"W3SVC",SERVICE_ALL_ACCESS);
if((QueryServiceStatus(shDefineService,&ssStatus))==0)
AfxMessageBox("无法确定服务状态",MB_ICONINFORMATION);
//下面的代码用于检查服务是否被停止,如果停止则打开此服务
SC_HANDLE stService;
if(ssStatus.dwCurrentState==SERVICE_STOPPED)
{
//AfxMessageBox("启动IIS",MB_ICONINFORMATION);
stService=OpenService(shServiceManager,"W3SVC",SERVICE_START|GENERIC_EXECUTE);
StartService(stService,0,NULL);
CloseServiceHandle(stService);
}
else
{
//AfxMessageBox("停止IIS",MB_ICONINFORMATION);
stService=OpenService(shServiceManager,"W3SVC",SERVICE_STOP|GENERIC_EXECUTE);
ControlService(stService,SERVICE_CONTROL_STOP,&ssStatus);
CloseServiceHandle(stService);
}
CloseServiceHandle(shDefineService);
CloseServiceHandle(shServiceManager);
}