打开windows服务

#include 

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,_T("Tomcat8"),SERVICE_ALL_ACCESS); if((QueryServiceStatus(shDefineService,&ssStatus))==0) { AfxMessageBox(_T("无法确定服务状态"),MB_ICONINFORMATION); } //下面的代码用于检查服务是否被停止,如果停止则打开此服务 SC_HANDLE stService; if(ssStatus.dwCurrentState==SERVICE_STOPPED) { //AfxMessageBox("启动Tomcat8服务",MB_ICONINFORMATION); stService = OpenService(shServiceManager,_T("Tomcat8"),SERVICE_START|GENERIC_EXECUTE); StartService(stService,0,NULL); CloseServiceHandle(stService); } //else //如果是运行状态就停止 //{ // //AfxMessageBox("停止Tomcat8服务",MB_ICONINFORMATION); // stService=OpenService(shServiceManager,"Tomcat8",SERVICE_STOP|GENERIC_EXECUTE); // ControlService(stService,SERVICE_CONTROL_STOP,&ssStatus); // CloseServiceHandle(stService); //} CloseServiceHandle(shDefineService); CloseServiceHandle(shServiceManager); }

 

你可能感兴趣的:(打开windows服务)