Qt:Windows编程—Qt实现进程管理
前⾔
继续学window编程,学呀学呀 学⽆⽌境,学到Windows进程相关API了。利⽤相关的API使⽤Qt写界⾯实现了⼀个简单的进程管理。主要⽤到 进程的创建、进程的枚举、线程的枚举、DLL的枚举、进程提权等功能。相关API的介绍可以看
界⾯
先看看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 = StdString().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);
}
停⽌进程
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);
}
恢复进程
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)
{
createprocessaqDebug() << "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);
}
完整⼯程
⼯程代码在这⾥。或者这⾥。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论