C++ Windows - 如何从其 PID 获取进程路径(错误)

标签 c++ windows process path psapi

我想用pid来获取进程的完整路径。

#include <psapi.h>

HANDLE processHandle = NULL;
TCHAR filename[MAX_PATH];

processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nPID);
if (processHandle != NULL) 
{
    if (GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH) == 0) 
    {
        //fail to get module file name
    } 
    else 
    {
        //module file name : filename
    }
    CloseHandle(processHandle);
} 
else 
{
    //fail to open process
}

这是使用pid获取进程路径的代码。

但是,当我执行它时,出现以下错误。

(我用的是visual c++6.0。)

Linking...
Process01Dlg.obj : error LNK2001: unresolved external symbol _GetModuleFileNameExA@16
Debug/Process01.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
  • QueryFullProcessImageName
  • 获取模块文件名
  • 获取模块文件名Ex
  • 获取进程图像文件名

以上方法都报错了。

是不是版本问题?

请回答。谢谢:)

最佳答案

您似乎忘记将您的产品与 psapi.lib 链接起来。将其添加到项目依赖项中。

不确定它是否适用于 VC6.0。

但是 MSDN 推荐使用其他函数来检索进程名称:

To retrieve the name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function. This is more efficient and more reliable than calling the GetModuleFileNameEx function with a NULL module handle.

关于C++ Windows - 如何从其 PID 获取进程路径(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48858330/

相关文章:

c - 变量.c :4:1: error: expected declaration specifiers or '...' before '(' token

windows - 下载VBS错误 "(null): 0x80072EE6"

Windows 资源管理器外壳扩展:通过 ITransferSource::RemoveItem 递归删除?

java - 如何杀死我们在java中使用exec启动的进程 {.exec ("cmd/c"+command)}

c++ - vector<bool> 可以由 int 初始化吗

c++ - 如何在不使用真正的剪贴板的情况下进行类似剪贴板的操作?

C#/单声道 : get list of child processes on Windows and Linux

java - 从 java 程序运行 bash 脚本时的奇怪行为

c++ - 在 Visual Studio 中编译 C++

c++ - 替换字符串中的一些字符