1 #include "Psapi.h" 2 #pragma comment(lib, "Psapi.lib ")//需要包含头文件哦!
1 /* 功 能:获取指定进程所对应的可执行(EXE)文件全路径 2 * 参 数:hProcess - 进程句柄。必须具有PROCESS_QUERY_INFORMATION 或者 3 PROCESS_QUERY_LIMITED_INFORMATION 权限 4 * sFilePath - 进程句柄hProcess所对应的可执行文件路径 5 * 返回值: 6 */ 7 void GetProcessFilePath(IN HANDLE hProcess, OUT CString& sFilePath) 8 { 9 sFilePath = _T(""); 10 TCHAR tsFileDosPath[MAX_PATH + 1]; 11 ZeroMemory(tsFileDosPath, sizeof(TCHAR)*(MAX_PATH + 1)); 12 if (0 == GetProcessImageFileName(hProcess, tsFileDosPath, MAX_PATH + 1)) 13 { 14 return; 15 } 16 17 // 获取Logic Drive String长度 18 UINT uiLen = GetLogicalDriveStrings(0, NULL); 19 if (0 == uiLen) 20 { 21 return; 22 } 23 24 PTSTR pLogicDriveString = new TCHAR[uiLen + 1]; 25 ZeroMemory(pLogicDriveString, uiLen + 1); 26 uiLen = GetLogicalDriveStrings(uiLen, pLogicDriveString); 27 if (0 == uiLen) 28 { 29 delete[]pLogicDriveString; 30 return; 31 } 32 33 TCHAR szDrive[3] = TEXT(" :"); 34 PTSTR pDosDriveName = new TCHAR[MAX_PATH]; 35 PTSTR pLogicIndex = pLogicDriveString; 36 37 do 38 { 39 szDrive[0] = *pLogicIndex; 40 uiLen = QueryDosDevice(szDrive, pDosDriveName, MAX_PATH); 41 if (0 == uiLen) 42 { 43 if (ERROR_INSUFFICIENT_BUFFER != GetLastError()) 44 { 45 break; 46 } 47 48 delete[]pDosDriveName; 49 pDosDriveName = new TCHAR[uiLen + 1]; 50 uiLen = QueryDosDevice(szDrive, pDosDriveName, uiLen + 1); 51 if (0 == uiLen) 52 { 53 break; 54 } 55 } 56 57 uiLen = _tcslen(pDosDriveName); 58 if (0 == _tcsnicmp(tsFileDosPath, pDosDriveName, uiLen)) 59 { 60 sFilePath.Format(_T("%s%s"), szDrive, tsFileDosPath + uiLen); 61 break; 62 } 63 64 while (*pLogicIndex++); 65 } while (*pLogicIndex); 66 67 delete[]pLogicDriveString; 68 delete[]pDosDriveName; 69 }
时间: 2024-10-31 20:25:04