打开windows服务

#include <winsvc.h>

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); }

 

posted @ 2018-08-06 13:20  余生以学  阅读(170)  评论(0编辑  收藏  举报