gpt4 book ai didi

delphi - 使用delphi从PID获取完整路径

转载 作者:行者123 更新时间:2023-12-03 14:39:57 25 4
gpt4 key购买 nike

我需要从 PID 获取完整路径。

我已经检查过这个问题C++ Windows - How to get process path from its PID我编写了以下代码:

 function GetFullPathFromPID(PID: DWORD): string;
var
hProcess: THandle;
ModName : Array[0..MAX_PATH + 1] of Char;
begin
Result:='';
hProcess := OpenProcess(PROCESS_ALL_ACCESS,False, PID);
try
if hProcess <> 0 then
if GetModuleFileName(hProcess, ModName, Sizeof(ModName))<>0 then
Result:=ModName
else
ShowMessage(SysErrorMessage(GetLastError));
finally
CloseHandle(hProcess);
end;
end;

但始终返回此消息:

the specified module could not be found

如何从 PID 获取完整路径?

最佳答案

您需要使用GetModuleFileNameEx功能。来自 MSDN:

GetModuleFileName Function

Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.

To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.

示例用法(使用 PsAPI):

function GetPathFromPID(const PID: cardinal): string;
var
hProcess: THandle;
path: array[0..MAX_PATH - 1] of char;
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
if hProcess <> 0 then
try
if GetModuleFileNameEx(hProcess, 0, path, MAX_PATH) = 0 then
RaiseLastOSError;
result := path;
finally
CloseHandle(hProcess)
end
else
RaiseLastOSError;
end;

关于delphi - 使用delphi从PID获取完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4178443/

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