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. hdu2199(高精度二分模版)

    Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and ...

  2. hdu26道动态规划总结

    前言:我们队的dp一直是我在做,说不上做的很顺,有些可以做,有些不能做.到现在为止,做dp题目也有七八十道了,除了背包问题的题目我可以说有百分之七八十的把握ac的话,其他类型的dp,还真没有多大把握. ...

  3. java资料——链表(转)

    链表 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个 ...

  4. Mac为nginx安装nginx-sticky-module

    Mac为nginx安装nginx-sticky-module nginx版本: nginx-1.9.8 nginx-sticky-module版本:nginx-sticky-module-ng *注意 ...

  5. IOS多线程之Block编程

    1 什么是block   iOS SDK 4.0開始,Apple引入了block这一特性.字面上说,block就是一个代码块.可是它的奇妙之处在于在内联(inline)运行的时候(这和C++非常像)还 ...

  6. golang第三方日志包seelog配置文件详解

    开发任何项目,都离不开日志,配好自己的项目日志输出,往往是开发项目的前提.在golang中,seelog应该是比较有名的日志处理包了,功能非常强大,seelog官方文档 一.seelog主要功能下面我 ...

  7. 防火墙导致MySQL无法访问的问题解决案例

    问题 数据库总是访问不了以致不得不反反复复的重启MySQL数据库. 但是重启的效果并不明显,重启10几分钟后问题依然出现.导致前端用户难以正常访问网站.另外他们还通过google,百度搜索,怀疑是数据 ...

  8. LIBSVM使用方法及参数设置 主要参考了一些博客以及自己使用经验。

    主要参考了一些博客以及自己使用经验.收集来觉得比较有用的. LIBSVM 数据格式需要---------------------- 决策属性  条件属性a  条件属性b  ... 2    1:7   ...

  9. 关于Struts2的action的execute方法

    这个方法必须要有一个String类型的返回值,所以如果写很多if else的话,记得要在最后加一个else,就是无论如何就会放回一个字符串,否则编译会报错,在execute方法名字下面有红线.

  10. Batch Normalization原理及其TensorFlow实现——为了减少深度神经网络中的internal covariate shift,论文中提出了Batch Normalization算法,首先是对”每一层“的输入做一个Batch Normalization 变换

    批标准化(Bactch Normalization,BN)是为了克服神经网络加深导致难以训练而诞生的,随着神经网络深度加深,训练起来就会越来越困难,收敛速度回很慢,常常会导致梯度弥散问题(Vanish ...