To obtain the process running path through the process PID in VC++, you can use the OpenProcess, GetModuleFileNameEx and CloseHandle functions in the Win32 API.
#include <Windows.h>
#include <iostream>
#include <psapi.h>
#pragma comment(lib, "psapi.lib")
int main() {
// 获取进程句柄
DWORD pid = 1234; // 替换为您实际的进程PID
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (handle == NULL) {
std::cout << "无法打开进程" << std::endl;
return 1;
}
// 获取进程运行路径
char path[MAX_PATH];
DWORD size = GetModuleFileNameEx(handle, NULL, path, MAX_PATH);
if (size == 0) {
std::cout << "无法获取进程运行路径" << std::endl;
CloseHandle(handle);
return 1;
}
std::cout << "进程运行路径:" << path << std::endl;
// 关闭进程句柄
CloseHandle(handle);
return 0;
}
In this example, we first open the process handle using the OpenProcess function. Then, we use the GetModuleFileNameEx function to get the process running path and store it in a buffer.
Finally, output the running path of the process and close the process handle. Note that pid needs to be replaced with your actual process PID.