gpt4 book ai didi

delphi - 我如何确定进程 ID (PID) 是 32 位还是 64 位应用程序?

转载 作者:行者123 更新时间:2023-12-03 14:40:05 27 4
gpt4 key购买 nike

我需要使用 delphi 确定进程 ID (PID) 是 32 位还是 64 位应用程序,我该怎么做?我真的检查了IsWow64Process函数,但使用进程句柄而不是 PID。

最佳答案

您可以使用OpenProcess函数获取 pid 的句柄,然后调用 IsWow64Process功能。

请记住,您必须加载 IsWow64Process使用 GetProcAddress 的函数功能,因为某些版本的 Windows 不包含此功能。

检查此示例代码

{$APPTYPE CONSOLE}

uses
Windows,
SysUtils;

type
TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall;
var
IsWow64Process : TIsWow64Process;

procedure Init_IsWow64Process;
var
hKernel32 : Integer;
begin
hKernel32 := LoadLibrary(kernel32);
if (hKernel32 = 0) then RaiseLastOSError;
try
IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process');
finally
FreeLibrary(hKernel32);
end;
end;

function PidIs64BitsProcess(dwProcessId: DWORD): Boolean;
var
IsWow64 : BOOL;
PidHandle : THandle;
begin
Result := False;
if Assigned(IsWow64Process) then
begin
//check if the current app is running under WOW
if IsWow64Process(GetCurrentProcess(), IsWow64) then
Result := IsWow64
else
RaiseLastOSError;

//the current delphi App is not running under wow64, so the current Window OS is 32 bit
//and obviously all the apps are 32 bits.
if not Result then Exit;

PidHandle := OpenProcess(PROCESS_QUERY_INFORMATION,False,dwProcessId);
if PidHandle > 0 then
try
if (IsWow64Process(PidHandle, IsWow64)) then
Result := not IsWow64
else
RaiseLastOSError;
finally
CloseHandle(PidHandle);
end;
end;
end;


begin
try
Init_IsWow64Process;
//here pass the pid which you want to check
Writeln(BoolToStr(PidIs64BitsProcess(1940),True));
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln;
end.

关于delphi - 我如何确定进程 ID (PID) 是 32 位还是 64 位应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5888975/

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