Qt监控后台服务运行状态
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监控后台服务运行状态的更多相关文章
- 使用 pm2-web 监控 pm2 服务运行状态
pm2-web 是一款 pm2 服务状态监控程序,基于 web . 安装 $ npm install -g pm2-web 运行(默认是在8080端口) $ pm2-web 配置 pm2-web 将会 ...
- angular访问后台服务及监控会话超时的封装
angular访问后台服务及监控会话超时的封装 angular本身自带访问组件http和httpclient,组件本身都是异步模式访问.本文只列举了对http组件的封装同时也一同处理会话超时监控. 获 ...
- 基于SignalR实现B/S系统对windows服务运行状态的监测
通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windows服务肯定是必不可少的,我们还需要利用B/S与windows服务进 ...
- SignalR实现B/S系统对windows服务运行状态的监测
基于SignalR实现B/S系统对windows服务运行状态的监测 通常来讲一个BS项目肯定不止单独的一个BS应用,可能涉及到很多后台服务来支持BS的运行,特别是针对耗时较长的某些任务来说,Windo ...
- Nagios监控nginx服务具体过程
1在nginx 服务器上安装nrpe客户端: Nginx的服务须要监控起来.不然万一down了而不及时修复,会影响web应用.例如以下web应用上面启动的nginx后台进程[root@lb-net-2 ...
- Grafana部署监控docker服务
Grafana部署监控docker服务 一.使用InfluxDB+cAdvisor+Grafana配置Docker监控 1.1Docker监控组件 1.2cAdvisor: 1.3Docker监控安装 ...
- iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法
问题描述: iPhone越狱了,之后在Cydia中安装Anywhere虚拟定位,但是打开app提示:后台服务未启动,请重新安装应用后使用. 程序无法正常使用... 解决方法: 打开Cydia-已安装, ...
- 带后台服务配置的tomcat使用
tomcat服务启动,将不需要手动启动startup.bat,避免cmd窗口的出现,因为隐藏到后台服务执行: 1,下载. 官网:http://tomcat.apache.org/download-70 ...
- Android 三级联动选择城市+后台服务加载数据库
技术渣,大家将就着看 首先我们需要一个xml数据保存到数据库,这里我从QQ下面找到一个loclist.xml文件 <CountryRegion Name="中国" Code= ...
随机推荐
- lambda续集——1
捕获列表,只用于局部非static变量,lambda可以直接使用局部static变量和它所在函数之外声明的名字. eg: #include<iostream> using namespac ...
- HTML中的布局方式:absolute、relative、fixed、static
在CSS中关于定位的内容是: position:relative | absolute | static | fixed static(静态) 没有特别的设定,遵循基本的定位规定,不能通过z- ...
- jsp c标签不遍历的方法
生产中遇到过不需要遍历的事情. 一般遍历必须要 <c:forEach items="${resultObj.xxx}" var="data" varSta ...
- java并发编程()阻塞方法与中断方法
看完这篇,我感觉我对java多线程又懵逼了. 线程可能会阻塞或暂停执行,原因有多种: 等待I/O操作结束 等待获得一个锁 等待从Thread.sleep方法中醒来 等待另一个线程计算的结果 当线程阻塞 ...
- MongoDB 集群搭建(主从复制、副本及)(五)
六:架构管理 mongodb的主从集群分为两种: 1:master-Slave 复制(主从) --从server不会主动变成主server,须要设置才行 2:replica Sets 复制(副本 ...
- 查看nginx cache命中率
一.在http header上增加命中显示 nginx提供了$upstream_cache_status这个变量来显示缓存的状态,我们可以在配置中添加一个http头来显示这一状态,达到类似squid的 ...
- 刘昕明:送给和我一样曾经浮躁过的PHP程序员
作者注:2012年偶决定开始写博客了,不为别的,就希望可以通过博客记录我的成长历程同时也希望可以帮助一些刚毕业,刚入行业的兄弟姐们们.我们是一群充满浮躁.抱怨.迷茫的程序猿,想一想3年就这么过去了,社 ...
- R语言设置write.table()输出的文件格式
write.table(),是保存数据为文件的函数. > xiaohuqingdan <- c(3900088702, 3900072499,3900021029) > xiaohu ...
- 查询相应id下的数据
---恢复内容开始--- u方法这样的:带不起模板引擎 <a href="{:U('Del/wzxg','','')}/{$vo.id}">修改</a> 这 ...
- linux cpio
在使用bootimg tools压缩和打包android的ramdisk时,用到了cpio命令.所以了解一下用法. 参考 http://man.linuxde.net/cpio http://cn.l ...