| 1 | #include "ProcessListWindows.h" |
| 2 | |
| 3 | |
| 4 | #if defined(Q_OS_WIN) |
| 5 | typedef LONG (WINAPI *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG); |
| 6 | |
| 7 | PROCNTQSIP NtQueryInformationProcess; |
| 8 | #endif |
| 9 | |
| 10 | |
| 11 | #if defined(Q_OS_WIN) |
| 12 | BOOL ProcessListWindows::GetProcessCmdLine(DWORD dwId,LPWSTR wBuf,DWORD dwBufLen) |
| 13 | { |
| 14 | LONG status; |
| 15 | HANDLE hProcess; |
| 16 | PROCESS_BASIC_INFORMATION pbi; |
| 17 | PEB Peb; |
| 18 | PROCESS_PARAMETERS ProcParam; |
| 19 | SIZE_T dwDummy; |
| 20 | DWORD dwSize; |
| 21 | LPVOID lpAddress; |
| 22 | BOOL bRet = FALSE; |
| 23 | |
| 24 | // Get process handle |
| 25 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,dwId); |
| 26 | if (!hProcess) |
| 27 | return FALSE; |
| 28 | |
| 29 | // Retrieve information |
| 30 | status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, (PVOID)&pbi, sizeof(PROCESS_BASIC_INFORMATION), NULL); |
| 31 | |
| 32 | |
| 33 | if (status) |
| 34 | goto cleanup; |
| 35 | |
| 36 | if ( !ReadProcessMemory(hProcess, pbi.PebBaseAddress, &Peb, sizeof(PEB), &dwDummy) ) |
| 37 | goto cleanup; |
| 38 | |
| 39 | if ( !ReadProcessMemory(hProcess, Peb.ProcessParameters, &ProcParam, sizeof(PROCESS_PARAMETERS), &dwDummy) ) |
| 40 | goto cleanup; |
| 41 | |
| 42 | lpAddress = ProcParam.CommandLine.Buffer; |
| 43 | dwSize = ProcParam.CommandLine.Length; |
| 44 | |
| 45 | if (dwBufLen<dwSize) |
| 46 | goto cleanup; |
| 47 | |
| 48 | if ( !ReadProcessMemory(hProcess, lpAddress, wBuf, dwSize, &dwDummy) ) |
| 49 | goto cleanup; |
| 50 | |
| 51 | bRet = TRUE; |
| 52 | |
| 53 | cleanup: |
| 54 | CloseHandle (hProcess); |
| 55 | |
| 56 | return bRet; |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | ProcessListWindows::ProcessListWindows(QObject *parent) : QObject(parent){ |
| 62 | |
| 63 | } |
| 64 | |
| 65 | int ProcessListWindows::initProccessList(){ |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | QString ProcessListWindows::getProgram(QString pid){ |
| 70 | |
| 71 | int iPID = pid.toInt(); |
| 72 | |
| 73 | #if defined(Q_OS_WIN) |
| 74 | HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, iPID); |
| 75 | if (hProcess==NULL) return "-"; |
| 76 | |
| 77 | WCHAR szFilePath[MAX_PATH] = {0}; |
| 78 | GetModuleFileNameEx(hProcess, NULL, szFilePath, MAX_PATH); |
| 79 | CloseHandle(hProcess); |
| 80 | return QString::fromWCharArray(szFilePath); |
| 81 | |
| 82 | #endif |
| 83 | |
| 84 | return ""; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | |
| 89 | QString ProcessListWindows::getCmdline(QString pid){ |
| 90 | #if defined(Q_OS_WIN) |
| 91 | NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress( GetModuleHandle((const WCHAR*)"NTDLL"), "NtQueryInformationProcess" ); |
| 92 | |
| 93 | if (!NtQueryInformationProcess) return ""; |
| 94 | |
| 95 | DWORD dwId; |
| 96 | sscanf(pid.toStdString().c_str(),"%lu",&dwId); |
| 97 | |
| 98 | WCHAR wstr[255]; |
| 99 | |
| 100 | if (GetProcessCmdLine(dwId,wstr,sizeof(wstr))){ |
| 101 | //wprintf(L"Command line for process %lu is:\n%s\n",dwId,wstr); |
| 102 | return QString::fromWCharArray(wstr); |
| 103 | } |
| 104 | //wprintf(L"Could not get command line!"); |
| 105 | #endif |
| 106 | |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 110 | QStringList ProcessListWindows::getSockets(QString pid){ |
| 111 | return QStringList(); |
| 112 | } |
| 113 | |
| 114 | sProccess ProcessListWindows::getProc(QString pid){ |
| 115 | sProccess proc; |
| 116 | proc.pid=pid; |
| 117 | //proc.sockets=getSockets(pid); |
| 118 | proc.program=getProgram(pid); |
| 119 | proc.cmdline=getCmdline(pid); |
| 120 | |
| 121 | return proc; |
| 122 | } |