mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QMap>
#include <QTimer> namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow(); void getAllAppPidList(QMap<QString, qint64> &app_pid);
void OpenServiceManager();
private slots:
void scanTable(); void on_pushButton_add_progress_clicked(); void on_pushButton_add_service_clicked(); private:
Ui::MainWindow *ui;
QTimer* scanTimer;
}; #endif // MAINWINDOW_H

mainwindow.cpp

#include <windows.h>// for OpenService
#include <tlhelp32.h>// for CreateToolhelp32Snapshot
#include <Psapi.h> // for GetModuleFileNameEx
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
/*
*
* =================================================================
* !!!!!!!!!!!!!本程序需要管理员身份运行!!!!!!!!!!!!!!!
* =================================================================
*
* */ //使用带bom的UTF8文件格式,在Tools-Options-Text Editor-Behavior-File Encoding-UTF-8 BOM:Add If Emcoding Is UTF-8
#pragma execution_character_set("utf-8") SC_HANDLE hSCM; MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); scanTimer = new QTimer(this);
connect( scanTimer, SIGNAL(timeout()), SLOT(scanTable()) );
scanTimer->start( ); // for 100fps OpenServiceManager();
} MainWindow::~MainWindow()
{
delete ui;
CloseServiceHandle(hSCM);
} void MainWindow::scanTable()
{
//进程
for(int row=; row<ui->tableWidget_progress->rowCount(); row++)
for(int col=; col<ui->tableWidget_progress->columnCount();col++)
{
QTableWidgetItem* item = ui->tableWidget_progress->item(row,col);
if(item!=NULL)
{
QString app=item->text();
}
} //检测服务是否在运行
if(hSCM)
{
for(int row=; row<ui->tableWidget_service->rowCount(); row++)
for(int col=; col<ui->tableWidget_service->columnCount();col++)
{
QTableWidgetItem* item = ui->tableWidget_service->item(row,col);
if(item!=NULL)
{
QString serviceName=item->text();
SC_HANDLE hService = ::OpenService( hSCM, serviceName.toStdWString().data(), SERVICE_ALL_ACCESS );
if(hService)
{
SERVICE_STATUS ssStatus;
QueryServiceStatus(hService,&ssStatus);//查看该Service的状态
if(ssStatus.dwCurrentState==SERVICE_STOPPED)
{
::StartService( hService, , NULL );
ui->textEdit->append("StartService "+serviceName);
}
else if(ssStatus.dwCurrentState==SERVICE_RUNNING)
{
item->setBackground(Qt::green);
}
CloseServiceHandle(hService);
}
else
{
//ui->textEdit->append("OpenService Failed "+serviceName);
item->setBackground(Qt::red);
}
}
}
}
} void MainWindow::OpenServiceManager()
{
/*
*
*
* 以下服务相关操作需要管理员权限
*
*
*
* */
hSCM = ::OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access
if (hSCM) {
//hService = ::OpenService( hSCM, QString("STEPVR_MMAP_SERVICE").toStdWString().data(), SERVICE_ALL_ACCESS );
//if(hService==NULL)
// ui->textEdit->append("OpenService Failed");
}
else
{
qDebug()<<"OpenSCManager Fail"<<GetLastError();
ui->textEdit->append("OpenSCManager Failed");
}
} // 根据进程号获取exe所在文件绝对路径
/*QString GetPathByProcessID(DWORD pid)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (!hProcess)
{
//QMessageBox::warning(NULL,"GetPathByProcessID","无权访问该进程");
return "";
}
WCHAR filePath[MAX_PATH];
DWORD ret= GetModuleFileNameEx(hProcess, NULL, filePath, MAX_PATH) ;
QString file = QString::fromStdWString( filePath );
//QMessageBox::warning(NULL,"GetPathByProcessID ret=", QString::number(ret)+":"+file);
CloseHandle(hProcess);
return ret==0?"":file;
} // 获取机器上正在运行的全部exe
void MainWindow::getAllAppPidList(QMap<QString, qint64> &app_pid)
{
app_pid.clear();
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
//warningLabel->setText("CreateToolhelp32Snapshot调用失败");
return ;
}
BOOL bMore = Process32First(hProcessSnap,&pe32);
while(bMore)
{
//printf("进程名称:%s\n",pe32.szExeFile);
//printf("进程ID:%u\n\n",pe32.th32ProcessID); QString exeName = (QString::fromUtf16(reinterpret_cast<const unsigned short *>(pe32.szExeFile)));
QString exePath = GetPathByProcessID( pe32.th32ProcessID );
exePath = FORMAT_PATH( exePath );
//qDebug()<<exePath.toLower();
if( exePath.isEmpty() )
{
//warningLabel->setText("获取进程 " + exeName + " 路径失败");
}
else
{
app_pid[exePath] = pe32.th32ProcessID;
} bMore = Process32Next(hProcessSnap,&pe32);
}
CloseHandle(hProcessSnap);
}*/ void MainWindow::on_pushButton_add_progress_clicked()
{
ui->tableWidget_progress->insertRow(ui->tableWidget_progress->rowCount());
} void MainWindow::on_pushButton_add_service_clicked()
{
ui->tableWidget_service->insertRow(ui->tableWidget_service->rowCount());
}

