2010-03-26 59 views
7

我如何獲得有關Windows操作系統類型的信息?它是32位還是64位?我如何以編程方式獲取這些信息?如何獲取有關計算機的信息? [32位或64位]

+2

「compiuter」? 「abaout」? 「programory」?請在發佈之前花時間閱讀您的問題。 – 2010-03-26 14:32:59

+4

@Joachim Sauer:StackOverflow是一個國際論壇,你不能指望每個人都有完美的英語 - 只需修復煩人的語法錯誤即可。 – kludg 2010-03-26 15:34:01

+1

@Serg:不完美的英文和簡單的錯別字是兩個不同的東西。我很確定,「abaout」是後者。我對英語不完全沒有任何問題(我本人不是母語)。 – 2010-03-26 15:51:56

回答

6

您需要使用GetProcAddress()在運行時檢查IsWow64Process()功能的可用性,就像這樣:

function Is64BitWindows: boolean; 
type 
    TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL; 
    stdcall; 
var 
    DLLHandle: THandle; 
    pIsWow64Process: TIsWow64Process; 
    IsWow64: BOOL; 
begin 
    Result := False; 
    DllHandle := LoadLibrary('kernel32.dll'); 
    if DLLHandle <> 0 then begin 
    pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process'); 
    Result := Assigned(pIsWow64Process) 
     and pIsWow64Process(GetCurrentProcess, IsWow64) and IsWow64; 
    FreeLibrary(DLLHandle); 
    end; 
end; 

,因爲該功能僅適用於那些有一個64位的味道Windows版本。聲明它爲external將阻止您的應用程序在SP2以前的Windows 2000或Windows XP上運行。

編輯:

克里斯已經張貼了關於緩存出於性能的考慮結果的評論。對於這個特定的API函數,這可能不是必需的,因爲kernel32.dll將一直存在(並且我無法想象一個程序甚至會在沒有它的情況下加載),但對於其他函數,情況可能不同。因此,這裏的是緩存功能結果的版本:

function Is64BitWindows: boolean; 
type 
    TIsWow64Process = function(hProcess: THandle; var Wow64Process: BOOL): BOOL; 
    stdcall; 
var 
    DLLHandle: THandle; 
    pIsWow64Process: TIsWow64Process; 
const 
    WasCalled: BOOL = False; 
    IsWow64: BOOL = False; 
begin 
    if not WasCalled then begin 
    DllHandle := LoadLibrary('kernel32.dll'); 
    if DLLHandle <> 0 then begin 
     pIsWow64Process := GetProcAddress(DLLHandle, 'IsWow64Process'); 
     if Assigned(pIsWow64Process) then 
     pIsWow64Process(GetCurrentProcess, IsWow64); 
     WasCalled := True; 
     FreeLibrary(DLLHandle); 
    end; 
    end; 
    Result := IsWow64; 
end; 

緩存此功能結果安全的,因爲API函數要麼是有或沒有,其結果不能在同一個Windows安裝改變。甚至可以安全地從多個線程同時調用它,因爲找到WasCalledFalse10的兩個線程都將調用該函數,將相同的結果寫入相同的內存位置,並且之後僅將WasCalled設置爲True

+0

如果程序需要多次知道答案,緩存結果將是一個好主意。即你不想在循環中調用它,因爲LoadLibrary會很昂貴,特別是當它找不到任何東西時。我並沒有敲響解決方案,只是提供使用案例的建議。 – 2010-03-26 18:02:22

+2

只有在項目設置中啓用了可分配/可寫常量時,此代碼纔會編譯。爲了避免對此編譯器設置敏感,可以引入指令以確保根據需要設置(並恢復)該編譯器行爲,或者更好,我建議使用單位變量作爲緩存結果(使用整數避免需要使用兩個這樣的變量:例如聲明initialised = -1表示「未設置」,set = 0表示Win32 set = 1表示Win64)。 – Deltics 2010-03-28 20:23:46

3

如果a)你在windows和b)你可以訪問註冊表,那麼HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion應該是提供信息的。

+0

對不起,但沒有註冊表。我正在使用windows – gedO 2010-03-26 14:33:56

+5

@gedO:你是自相矛盾的。 Windows意味着註冊表。嘗試從開始菜單運行「regedit」。 – Brian 2010-03-26 14:36:14

0

我不知道如何在Delphi中調用Win32函數。

但是,如果您編寫一個32位程序,您可以調用Win32 API IsWow64Process來了解您是否在64位操作系統中。

當然,如果你編寫一個64位的exe文件,它只能運行在64位的Windows上,所以不需要問。

+0

祝你用Delphi寫一個64位的exe文件。 ;) – Deltics 2010-03-28 20:19:03

+0

剛纔這樣做。 (2015) – penarthur66 2015-05-26 14:58:06

12
function IsWin64: Boolean; 
var 
    IsWow64Process : function(hProcess : THandle; var Wow64Process : BOOL): BOOL; stdcall; 
    Wow64Process : BOOL; 
begin 
    Result := False; 
    IsWow64Process := GetProcAddress(GetModuleHandle(Kernel32), 'IsWow64Process'); 
    if Assigned(IsWow64Process) then begin 
    if IsWow64Process(GetCurrentProcess, Wow64Process) then begin 
     Result := Wow64Process; 
    end; 
    end; 
end; 
1

除了IsWow64Process,將GetNativeSystemInfo API函數可能是你的興趣(它在Windows單元定義),以瞭解更多有關你的CPU(或者你可以使用裝配和CPUID)。

0

//沒有測試但你可以試試這個

is64 := (Environment.GetEnvironmentVariable('ProgramW6432') <> ''); 
+0

這個問題被標記爲'delphi',並沒有提及.net或Java。因此,你不能假定'Environment'可用。請在回答問題之前閱讀標籤,以確保您的答案適用於他們。如果你不確定,你可能不應該回答這個問題。 :-) – 2013-03-18 17:59:25

+0

德爾福的編碼器會到達那裏,注意到它必須有一個定義的對象環境,因此他會刪除它: is64:=({Environment。} GetEnvironmentVariable('ProgramW6432')<>''); 謝謝! – 2013-05-23 02:55:31

0

德爾福XE +

Uses System.SysUtils 

Function IsWin64Or32: string; 
Begin 
    if Pos('64-bit', TOSVersion.ToString) > 0 then 
    Result := '64-bit' 
    Else 
    Result := '32-bit'; 
End; 

lbl1.Caption := IsWin64Or32; 
0
function TForm2.Arch: string; 
begin 
if TOSVersion.Architecture=arIntelX86 then 
    Result := '32-bit' Else Result := '64-bit' 
end; 
+2

在XE2上引入了TOSVersion,問題是關於Delphi 2007。 – bummi 2014-11-16 09:25:49