【问题标题】:How i can determine when a process id (PID) is 32 or 64 bit application?我如何确定进程 ID (PID) 何时是 32 位或 64 位应用程序?
【发布时间】:2011-05-04 19:41:28
【问题描述】:

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

【问题讨论】:

    标签: delphi winapi


    【解决方案1】:

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

    【讨论】:

    • 但是,如果这段代码是在 64 位 Delphi 下编译的,则结果不正确。 “IsWow64 from IsWow64Process:如果进程是在 64 位 Windows 下运行的 64 位应用程序,则该值也设置为 FALSE。”我们总是从 64 位进程中得到 False
    【解决方案2】:

    如果您正在检查应用程序本身:

    {$IFDEF WIN32}
        ShowMessage('32-bit App itself');
    {$ENDIF}
    {$IFDEF WIN64}
        ShowMessage('64-bit App itself');
    {$ENDIF}
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 2012-08-04
      • 1970-01-01
      • 2010-12-23
      • 2010-12-29
      • 2012-01-19
      相关资源
      最近更新 更多