Qt监控后台服务运行状态的更多相关文章

  1. 使用 pm2-web 监控 pm2 服务运行状态

    pm2-web 是一款 pm2 服务状态监控程序,基于 web . 安装 $ npm install -g pm2-web 运行(默认是在8080端口) $ pm2-web 配置 pm2-web 将会 ...

  2. angular访问后台服务及监控会话超时的封装

    angular访问后台服务及监控会话超时的封装 angular本身自带访问组件http和httpclient,组件本身都是异步模式访问.本文只列举了对http组件的封装同时也一同处理会话超时监控. 获 ...

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

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

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

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

  5. Nagios监控nginx服务具体过程

    1在nginx 服务器上安装nrpe客户端: Nginx的服务须要监控起来.不然万一down了而不及时修复,会影响web应用.例如以下web应用上面启动的nginx后台进程[root@lb-net-2 ...

  6. Grafana部署监控docker服务

    Grafana部署监控docker服务 一.使用InfluxDB+cAdvisor+Grafana配置Docker监控 1.1Docker监控组件 1.2cAdvisor: 1.3Docker监控安装 ...

  7. iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法

    问题描述: iPhone越狱了,之后在Cydia中安装Anywhere虚拟定位,但是打开app提示:后台服务未启动,请重新安装应用后使用. 程序无法正常使用... 解决方法: 打开Cydia-已安装, ...

  8. 带后台服务配置的tomcat使用

    tomcat服务启动,将不需要手动启动startup.bat,避免cmd窗口的出现,因为隐藏到后台服务执行: 1,下载. 官网:http://tomcat.apache.org/download-70 ...

  9. Android 三级联动选择城市+后台服务加载数据库

    技术渣,大家将就着看 首先我们需要一个xml数据保存到数据库,这里我从QQ下面找到一个loclist.xml文件 <CountryRegion Name="中国" Code= ...

随机推荐

  1. .NET项目web自动化测试实战——Selenium 2.0

    PS:这次用公司的项目来练手,希望公司不会起诉我,因为我绝对是抱着学习的态度,没有任何恶意.仅供交流学习. 该项目是基于SharePoint平台所开发的门户网站,为了切身感受一下Selenium 2. ...

  2. 一款纯css实现的漂亮导航

    今天给大家分享一款纯css实现的漂亮导航.之前为大家分享过jquery实现的个人中心导航菜单,今天这款也是适合放在个人中心.还带来图标,效果不错.一起看下效果图: 在线预览   源码下载 实现的代码. ...

  3. 【转】【C#】判断当前操作系统

    写一个判断操作系统的工具类OSHelper.cs public class OSHelper { // 获取操作系统ID public static System.PlatformID GetPlat ...

  4. C语言 · 前10名

    算法提高 前10名   时间限制:1.0s   内存限制:256.0MB      问题描述 数据很多,但我们经常只取前几名,比如奥运只取前3名.现在我们有n个数据,请按从大到小的顺序,输出前10个名 ...

  5. Java 加载配置文件的方式

    一 使用原生方式读取配置文件 1 文件系统加载 Java代码   InputStream in = new FileInputStream("config.properties") ...

  6. Spring Boot 更换 Banner

    Spring 启动时,会有一个Banner图案,这个图案是可以更换的 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ ...

  7. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  8. zipkin使用mysql保存数据

    zipkin和mysql结合保存zipkin在项目监控中得到的所有数据. <dependency> <groupId>io.zipkin.java</groupId> ...

  9. -27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found

    LoadRunner请求无法找到:在录制Web协议脚本回放脚本的过程中,会出现请求无法找到的现象,而导致脚本运行停止. 错误现象:Action.c(41): Error -27979: Request ...

  10. ubuntu之Matlab安装

    (清华大学校内适用) 1.首先下载Matlab镜像:http://its.tsinghua.edu.cn/column/jsrj/1,一共有两个ios文件. 2.然后执行 cd ~ mkdir mat ...