delphi – 我可以确定一个进程ID(PID)是32位还是64位应用程序?

我需要确定一个进程id(PID)是32或64位应用程序使用delphi,我该怎么办?我真的检查了 IsWow64Process功能,但使用过程句柄不是PID.

解决方法

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

请记住,您必须使用GetProcAddress功能加载IsWow64Process功能,因为某些版本的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.

相关文章

ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c++ 调用, 抽空研究了一下该库的最新版 ,把...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和EDX)2个变址和指针寄存器(ESI和EDI) 2个指针寄...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的功能是:将源操作数送到目的操作数中,即:...
有三个API函数可以运行可执行文件WinExec、ShellExecute和CreateProcess。 1.CreateProcess因为使用复杂...
API原型: Declare Function MoveFileEx& Lib "kernel32" Alias "MoveFileExA" (By...