Qt笔记-获取Windows下目前运行的进程信息

目录

 

 

基本概念

代码与实例

源码下载


 

 

基本概念

知识点如下:

CreateToolhelp32Snapshot    获取当前系统进程快照

void Thread::getSnapshot()
{
    HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if(!hProcess){

        qDebug() << "CreateToolhelp32Snapshot failed!";
        return;
    }

    PROCESSENTRY32 info;
    info.dwSize = sizeof(PROCESSENTRY32);
    if(!Process32First(hProcess, &info)){

        qDebug() << "Process32First null";
        return;
    }

    bool haveNext = true;
    while(haveNext){

        haveNext = Process32Next(hProcess, &info);
        if(haveNext){

            HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, info.th32ProcessID);

            insertInfo(QString::number(info.th32ProcessID), QString::number(info.cntUsage),
                       QString::number(info.th32ParentProcessID), QString::number(info.pcPriClassBase),
                       QString::number((DWORD)hProcess), QString::fromWCharArray(info.szExeFile));

//            qDebug() << info.th32ProcessID << "  " << info.pcPriClassBase << "  " << QString::fromWCharArray(info.szExeFile) << "  " << hProcess;
        }
    }
//    qDebug() << "Over!";
}

通过此方式获取当前系统的进程信息

 

另外一个是关于QTableWidget相关设置:

    QStringList headList;
    headList << "进程ID" << "     进程名     " << "父类进程ID" << "进程句柄" << "优先级";
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(headList);
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setColumnWidth(1, 150);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);


    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setShowGrid(false);

设置表头数据,以及最后一列填充,以及不可编辑,以及设置每一项Stretch

 

 

代码与实例

程序运行截图如下:

上面那有一部分关键代码。

下面给出补充的关键代码:

#include "widget.h"
#include "ui_widget.h"
#include "thread.h"
#include <QDebug>
#include <QTableWidgetItem>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_thread = new Thread;

    QStringList headList;
    headList << "进程ID" << "     进程名     " << "父类进程ID" << "进程句柄" << "优先级";
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(headList);
//    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setColumnWidth(1, 150);
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);


    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setShowGrid(false);


    startTimer(1000);
}

Widget::~Widget()
{
    delete ui;
    delete m_thread;
}

void Widget::addNew()
{
    QVector<ProcessInfo> vec = m_thread->getVec();


    for(ProcessInfo info : vec){

        bool have = false;
        for(int i = 0; i < ui->tableWidget->rowCount(); i++){

            if(ui->tableWidget->item(i, 0)->text() == info.processID){

               have = true;
            }
        }
        if(!have){

            ui->tableWidget->insertRow(0);
            ui->tableWidget->setItem(0, 0, new QTableWidgetItem(info.processID));
            ui->tableWidget->setItem(0, 1, new QTableWidgetItem(info.processName));
            ui->tableWidget->setItem(0, 2, new QTableWidgetItem(info.parentProcessID));
            ui->tableWidget->setItem(0, 3, new QTableWidgetItem(info.processHandle));
            ui->tableWidget->setItem(0, 4, new QTableWidgetItem(info.processPri));
        }
    }
}

void Widget::removeOld()
{
    QVector<ProcessInfo> vec = m_thread->getVec();


    for(int i = 0; i < ui->tableWidget->rowCount(); i++){

        bool have = false;
        for(ProcessInfo info : vec){

            if(info.processID == ui->tableWidget->item(i, 0)->text()){

                have = true;
                continue;
            }
        }

        if(!have){

            ui->tableWidget->removeRow(i);
        }
    }
}

void Widget::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    m_thread->clearVec();
    m_thread->getSnapshot();
    addNew();
    removeOld();
}

 

 

 

源码下载

打包下载地址:https://github.com/fengfanchen/Qt/tree/master/ProcessList

 

 

已标记关键词 清除标记
package com.ylw.p2p.common.utils; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FileUtils { public final static Map<String, String> IMG_FILE_TYPE_MAP = new HashMap<String, String>(); /** * @Description: 图片文件上传 * @author Xiao.Sky * @creaetime 2015年4月17日下午5:20:27 * @param request * @param response * @param photo * @param strtmp * 文件名称 xxx.jpg * @param path * 文件路径 * @param num * @return */ public static boolean updatePhoto(HttpServletRequest request,HttpServletResponse response, File photo, String strtmp,String path, long num) { File dir = new File(path); // 如果不存在就创建次文件夹 if (!dir.exists()) { dir.mkdirs(); } File newFile = new File(dir, strtmp); // 如果存在此文件就删除此文件 if (newFile.exists()) newFile.delete(); BufferedInputStream bis = null; FileInputStream fis = null; try { fis = new FileInputStream(photo); FileOutputStream fos = new FileOutputStream(newFile); BufferedImage src = ImageIO.read(fis); ImageIO.write(src, "png", fos); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != bis) { bis.close(); } if (null != fis) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * * @Description: 普通文件上传 * @author Xiao.Sky * @creaetime 2015年4月23日下午3:33:53 * @param request * @param response * @param photo * @param strtmp * @param path * @param num * @return */ public static boolean updateFile(HttpServletRequest request,HttpServletResponse response, File photo, String strtmp,String path, long num) { File dir = new File(path); // 如果不存在就创建次文件夹 if (!dir.exists()) { dir.mkdirs(); } File newFile = new File(dir, strtmp); // 如果存在此文件就删除此文件 if (newFile.exists()) newFile.delete(); BufferedOutputStream bos = null; BufferedInputStream bis = null; FileInputStream fis = null; try { fis = new FileInputStream(photo); long s = fis.available(); // 检查文件上传的大小,文件不能大于2MB 2097152 if (s > num) { return false; } bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(newFile); bos = new BufferedOutputStream(fos); byte[] buf = new byte[4096]; int len = -1; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != bos) { bos.flush(); bos.close(); } if (null != bis) { bis.close(); } if (null != fis) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * Discription:[getAllFileType,常见文件头信息] */ static{ IMG_FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg) IMG_FILE_TYPE_MAP.put("jpeg", "FFD8FF"); IMG_FILE_TYPE_MAP.put("png", "89504E47");// PNG (png) IMG_FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp) } /** * * @Description:getImageFileType,获取图片文件实际类型,若不是图片则返回null * @author Tang.Homvee * @creaetime 2015年8月21日下午5:43:53 * @param f * @return */ public final static String getImageFileType(File f) { if (isImage(f)) { ImageInputStream iis = null; try { iis= ImageIO.createImageInputStream(f); Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { return null; } ImageReader reader = iter.next(); return reader.getFormatName(); } catch (IOException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; }finally{ if(iis != null){ try { iis.close(); } catch (IOException e) { } } } } return null; } public final static String getFileByFile(File file) { String filetype = null; byte[] b = new byte[50]; InputStream is = null; try { is = new FileInputStream(file); is.read(b); filetype = getFileTypeByStream(b); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { } } } return filetype; } public final static String getFileTypeByStream(byte[] b) { String filetypeHex = String.valueOf(getFileHexString(b)); Iterator<Entry<String, String>> entryiterator = IMG_FILE_TYPE_MAP .entrySet().iterator(); while (entryiterator.hasNext()
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页