解决系统注册服务的状态查询、启动、停止及执行文件路径查询
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
// ServiceHlpr.h: #if !defined(AFX_SERVICEHLPR_H__58594DB7_DD67_44B1_AE76_EDB403437972__INCLUDED_) #define AFX_SERVICEHLPR_H__58594DB7_DD67_44B1_AE76_EDB403437972__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class ServiceHlpr { public: static DWORD QueryPath(LPCTSTR pszName,LPTSTR pszPathName,DWORD nSize=255); static DWORD Stop(LPCTSTR pszName); static DWORD Start(LPCTSTR pszName); static DWORD QueryStats(LPCTSTR pszName); ServiceHlpr(); virtual ~ServiceHlpr(); }; #endif // !defined(AFX_SERVICEHLPR_H__58594DB7_DD67_44B1_AE76_EDB403437972__INCLUDED_) // ServiceHlpr.cpp #include "stdafx.h" #include "ServiceHlpr.h" #include <winsvc.h> #pragma comment(lib,"advapi32.lib") #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// ServiceHlpr::ServiceHlpr() { } ServiceHlpr::~ServiceHlpr() { } /* 1:SERVICE_STOPPED The service is not running. 2:SERVICE_START_PENDING The service is starting. 3:SERVICE_STOP_PENDING The service is stopping. 4:SERVICE_RUNNING The service is running. 5:SERVICE_CONTINUE_PENDING The service continue is pending. 6:SERVICE_PAUSE_PENDING The service pause is pending. 7:SERVICE_PAUSED The service is paused. */ DWORD ServiceHlpr::QueryStats(LPCTSTR pszName) { SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE); if( schSCManager == NULL ) return 8; SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_QUERY_STATUS); if(schService == NULL) { ::CloseServiceHandle(schSCManager); return 9; } SERVICE_STATUS ssStatus; BOOL bRet = ::QueryServiceStatus(schService, &ssStatus); ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; return 10; } //-- Ret 4==RUNNING:OK --- DWORD ServiceHlpr::Start(LPCTSTR pszName) { if(QueryStats(pszName) == SERVICE_RUNNING) return 0; SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if( schSCManager == NULL ) return 8; SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_ALL_ACCESS); if( schService== NULL ) { ::CloseServiceHandle(schSCManager); return 9; } SERVICE_STATUS ssStatus; BOOL bRet = ::StartService(schService, 0, 0); if(bRet) { Sleep(1000); while( ::QueryServiceStatus(schService, &ssStatus)) { if(ssStatus.dwCurrentState == SERVICE_START_PENDING ) Sleep(1000); else break; } if(ssStatus.dwCurrentState != SERVICE_RUNNING) bRet = FALSE; } ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; return 10; } //-- Ret 1==SERVICE_STOPPED:OK --- DWORD ServiceHlpr::Stop(LPCTSTR pszName) { if(QueryStats(pszName) == SERVICE_STOPPED) return 0; SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if( schSCManager == NULL ) return 8; SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_ALL_ACCESS); if( schService== NULL ) { ::CloseServiceHandle(schSCManager); return 9; } SERVICE_STATUS ssStatus; BOOL bRet = ControlService(schService,SERVICE_CONTROL_STOP,&ssStatus); if(bRet) { if(ssStatus.dwCurrentState == SERVICE_STOP_PENDING) { Sleep(1000); while( ::QueryServiceStatus(schService, &ssStatus)) { if(ssStatus.dwCurrentState == SERVICE_START_PENDING ) Sleep(1000); else break; } } if(ssStatus.dwCurrentState != SERVICE_STOPPED) bRet = FALSE; } ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return ssStatus.dwCurrentState; return 10; } DWORD ServiceHlpr::QueryPath(LPCTSTR pszName,LPTSTR pszPathName,DWORD nSize) { SC_HANDLE schSCManager = ::OpenSCManager(NULL,NULL,SC_MANAGER_ENUMERATE_SERVICE); if( schSCManager == NULL ) return 8; SC_HANDLE schService = ::OpenService(schSCManager,pszName,SERVICE_QUERY_CONFIG); if(schService == NULL) { ::CloseServiceHandle(schSCManager); return 9; } LPQUERY_SERVICE_CONFIG lpqsc= (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR,4096); DWORD deByteNeed = 0; BOOL bRet = ::QueryServiceConfig(schService, lpqsc,4096,&deByteNeed); if(bRet) strcpy(pszPathName,lpqsc->lpBinaryPathName); LocalFree(lpqsc); ::CloseServiceHandle(schService); ::CloseServiceHandle(schSCManager); if(bRet) return 0; return 10; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=280