2011-05-04 107 views

回答

16

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

但是,如果這個代碼在64位的Delphi編譯的,這是不正確的結果。 「IsWow64Process中的IsWow64:如果該進程是在64位Windows下運行的64位應用程序,則該值也設置爲FALSE。」我們總是從64位進程中獲得False – 2015-11-25 11:20:24

相關問題