How to use:
Qmap<qstring,qint64> App_pid;
Getallapppidlist (APP_PID);
#include <tlhelp32.h>//for CreateToolhelp32Snapshot
#include <Psapi.h> //For Getmodulefilenameex
#define FORMAT_PATH (PATH) path.replace (' \ \ ', '/'). ToLower ()
QString getpathbyprocessid (DWORD pid) {HANDLE hprocess=openprocess (process_all_access, FALSE, PID); if(!hprocess) { //qmessagebox::warning (NULL, "Getpathbyprocessid", "unauthorized access to the process"); return ""; } WCHAR Filepath[max_path]; DWORD ret=Getmodulefilenameex (hprocess, NULL, FilePath, MAX_PATH); QString file=qstring::fromstdwstring (FilePath); //qmessagebox::warning (NULL, "Getpathbyprocessid ret=", Qstring::number (ret) + ":" +file);CloseHandle (hprocess); returnret==0?"": file;}voidUdpclient::getallapppidlist (qmap<qstring,qint64>&app_pid) {PROCESSENTRY32 pe32; Pe32.dwsize=sizeof(PE32); HANDLE Hprocesssnap= CreateToolhelp32Snapshot (th32cs_snapprocess,0); if(Hprocesssnap = =Invalid_handle_value) {Warninglabel->settext ("CreateToolhelp32Snapshot call failed"); return ; } BOOL bmore= Process32First (hprocesssnap,&pe32); while(bmore) {//printf ("Process name:%s\n", pe32.szexefile); //printf ("Process id:%u\n\n", pe32.th32processid);QString exename= (Qstring::fromutf16 (reinterpret_cast<ConstUnsigned Short*>(Pe32.szexefile))); QString ExePath=Getpathbyprocessid (PE32.TH32PROCESSID); ExePath=Format_path (ExePath); if(Exepath.isempty ()) {Warninglabel->settext ("Get Process"+ EXEName +"Path Failure"); } Else{App_pid[exepath]=Pe32.th32processid; } bmore= Process32Next (hprocesssnap,&pe32); } closehandle (Hprocesssnap);}
List of Qt scan processes and get process information