参考:
http://www.bubuko.com/infodetail-1727473.html
https://blog.csdn.net/weixin_33751566/article/details/93942450
// 获取进程路径
CString CMFCApplication1Dlg::GetProcessPath(DWORD dwProcessID)
{
CString strProcessPath;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID);
if (hProcess == NULL)
{
int nErr = GetLastError();
CString strErr;
strErr.Format("%d", nErr);
MessageBox(strErr);
return "";
}
// 获取x86程序的路径
if (GetModuleFileNameEx(hProcess, NULL, strProcessPath.GetBufferSetLength(MAX_PATH), MAX_PATH) == 0)
{
// 获取64位程序的路径
if (GetProcessImageFileName(hProcess, strProcessPath.GetBufferSetLength(MAX_PATH), MAX_PATH) == 0)
{
strProcessPath = "";
}
else
{
// 转为绝对路径
strProcessPath = DosDevicePath2LogicalPath(strProcessPath);
}
}
strProcessPath.ReleaseBuffer();
CloseHandle(hProcess);
return strProcessPath;
}
// 把DOS路径转为绝对路径
CString CMFCApplication1Dlg::DosDevicePath2LogicalPath(LPCTSTR lpszDosPath)
{
CString strResult;
// Translate path with device name to drive letters.
TCHAR szTemp[MAX_PATH];
szTemp[0] = '\0';
if (lpszDosPath == NULL || !GetLogicalDriveStrings(_countof(szTemp) - 1, szTemp)) {
return strResult;
}
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(" :");
BOOL bFound = FALSE;
TCHAR* p = szTemp;
do {
// Copy the drive letter to the template string
*szDrive = *p;
// Look up each device name
if (QueryDosDevice(szDrive, szName, _countof(szName))) {
UINT uNameLen = (UINT)_tcslen(szName);
if (uNameLen < MAX_PATH)
{
bFound = _tcsnicmp(lpszDosPath, szName, uNameLen) == 0;
if (bFound) {
// Reconstruct pszFilename using szTemp
// Replace device path with DOS path
TCHAR szTempFile[MAX_PATH];
_stprintf_s(szTempFile, TEXT("%s%s"), szDrive, lpszDosPath + uNameLen);
strResult = szTempFile;
}
}
}
// Go to the next NULL character.
while (*p++);
} while (!bFound && *p); // end of string
return strResult;
}