问题 我如何确定进程ID(PID)何时是32或64位应用程序?


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


1932
2018-05-04 19:41


起源



答案:


你可以使用 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.

16
2018-05-04 19:49



但是,如果此代码在64位Delphi下编译,则结果不正确。 “来自IsWow64Process的IsWow64:如果进程是在64位Windows下运行的64位应用程序,则该值也设置为FALSE。”并且我们从64位进程始终是False - Alex Egorov


答案:


你可以使用 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.

16
2018-05-04 19:49



但是,如果此代码在64位Delphi下编译,则结果不正确。 “来自IsWow64Process的IsWow64:如果进程是在64位Windows下运行的64位应用程序,则该值也设置为FALSE。”并且我们从64位进程始终是False - Alex Egorov