gpt4 book ai didi

Delphi 获取 EXE 的句柄

转载 作者:行者123 更新时间:2023-12-03 15:20:30 27 4
gpt4 key购买 nike

这是我现在如何做的一个例子:

var
Client : String;
Handle : Integer;
begin
Client := 'Window Name';
GetWindowThreadProcessId(FindWindow(nil, PAnsiChar(Client)), @ProcessId);
Handle := OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId);
end;

我宁愿捕获进程的句柄及其exe名称......这可能吗?

最佳答案

由于 vcldeveloper 提供的链接已损坏,因此这里是无需第 3 方组件即可工作的完整功能代码。

首先我们会找到进程ID(PID),然后我们将通过打开所有访问来获取进程句柄(因为评论中提到OP他需要这个来实现ReadProcessMemory功能)。

如果PID函数返回0,则意味着该进程很可能没有运行(或者只是在运行进程列表中找不到)

function GetPIDbyProcessName(processName:String):integer;
var
GotProcess: Boolean;
tempHandle: tHandle;
procE: tProcessEntry32;
begin
tempHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
procE.dwSize:=SizeOf(procE);
GotProcess:=Process32First(tempHandle, procE);
{$B-}
if GotProcess and not SameText(procE.szExeFile, processName) then
repeat GotProcess := Process32Next(tempHandle, procE);
until (not GotProcess) or SameText(procE.szExeFile,processName);
{$B+}

if GotProcess then
result := procE.th32ProcessID
else
result := 0; // process not found in running process list

CloseHandle(tempHandle);
end;

接下来,我们将从我们获得的 PID 中获取/打开进程句柄。整个代码/用法如下:

var myPID, myProcessHandle: integer;
begin
myPID:=GetPIDbyProcessName('someExeName.exe');
myProcessHandle:=OpenProcess(PROCESS_ALL_ACCESS,False,myPID);
end;

您应该以可作为第一个参数的
ReadProcessMemory(myProcessHandle...) 的方式存储 myProcessHandle

此外,将这些添加到您的全局使用条款中:
Winapi.Windows(用于 ReadProcessMemory 和 OpenProcess)
Winapi.tlHelp32(用于获取PID tProcessEntry32变量)

关于Delphi 获取 EXE 的句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4843181/

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