BOOL
GetProcPath(
__in BOOL bCurrentProc,
__in ULONG ulPid,
__inout LPTSTR lpProcPath,
__in ULONG ulProcPathBufLen
)
{
BOOL bRet = FALSE;
HMODULE hModule = NULL;
HANDLE hProc = NULL;
DWORD dwProcPathLen = 0;
TCHAR tchProcPathDev[MAX_PATH] = {0};
TCHAR tchVolNameDev[MAX_PATH] = {0};
TCHAR tchVolName[MAX_PATH] = {0};
__try
{
if (!lpProcPath || !ulProcPathBufLen || (!bCurrentProc && !ulPid))
{
printf("[GetProcPath] : input parameter error \n");
__leave;
}
if (bCurrentProc)
{
if (!GetModuleFileName(NULL, lpProcPath, ulProcPathBufLen / sizeof(TCHAR)))
{
printf("[GetProcPath] : GetModuleFileName failed. (%d) \n", GetLastError());
__leave;
}
bRet = TRUE;
__leave;
}
hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, ulPid);
if (!hProc)
{
printf("[GetProcPath] : OpenProcess failed. (%d) \n", GetLastError());
__leave;
}
hModule = LoadLibrary(_T("Kernel32.dll"));
if (!hModule)
{
printf("[GetProcPath] : LoadLibrary failed. (%d) \n", GetLastError());
__leave;
}
if (GetProcAddress(hModule, "QueryFullProcessImageNameA"))
{
dwProcPathLen = ulProcPathBufLen / sizeof(TCHAR);
if (!QueryFullProcessImageName(hProc, 0, lpProcPath, &dwProcPathLen))
{
printf("[GetProcPath] : QueryFullProcessImageName failed. (%d) \n", GetLastError());
__leave;
}
bRet = TRUE;
__leave;
}
if (!GetProcessImageFileName(hProc, tchProcPathDev, MAX_PATH))
{
printf("[GetProcPath] : GetProcessImageFileName failed. (%d) \n", GetLastError());
__leave;
}
_tcscat_s(tchVolName, MAX_PATH, _T("A:"));
for (; *tchVolName <= _T('Z'); (*tchVolName)++)
{
memset(tchVolNameDev, 0, MAX_PATH * sizeof(TCHAR));
if (!QueryDosDevice(tchVolName, tchVolNameDev, MAX_PATH))
{
if (GetLastError() == 2)
continue;
else
{
printf("[GetProcPath] : QueryDosDevice failed. (%d) \n", GetLastError());
__leave;
}
}
if (_tcsncmp(tchProcPathDev, tchVolNameDev, _tcslen(tchVolNameDev)) == 0)
{
bRet = TRUE;
break;
}
}
if (bRet)
{
_tcscat_s(lpProcPath, ulProcPathBufLen / sizeof(TCHAR), tchVolName);
_tcscat_s(lpProcPath, ulProcPathBufLen / sizeof(TCHAR), tchProcPathDev + _tcslen(tchVolNameDev));
}
}
__finally
{
if (hModule)
FreeLibrary(hModule);
if (hProc)
CloseHandle(hProc);
}
return bRet;
}
获取进程全路径
最新推荐文章于 2022-05-08 16:04:32 发布