gpt4 book ai didi

c++ - 如何在 C++ 中获取进程名称

转载 作者:IT老高 更新时间:2023-10-28 13:03:15 28 4
gpt4 key购买 nike

如何在 Windows 中使用 C++ 从 PID 中获取进程名称?

最佳答案

我猜是 OpenProcess鉴于您的进程拥有必要的权限,函数应该会有所帮助。获得进程句柄后,您可以使用 GetModuleFileNameEx函数获取进程的完整路径(.exe文件的路径)。

#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include "stdio.h"
#include "psapi.h"
// Important: Must include psapi.lib in additional dependencies section
// In VS2005... Project > Project Properties > Configuration Properties > Linker > Input > Additional Dependencies

int _tmain(int argc, _TCHAR* argv[])
{
HANDLE Handle = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
8036 /* This is the PID, you can find one from windows task manager */
);
if (Handle)
{
TCHAR Buffer[MAX_PATH];
if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
// At this point, buffer contains the full path to the executable
}
else
{
// You better call GetLastError() here
}
CloseHandle(Handle);
}
return 0;
}

关于c++ - 如何在 C++ 中获取进程名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4570174/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com