检查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   = ;
    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;
        }
        cout << endl;
    }

cin.get();
    ;
}

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

  

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

C++ 检查Windows服务运行状态的更多相关文章

  1. 基于SignalR实现B/S系统对windows服务运行状态的监测

    通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...

  2. SignalR实现B/S系统对windows服务运行状态的监测

    基于SignalR实现B/S系统对windows服务运行状态的监测 通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windo ...

  3. windows服务,安装、启动、停止,配置,一个批处理文件搞定

    相对而言,还是比较通用的吧,如果哪位仁兄有更好的实现方式,或者发现有不足之处,还请多多指教.  @echo off echo.------------------------------------- ...

  4. 第十三篇 一个安装、管理windows服务的桌面程序

    在网上看到一个修改程序入口的程序去把windows 服务修改成控制台的程序,然后利用控制台的程序把服务安装和管理,也想起自己原来也写了一个对windows 报务管理的程序,不过是winform的.   ...

  5. C#通过SC命令和静态公共类来操作Windows服务

    调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...

  6. c# 安装windows服务

    C# windows服务: 第一种 :通过cmd命令安装.卸载.启动和停止Windows Service(InstallUtil.exe) 步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行 ...

  7. 阿里云下 centos7下启动程序总是被killed ,看内存占用情况以检查哪些服务存在问题并调整参数作调优

    很久不搭理自己的网站了,几天突然发现启动程序总是被killed, 于是查看了系统日志 vi /var/log/messages 发现出现 kernel: Out of memory: Kill pro ...

  8. C# windows服务制作(包括安装及卸载)

    开篇语 因工作内容需要做一个windows服务,此前并没有相关经验,所以做了一个demo来跑跑这个梗(高手跳过,需要的来踩)- 效果如下:打开服务,可以找到我们新增的一个windows服务,这个dem ...

  9. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

随机推荐

  1. python将字典内容存入mysql

    1.背景      项目须要,用python实现了将字典内容存入本地的mysql数据库. 比方说有个字典dic={"a":"b","c":& ...

  2. 08-session详解

    如何获取session对象? 1,openSession 2,getCurrentSession 如果使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置: 如 ...

  3. python处理xls、xlsx格式excle

    一.windows下读取xls格式文件,所需模块xlrd.xlw 1.下载安装包 xlrd地址:https://pypi.org/project/xlrd/#files xlwt地址:https:// ...

  4. Pygame制作答题类游戏的实现

    代码地址如下:http://www.demodashi.com/demo/13495.html 概述 个人比较喜欢玩这些答题类的游戏,在这类的游戏中其实存在着一些冷知识在里面.练习pygame的过程中 ...

  5. Popup.js

    test.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> ...

  6. Vue Element Form表单时间验证控件使用

    如果直接使用Element做时间选择器,其规则(rules)不添加type:'date',会提示类型错误,处理这个需要规范值的类型为date. 时间格式化过滤器 import Vue from 'vu ...

  7. GNU--gprof使用总结

    Added macros ACE_USES_GPROF which enables users to use gprof in a multithreaded environment with ACE ...

  8. C++ opencv高速样例学习——读图显示

    1.关键函数 1. 读入图片 imread(图片或位置,显示格式)默觉得:IMREAD_COLOR 显示格式: IMREAD_UNCHANGED =-1    // 8bit, color or no ...

  9. Fiddler设置代理抓手机包

    启动Fiddler,打开菜单栏中的 Tools > Fiddler Options,打开“Fiddler Options”对话框. 在Fiddler Options”对话框切换到“Connect ...

  10. 测试-一个unity的编译bug,初始化器

    .net C#下测试: public class Class1 { public bool toggle1 = true; public bool toggle2; } 一个结构类Class1,对里面 ...