Qt:Windows编程—Qt实现进程管理
生活随笔
收集整理的這篇文章主要介紹了
Qt:Windows编程—Qt实现进程管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
繼續學window編程,學呀學呀 學無止境,學到Windows進程相關API了。利用相關的API使用Qt寫界面實現了一個簡單的進程管理。主要用到 進程的創建、進程的枚舉、線程的枚舉、DLL的枚舉、進程提權等功能。相關API的介紹可以看 C/C++:Windows編程—創建進程、終止進程、枚舉進程、枚舉線程、枚舉DLL
界面
先看看qt寫的界面吧。
代碼
創建進程
// 創建進程 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); }完整工程
工程代碼在這里可以下載。或者這里下載最新代碼。
總結
以上是生活随笔為你收集整理的Qt:Windows编程—Qt实现进程管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python判断进程是否存在
- 下一篇: windows 播放MP3音乐