zoukankan      html  css  js  c++  java
  • 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) ", 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) ", 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) ", 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

  • 相关阅读:
    Django测试开发-36- xadmin模板中缩略图django-stdimage
    Django测试开发-35- xadmin模板中添加上传文件和图片功能
    Django测试开发-34- xadmin模板中添加action插件
    Django测试开发-33- xadmin模板中添加小组件报错
    Django测试开发-32- xadmin模板使用自定义菜单项
    Django测试开发-31- xadmin模板中choices使用
    序列化模块
    一大群模块
    python基础之内置函数和匿名函数
    python基础之迭代器和生成器
  • 原文地址:https://www.cnblogs.com/MakeView660/p/8358419.html
Copyright © 2011-2022 走看看