windows 从进程 ID 中获取进程可执行文件名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8475009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a process executable name from process ID
提问by jgpt
I am currently trying to get the names of a list of programs whose pid I have.
我目前正在尝试获取我拥有其 pid 的程序列表的名称。
The program is run as administrator, but GetModuleFileNameEx
fails with error code 5.
该程序以管理员身份运行,但GetModuleFileNameEx
失败并显示错误代码 5。
I open the program with OpenProcess(PROCESS_TERMINATE,PROCESS_QUERY_INFORMATION)
and I have the SE_DEBUG_PRIVILEGE
enabled.
我打开程序OpenProcess(PROCESS_TERMINATE,PROCESS_QUERY_INFORMATION)
并SE_DEBUG_PRIVILEGE
启用了。
回答by hmjd
The process handle passed to GetModuleFileNameEx()requires PROCESS_QUERY_INFORMATION
and PROCESS_VM_READ
access rights.
传递给进程句柄)GetModuleFileNameEx(需要PROCESS_QUERY_INFORMATION
和PROCESS_VM_READ
访问权限。
This worked for me:
这对我有用:
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
6088);
if (0 == h)
{
std::cerr << "OpenProcess() failed: " << GetLastError() << "\n";
}
else
{
char exe_path[2048] = {};
if (GetModuleFileNameEx(h, 0, exe_path, sizeof(exe_path) - 1))
{
std::cout << exe_path << "\n";
}
else
{
std::cerr << "GetModuleFileNameEx() failed: " <<
GetLastError() << "\n";
}
CloseHandle(h);
}
However, as others have pointed out (and is also stated in documentation for GetModuleFileNameEx()) there are safer ways to acquire this information:
然而,正如其他人所指出的(在 GetModuleFileNameEx() 的文档中也有说明),有更安全的方法来获取这些信息:
回答by pritaeas
According to this threadthat error is returned when there's not enough information to return the filename.
根据此线程,当没有足够的信息返回文件名时,将返回错误。