- 导入库 pro
win32:LIBS += -lpsapi
win32:LIBS += -lkernel32
- 获取列表
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
#include <QtDebug>
#include <locale>
#include <tchar.h>
#include <string>
#include <QMessageBox>
#include <stdio.h>
#include <psapi.h>
void MainWindow::on_pushButton_2_clicked()
{
// 获取进程标识符列表。
DWORD aProcesses[1024], cbNeeded;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
qDebug() << "获取失败";
return;
}
// 计算返回了多少个进程标识符。
unsigned int cProcesses = cbNeeded / sizeof(DWORD);
// 打印每个进程的名称和进程标识符。
unsigned int i;
for ( i = 0; i < cProcesses; i++ )
{
DWORD processID = aProcesses[i];
if(processID == 0 ) continue;
char buff[255];
// 打开现有的本地过程对象
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE, processID );
if(hProcess == NULL)continue;
// 获取进程名称。
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),&cbNeeded) )
{
GetModuleBaseNameA(hProcess, hMod, (LPSTR)&buff, DWORD(sizeof(buff)/sizeof(char)));
}
QString s= QString::fromLocal8Bit(buff);
qDebug() << "PID: " << processID << ", name: " << s;
// 释放该过程的句柄。
CloseHandle( hProcess );
}
}
参考:
- https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocesses
- https://docs.microsoft.com/windows/desktop/psapi/enumerating-all-processes
- https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamea
- https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess