c++检测windows服务程序状态
2015-02-07 09:15
567 查看
代码如下:
TCHAR szSvcName[] = _TEXT("test"); SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_STATUS_PROCESS ssStatus; DWORD dwOldCheckPoint; DWORD dwStartTickCount; DWORD dwWaitTime; DWORD dwBytesNeeded; // Get a handle to the SCM database. schSCManager = OpenSCManager( NULL, // local computer NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) { printf("OpenSCManager failed (%d)\n", GetLastError()); } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS ); // full access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); } // Check the status in case the service is not stopped. if (!QueryServiceStatusEx( schService, // handle to service SC_STATUS_PROCESS_INFO, // information level (LPBYTE) &ssStatus, // address of structure sizeof(SERVICE_STATUS_PROCESS), // size of structure &dwBytesNeeded ) ) // size needed if buffer is too small { printf("QueryServiceStatusEx failed (%d)\n", GetLastError()); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } else { // Check if the service is already running. It would be possible // to stop the service here, but for simplicity this example just returns. printf("Service status: "); switch(ssStatus.dwCurrentState) { case SERVICE_STOPPED: case SERVICE_STOP_PENDING: printf("Stop"); break; case SERVICE_PAUSED: case SERVICE_PAUSE_PENDING: printf("Pause"); break; case SERVICE_CONTINUE_PENDING: case SERVICE_RUNNING: case SERVICE_START_PENDING: printf("Running"); break; } printf("\n"); }
文章转载自:http://www.dotblogs.com.tw/alonstar/archive/2011/06/30/c_plus_windows_service.aspx
相关文章推荐
- 关于c++检测内存泄露相关知识(windows程序调试)
- C++编写Windows服务程序
- 关于C++内存泄漏检测的相关知识(windows程序调试 )
- 关于c++检测内存泄露相关知识(windows程序调试)
- 关于c++检测内存泄露相关知识(windows程序调试)
- c++写windows服务程序
- C/C++创建windows服务程序
- C++编写Windows服务程序
- 用C/C++创建windows服务程序
- 启动和停止Windows服务程序需向SCM汇报状态
- C/C++ 创建windows系统服务程序
- Windows Service开发日志四(用程序设置服务的运行状态及启动方式)
- Windows下用C++编写服务程序
- C++编写及注册windows服务程序
- C++编写及注册windows服务程序
- 用C/C++创建windows服务程序
- C++编写Windows服务程序 用 C 语言编写 Windows 服务程序的五个步骤
- 关于c++检测内存泄露相关知识(windows程序调试)
- 关于C++检测内存泄露相关知识(Windows程序调试)
- Win2000/XP安装软件时提示“无法访问windows安装程序,服务中windows Installer状态为停止,不能启动”