c++ - QT : How to store all running processes full path of windows in QStringList? -
i need scan running processes of windows 1 one using qt. want store full path of running processes in qstringlist.
i tried code got error crt0_c.c:-1: error: undefined reference `winmain@16' collect2.exe:-1: error: error: ld returned 1 exit status
unsigned long aprocesses[1024], cbneeded, cprocesses; if (!enumprocesses(aprocesses, sizeof(aprocesses), &cbneeded)) { } qstringlist lprocess; cprocesses = cbneeded / sizeof(unsigned long); (unsigned int = 0; < cprocesses; i++) { if (aprocesses[i] == 0) continue; handle hprocess = openprocess(process_query_information | process_vm_read, 0, aprocesses[i]); wchar_t buffer[50]; getmodulebasename(hprocess, 0, buffer, 50); closehandle(hprocess); lprocess << qstring::fromwchararray(buffer); } ui->listwidget->additems(lprocess);
you using name main
function.
aside that, missing lot of other functions. need add library getmodulefilenameex
. needs windows library "psapi.lib
" or "libpsapi.a
" if using mingw
win32: libs += -lpsapi
example:
#include <qcoreapplication> #include <qstring> #include <qstringlist> #include <iostream> #include <windows.h> #include <tlhelp32.h> #include <psapi.h> qstringlist foo() { qstringlist list; handle hndl = createtoolhelp32snapshot(th32cs_snapprocess | th32cs_snapmodule, 0); if (hndl) { processentry32 process; memset(&process, 0, sizeof(processentry32)); process.dwsize = sizeof(processentry32); process32first(hndl, &process); { qstring qpath; wchar_t path[max_path] = l""; handle hprocess = openprocess( process_query_information, false, process.th32processid); if (hprocess) { getmodulefilenameex(hprocess, null, path, max_path); qpath = qstring::fromutf16((const ushort*)path); closehandle(hprocess); } else { qpath = qstring::fromutf16((const ushort*)process.szexefile); } list.append(qpath); } while (process32next(hndl, &process)); closehandle(hndl); } return list; }
usage:
qstringlist list = foo(); for(int = 0; < list.count(); i++) { //print list[i]... //or std::wcout << (const wchar_t*)list[i].utf16() << std::endl; }
running program in admin mode full path:
#include <iostream> #include <windows.h> #include <winbase.h> #include <tlhelp32.h> #include <psapi.h> #include <shlobj.h> void test() { if (!isuseranadmin()) std::wcout << "run in admin mode!\n"; void* tokenhandle; token_privileges privilegetoken; lookupprivilegevalue(0, se_debug_name, &privilegetoken.privileges[0].luid); privilegetoken.privilegecount = 1; privilegetoken.privileges[0].attributes = se_privilege_enabled; openprocesstoken(getcurrentprocess(), token_adjust_privileges, &tokenhandle); adjusttokenprivileges(tokenhandle, 0, &privilegetoken, sizeof(token_privileges), 0, 0); closehandle(tokenhandle); handle hndl = createtoolhelp32snapshot(th32cs_snapprocess | th32cs_snapmodule, 0); if (hndl) { processentry32 process; memset(&process, 0, sizeof(processentry32)); process.dwsize = sizeof(processentry32); process32first(hndl, &process); { wchar_t path[max_path] = l""; wcscpy_s(path, max_path, process.szexefile); handle hprocess = openprocess(process_query_limited_information, false, process.th32processid); if (hprocess) { getmodulefilenameexw(hprocess, 0, path, max_path); closehandle(hprocess); } else { //in case failed in winxp hprocess = openprocess(process_query_information | process_vm_read, false, process.th32processid); if (hprocess) { getmodulefilenameexw(hprocess, 0, path, max_path); closehandle(hprocess); } } std::wcout << path << std::endl; } while (process32next(hndl, &process)); closehandle(hndl); } }
Comments
Post a Comment