Qt:Windows编程—Qt实现进程管理

前言

继续学window编程,学呀学呀 学无止境,学到Windows进程相关API了。利用相关的API使用Qt写界面实现了一个简单的进程管理。主要用到 进程的创建、进程的枚举、线程的枚举、DLL的枚举、进程提权等功能。相关API的介绍可以看 C/C++:Windows编程—创建进程、终止进程、枚举进程、枚举线程、枚举DLL

界面

先看看qt写的界面吧。
Qt:Windows编程—Qt实现进程管理_第1张图片

代码

创建进程

// 创建进程
void Widget::on_pushButton_6_clicked()
{

    QFileDialog *fileDialog = new QFileDialog(this);
    fileDialog->setWindowTitle(tr("打开可执行文件"));
    fileDialog->setDirectory(".");
    fileDialog->setNameFilter(tr("可执行文件(*.exe)"));
    fileDialog->setViewMode(QFileDialog::Detail);
    QStringList fileNames;
    if(fileDialog->exec())
    {
        QString fileName;
        fileNames =  fileDialog->selectedFiles();
    }
    QString exePath = fileNames[0];
    qDebug() << exePath;

    const char* path = exePath.toStdString().c_str();
    STARTUPINFOA startInfo = {0};
    startInfo.cb = sizeof(startInfo);
    PROCESS_INFORMATION processInfo = {0};
    // startInfo 和 processInfo必须初始化
    BOOL ret = CreateProcessA(path,NULL,NULL,NULL,false,NULL
                  ,NULL,NULL,&startInfo,&processInfo);
    if( ret )
    {
        qDebug() << "processId = " << processInfo.dwProcessId << ",threadId = "<< processInfo.dwThreadId ;
        CloseHandle( processInfo.hProcess );
        CloseHandle( processInfo.hThread );
        emit refreshProcTab();
    }else
    {
        qDebug() << "创建进程失败" ;
    }

}

结束进程

// 结束进程
void Widget::on_pushButton_clicked()
{

    int row = ui->processTab->currentRow();
    uint pid = getPid();

    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid); // 获取进程句柄
    if( hProc == NULL)
    {
        qDebug() << "OpenProcess error ";
        return;
    }
    BOOL ret = TerminateProcess(hProc,0); // 强制进程退出
    if(ret == FALSE)
    {
        qDebug() << "TerminateProcess error ";
        return ;
    }
    ui->processTab->removeRow(row);
    CloseHandle(hProc);
}

停止进程

// 停止进程,就是将进程中的所有线程挂起
void Widget::on_pushButton_2_clicked()
{
    uint pid = getPid();
    HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
    if( INVALID_HANDLE_VALUE == snapHandele)
    {
        qDebug() << "CreateToolhelp32Snapshot error" ;
        return;
    }
    THREADENTRY32 entry = {0};
    entry.dwSize = sizeof(entry);
    BOOL ret = Thread32First(snapHandele,&entry);
    while( ret )
    {
        if( entry.th32OwnerProcessID == pid)
        {

            HANDLE tHandle = OpenThread(THREAD_ALL_ACCESS,FALSE,entry.th32ThreadID);
            if( tHandle == NULL)
            {
                qDebug() << "OpenThread error,threadId = " << entry.th32ThreadID;
            }
            else
            {
                DWORD ret = SuspendThread(tHandle);
                if( ret == -1)
                {
                    qDebug() << "SuspendThread error";
                }else
                {
                    qDebug() << "SuspendThread success";
                }
                CloseHandle(tHandle);
            }
        }
        ret = Thread32Next(snapHandele,&entry);
    }
    CloseHandle(snapHandele);
}

恢复进程

// 恢复进程,就是将进程中的所有线程恢复
void Widget::on_pushButton_3_clicked()
{
    uint pid = getPid();
    HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
    if( INVALID_HANDLE_VALUE == snapHandele)
    {
        qDebug() << "CreateToolhelp32Snapshot error" ;
        return;
    }
    THREADENTRY32 entry = {0};
    entry.dwSize = sizeof(entry);
    BOOL ret = Thread32First(snapHandele,&entry);
    while( ret )
    {
        if( entry.th32OwnerProcessID == pid)
        {

            HANDLE tHandle = OpenThread(THREAD_ALL_ACCESS,FALSE,entry.th32ThreadID);
            if( tHandle == NULL)
            {
                qDebug() << "OpenThread error,threadId = " << entry.th32ThreadID;
            }
            else
            {
                DWORD ret = ResumeThread(tHandle);
                if( ret == -1)
                {
                    qDebug() << "SuspendThread error";
                }else
                {
                    qDebug() << "ResumeThread success";
                }
                CloseHandle(tHandle);
            }
        }
        ret = Thread32Next(snapHandele,&entry);
    }
}

查看DLL

// 查看进程的DLL
// 查看某些系统进程的DLL是不行的,调用CreateToolhelp32Snapshot直接失败
// 必须提权,提权操作在upRole函数中
void Widget::on_pushButton_4_clicked()
{
    // 清空表格
    int rowCount = ui->dllTab->rowCount();
    for( int i = 0; i < rowCount; i++ )
    {
        ui->dllTab->removeRow(0);
    }
    uint pid = getPid();


    HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE ,pid);
    if( INVALID_HANDLE_VALUE == snapHandele)
    {
        qDebug() << "CreateToolhelp32Snapshot error" ;
        return;
    }
    MODULEENTRY32 entry = {0};
    entry.dwSize = sizeof(entry);// 长度必须赋值
    BOOL ret = Module32First(snapHandele,&entry);
    int i = 0;
    while (ret) {
        QString dllFile = QString::fromWCharArray(entry.szModule);
        QString dllPath = QString::fromWCharArray(entry.szExePath);
        ui->dllTab->insertRow(i);
        ui->dllTab->setItem(i,0,new QTableWidgetItem(dllFile));
        ui->dllTab->setItem(i,1,new QTableWidgetItem(QString("%1").arg(dllPath)));
        i++;
        ret = Module32Next(snapHandele,&entry);
    }
    CloseHandle(snapHandele);
}

完整工程

工程代码在这里可以下载。或者这里下载最新代码。

你可能感兴趣的:(【Language_C++】,【Language_Qt,】,【Windows编程】,Qt,window,API,进程管理,进程枚举)