C++ 检查Windows服务运行状态


检查Windows服务运行状态

 C++ Code 
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
 
#include  <iostream>
#include  <tchar.h>
#include  <Windows.h>

using   namespace  std;
/*  检查Windows服务状态信息
    使用API:
    OpenSCManager
    OpenService
    QueryServiceStatusEx
*/

int  main( void )
{
    TCHAR szSvcName[]       = _T(
"HistorySvr" );
    SC_HANDLE schSCManager  = 
NULL ;
    SC_HANDLE schService    = 
NULL ;

    SERVICE_STATUS_PROCESS ssStatus;
    DWORD dwOldCheckPoint   = 
0 ;
    DWORD dwStartTickCount  = 
0 ;
    DWORD dwWaitTime        = 
0 ;
    DWORD dwBytesNeeded     = 
0 ;

    
// 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 ;
        }
        cout << endl;
    }

    cin.get();
    
return   0 ;
}

  PS:请以管理员权限运行该程序,VS配置如下:

   

  参考:https://msdn.microsoft.com/en-us/library/bb540474(v=VS.85).aspx


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM