這幾天在學習windows的遠程協助,學到關於獲取系統進程完整路徑時,遇到了無法獲取高權限系統進程的路徑的問題,網上找了下,最後在csdn中找到了一個解決方法,個人感覺很好,於是借鑑來分享下(感謝tpnndhqc提供的方法)。
//c/c++ code #include <stdio.h> #include <windows.h> #include <tlhelp32.h> #include <string> #include <tchar.h> #include <Psapi.h> #include <conio.h> #pragma comment (lib,"Psapi.lib") BOOL DosPathToNtPath(LPTSTR pszDosPath, LPTSTR pszNtPath) { TCHAR szDriveStr[500]; TCHAR szDrive[3]; TCHAR szDevName[100]; INT cchDevName; INT i; //檢查參數 if(!pszDosPath || !pszNtPath ) return FALSE; //獲取本地磁盤字符串 if(GetLogicalDriveStrings(sizeof(szDriveStr), szDriveStr)) { for(i = 0; szDriveStr[i]; i += 4) { if(!lstrcmpi(&(szDriveStr[i]), _T("A:\\")) || !lstrcmpi(&(szDriveStr[i]), _T("B:\\"))) continue; szDrive[0] = szDriveStr[i]; szDrive[1] = szDriveStr[i + 1]; szDrive[2] = '\0'; if(!QueryDosDevice(szDrive, szDevName, 100))//查詢 Dos 設備名 return FALSE; cchDevName = lstrlen(szDevName); if(_tcsnicmp(pszDosPath, szDevName, cchDevName) == 0)//命中 { lstrcpy(pszNtPath, szDrive);//複製驅動器 lstrcat(pszNtPath, pszDosPath + cchDevName);//複製路徑 return TRUE; } } } lstrcpy(pszNtPath, pszDosPath); return FALSE; } //獲取進程完整路徑 BOOL GetProcessFullPath(DWORD dwPID, TCHAR pszFullPath[MAX_PATH]) { TCHAR szImagePath[MAX_PATH]; HANDLE hProcess; if(!pszFullPath) return FALSE; pszFullPath[0] = '\0'; hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPID); if(!hProcess) return FALSE; if(!GetProcessImageFileName(hProcess, szImagePath, MAX_PATH)) { CloseHandle(hProcess); return FALSE; } if(!DosPathToNtPath(szImagePath, pszFullPath)) { CloseHandle(hProcess); return FALSE; } CloseHandle(hProcess); _tprintf(_T("%d,%s \r\n"),dwPID,pszFullPath); return TRUE; } int main(int argc, char* argv[]) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (INVALID_HANDLE_VALUE == hSnapshot) { return NULL; } PROCESSENTRY32 pe ={0}; pe.dwSize = sizeof(PROCESSENTRY32); BOOL fOk; for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)) { TCHAR szProcessName[MAX_PATH] = {0}; GetProcessFullPath(pe.th32ProcessID, szProcessName); } getch(); return 0; }
效果圖:
原文出自:http://bbs.csdn.net/topics/390937481
轉載請註明原出處及原作者。