2013-03-25 121 views
-1

我試圖確定Windows版本在Delphi XE2使用OSVERSIONINFOEX在How to check in delphi the OS version? Windows 7 or Server 2008 R2?描述的教程中,我已經定義了以下代碼:Windows版本使用OSVERSIONINFOEX

unit ApplicationWizard01; 

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Registry; 

type 
    TMainForm = class(TForm) 
    BitBtn01: TBitBtn; 
    Memo01: TMemo; 
    procedure BitBtn01Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    MainForm: TMainForm; 

_OSVERSIONINFOEX : record 
    dwOSVersionInfoSize : DWORD; 
    dwMajorVersion  : DWORD; 
    dwMinorVersion  : DWORD; 
    dwBuildNumber  : DWORD; 
    dwPlatformId  : DWORD; 
    szCSDVersion  : array[0..127] of AnsiChar; 
    wServicePackMajor : WORD; 
    wServicePackMinor : WORD; 
    wSuiteMask   : WORD; 
    wProductType  : BYTE; 
    wReserved   : BYTE; 
    end; 
    TOSVERSIONINFOEX : _OSVERSIONINFOEX; 

    function GetVersionExA(var lpVersionInformation: TOSVersionInfoEX): BOOL; stdcall; external kernel32; 

const 
    VER_NT_WORKSTATION :Integer = 1; 
    VER_SUITE_ENTERPRISE :Integer = 2; 
    VER_NT_SERVER   :Integer = 3; 
    VER_SUITE_DATACENTER :Integer = 128; 
    VER_SUITE_PERSONAL :Integer = 512; 

const 
    PRODUCT_BUSINESS      = $00000006; {Business Edition} 
    PRODUCT_BUSINESS_N     = $00000010; {Business Edition} 
    PRODUCT_CLUSTER_SERVER    = $00000012; {Cluster Server Edition} 
    PRODUCT_DATACENTER_SERVER    = $00000008; {Server Datacenter Edition (Full Installation)} 
    PRODUCT_DATACENTER_SERVER_CORE  = $0000000C; {Server Datacenter Edition (Core Installation)} 
    PRODUCT_ENTERPRISE     = $00000004; {Enterprise Edition} 
    PRODUCT_ENTERPRISE_N     = $0000001B; {Enterprise Edition} 
    PRODUCT_ENTERPRISE_SERVER    = $0000000A; {Server Enterprise Edition (Full Installation)} 
    PRODUCT_ENTERPRISE_SERVER_CORE  = $0000000E; {Server Enterprise Edition (Core Installation)} 
    PRODUCT_ENTERPRISE_SERVER_IA64  = $0000000F; {Server Enterprise Edition For Itanium Based Systems} 
    PRODUCT_HOME_BASIC     = $00000002; {Home Basic Edition} 
    PRODUCT_HOME_BASIC_N     = $00000005; {Home Basic Edition} 
    PRODUCT_HOME_PREMIUM     = $00000003; {Home Premium Edition} 
    PRODUCT_HOME_PREMIUM_N    = $0000001A; {Home Premium Edition} 
    PRODUCT_HOME_SERVER     = $00000013; {Home Server Edition} 
    PRODUCT_SERVER_FOR_SMALLBUSINESS  = $00000018; {Server For Small Business Edition} 
    PRODUCT_SMALLBUSINESS_SERVER   = $00000009; {Small Business Server} 
    PRODUCT_SMALLBUSINESS_SERVER_PREMIUM = $00000019; {Small Business Server Premium Edition} 
    PRODUCT_STANDARD_SERVER    = $00000007; {Server Standard Edition (Full Installation)} 
    PRODUCT_STANDARD_SERVER_CORE   = $0000000D; {Server Standard Edition (Core Installation)} 
    PRODUCT_STARTER      = $0000000B; {Starter Edition} 
    PRODUCT_STORAGE_ENTERPRISE_SERVER  = $00000017; {Storage Server Enterprise Edition} 
    PRODUCT_STORAGE_EXPRESS_SERVER  = $00000014; {Storage Server Express Edition} 
    PRODUCT_STORAGE_STANDARD_SERVER  = $00000015; {Storage Server Standard Edition} 
    PRODUCT_STORAGE_WORKGROUP_SERVER  = $00000016; {Storage Server Workgroup Edition} 
    PRODUCT_UNDEFINED      = $00000000; {An Unknown Product} 
    PRODUCT_ULTIMATE      = $00000001; {Ultimate Edition} 
    PRODUCT_ULTIMATE_N     = $0000001C; {Ultimate Edition} 
    PRODUCT_WEB_SERVER     = $00000011; {Web Server Edition} 
    PRODUCT_UNLICENSED     = $ABCDABCD; {Unlicensed Product} 




implementation 

{$R *.dfm} 

var 
{$EXTERNALSYM GetProductInfo} 
    GetProductInfo: function (dwOSMajorVersion, dwOSMinorVersion, 
          dwSpMajorVersion, dwSpMinorVersion: DWORD; 
          var pdwReturnedProductType: DWORD): BOOL stdcall = NIL; 

function GetOSInfo: string; 
var 
    NTBres, BRes: Boolean; 
    OSVI: TOSVERSIONINFO; 
    OSVI_NT: TOSVERSIONINFOEX; 
    tmpStr: string; 
    dwOSMajorVersion, dwOSMinorVersion, 
    dwSpMajorVersion, dwSpMinorVersion, 
    pdwReturnedProductType : DWORD; 
begin 
    Result := 'Error'; 
    NTBRes := FALSE; 
    try 
    OSVI_NT.dwOSVersionInfoSize := SizeOf(TOSVERSIONINFOEX); 
    NTBRes := GetVersionExA(OSVI_NT); 
    OSVI.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); 
    BRes := GetVersionEx(OSVI); 
    except 
    OSVI.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); 
    BRes := GetVersionEx(OSVI); 
    end; 
    if (not BRes) and (not NTBres) then 
    Exit; 
    Move(OSVI, OSVI_NT, SizeOf(TOSVersionInfo)); 

    case OSVI_NT.dwPlatformId of 
    VER_PLATFORM_WIN32_NT: 
     begin 
     if OSVI_NT.dwMajorVersion <= 4 then 
      Result := 'Windows NT '; 
     if (OSVI_NT.dwMajorVersion = 5) and (OSVI_NT.dwMinorVersion = 0) then 
      Result := 'Windows 2000 '; 
     if (OSVI_NT.dwMajorVersion = 5) and (OSVI_NT.dwMinorVersion = 1) then 
      Result := 'Windows XP '; 
     if (OSVI_NT.dwMajorVersion = 6) and (OSVI_NT.dwMinorVersion = 0) then 
     begin 
      Result := 'Windows Vista '; 
      if Assigned(GetProductInfo) then 
      begin 
      GetProductInfo(dwOSMajorVersion, dwOSMinorVersion, 
          dwSpMajorVersion, dwSpMinorVersion, 
          pdwReturnedProductType); 
      case pdwReturnedProductType of 
       PRODUCT_BUSINESS: 
       tmpStr := 'Business Edition'; 
       PRODUCT_BUSINESS_N: 
       tmpStr := 'Business Edition'; 
       PRODUCT_CLUSTER_SERVER: 
       tmpStr := 'Cluster Server Edition'; 
       PRODUCT_DATACENTER_SERVER: 
       tmpStr := 'Server Datacenter Edition (full installation)'; 
       PRODUCT_DATACENTER_SERVER_CORE: 
       tmpStr := 'Server Datacenter Edition (core installation)'; 
       PRODUCT_ENTERPRISE: 
       tmpStr := 'Enterprise Edition'; 
       PRODUCT_ENTERPRISE_N: 
       tmpStr := 'Enterprise Edition'; 
       PRODUCT_ENTERPRISE_SERVER: 
       tmpStr := 'Server Enterprise Edition (full installation)'; 
       PRODUCT_ENTERPRISE_SERVER_CORE: 
       tmpStr := 'Server Enterprise Edition (core installation)'; 
       PRODUCT_ENTERPRISE_SERVER_IA64: 
       tmpStr := 'Server Enterprise Edition for Itanium-based Systems'; 
       PRODUCT_HOME_BASIC: 
       tmpStr := 'Home Basic Edition'; 
       PRODUCT_HOME_BASIC_N: 
       tmpStr := 'Home Basic Edition'; 
       PRODUCT_HOME_PREMIUM: 
       tmpStr := 'Home Premium Edition'; 
       PRODUCT_HOME_PREMIUM_N: 
       tmpStr := 'Home Premium Edition'; 
       PRODUCT_HOME_SERVER: 
       tmpStr := 'Home Server Edition'; 
       PRODUCT_SERVER_FOR_SMALLBUSINESS: 
       tmpStr := 'Server for Small Business Edition'; 
       PRODUCT_SMALLBUSINESS_SERVER: 
       tmpStr := 'Small Business Server'; 
       PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: 
       tmpStr := 'Small Business Server Premium Edition'; 
       PRODUCT_STANDARD_SERVER: 
       tmpStr := 'Server Standard Edition (full installation)'; 
       PRODUCT_STANDARD_SERVER_CORE: 
       tmpStr := 'Server Standard Edition (core installation)'; 
       PRODUCT_STARTER: 
       tmpStr := 'Starter Edition'; 
       PRODUCT_STORAGE_ENTERPRISE_SERVER: 
       tmpStr := 'Storage Server Enterprise Edition'; 
       PRODUCT_STORAGE_EXPRESS_SERVER: 
       tmpStr := 'Storage Server Express Edition'; 
       PRODUCT_STORAGE_STANDARD_SERVER: 
       tmpStr := 'Storage Server Standard Edition'; 
       PRODUCT_STORAGE_WORKGROUP_SERVER: 
       tmpStr := 'Storage Server Workgroup Edition'; 
       PRODUCT_UNDEFINED: 
       tmpStr := 'An unknown product'; 
       PRODUCT_ULTIMATE: 
       tmpStr := 'Ultimate Edition'; 
       PRODUCT_ULTIMATE_N: 
       tmpStr := 'Ultimate Edition'; 
       PRODUCT_WEB_SERVER: 
       tmpStr := 'Web Server Edition'; 
       PRODUCT_UNLICENSED: 
       tmpStr := 'Unlicensed product' 
      else 
       tmpStr := ''; 
      end;{ pdwReturnedProductType } 
      Result := Result + tmpStr; 
      NTBRes := FALSE; 
      end;{ GetProductInfo<>NIL } 
     end;{ Vista } 
     if NTBres then 
     begin 
      if OSVI_NT.wProductType = VER_NT_WORKSTATION then 
      begin 
      if OSVI_NT.wProductType = VER_NT_WORKSTATION then 
      begin 
       case OSVI_NT.wSuiteMask of 
       512: Result := Result + 'Personal'; 
       768: Result := Result + 'Home Premium'; 
       else 
       Result := Result + 'Professional'; 
       end; 
      end 
      else if OSVI_NT.wProductType = VER_NT_SERVER then 
      begin 
       if OSVI_NT.wSuiteMask = VER_SUITE_DATACENTER then 
       Result := Result + 'DataCenter Server' 
       else if OSVI_NT.wSuiteMask = VER_SUITE_ENTERPRISE then 
       Result := Result + 'Advanced Server' 
       else 
       Result := Result + 'Server'; 
      end; 
      end{ wProductType=VER_NT_WORKSTATION } 
      else 
      begin 
      with TRegistry.Create do 
       try 
       RootKey := HKEY_LOCAL_MACHINE; 
       if OpenKeyReadOnly('SYSTEM\CurrentControlSet\Control\ProductOptions') then 
        try 
        tmpStr := UpperCase(ReadString('ProductType')); 
        if tmpStr = 'WINNT' then 
         Result := Result + 'Workstation'; 
        if tmpStr = 'SERVERNT' then 
         Result := Result + 'Server'; 
        finally 
        CloseKey; 
        end; 
       finally 
       Free; 
       end; 
      end;{ wProductType<>VER_NT_WORKSTATION } 
      end;{ NTBRes } 
     end;{ VER_PLATFORM_WIN32_NT } 
    VER_PLATFORM_WIN32_WINDOWS: 
     begin 
     if (OSVI.dwMajorVersion = 4) and (OSVI.dwMinorVersion = 0) then 
     begin 
      Result := 'Windows 95 '; 
      if OSVI.szCSDVersion[1] = 'C' then 
      Result := Result + 'OSR2'; 
     end; 
     if (OSVI.dwMajorVersion = 4) and (OSVI.dwMinorVersion = 10) then 
     begin 
      Result := 'Windows 98 '; 
      if OSVI.szCSDVersion[1] = 'A' then 
      Result := Result + 'SE'; 
     end; 
     if (OSVI.dwMajorVersion = 4) and (OSVI.dwMinorVersion = 90) then 
      Result := 'Windows Me'; 
     end;{ VER_PLATFORM_WIN32_WINDOWS } 
    VER_PLATFORM_WIN32s: 
     Result := 'Microsoft Win32s'; 
    else 
    Result := 'Unknown'; 
    end;{ OSVI_NT.dwPlatformId } 
end;{ GetOSInfo } 

procedure TMainForm.BitBtn01Click(Sender: TObject); 
begin 
    // 
end; 

initialization 
    @GetProductInfo := GetProcAddress(GetModuleHandle('KERNEL32.DLL'), 
            'GetProductInfo'); 

end. 

但我得到一個很大的數字錯誤,如下所示:

[DCC Error] ApplicationWizard01.pas(36): E2007 Constant or type identifier expected 
[DCC Error] ApplicationWizard01.pas(38): E2005 'TOSVERSIONINFOEX' is not a type identifier 
[DCC Error] ApplicationWizard01.pas(96): E2007 Constant or type identifier expected 
[DCC Error] ApplicationWizard01.pas(105): E2066 Missing operator or semicolon 
[DCC Error] ApplicationWizard01.pas(117): E2029 'OF' expected but identifier 'dwPlatformId' found 
[DCC Error] ApplicationWizard01.pas(117): E2029 ',' or ':' expected but 'OF' found 
[DCC Error] ApplicationWizard01.pas(120): E2029 'THEN' expected but identifier 'dwMajorVersion' found 
[DCC Error] ApplicationWizard01.pas(122): E2029 ')' expected but identifier 'dwMajorVersion' found 
[DCC Error] ApplicationWizard01.pas(124): E2029 ')' expected but identifier 'dwMajorVersion' found 
[DCC Error] ApplicationWizard01.pas(126): E2029 ')' expected but identifier 'dwMajorVersion' found 
[DCC Error] ApplicationWizard01.pas(204): E2029 'THEN' expected but identifier 'wProductType' found 
[DCC Error] ApplicationWizard01.pas(206): E2029 'THEN' expected but identifier 'wProductType' found 
[DCC Error] ApplicationWizard01.pas(208): E2029 'OF' expected but identifier 'wSuiteMask' found 
[DCC Error] ApplicationWizard01.pas(208): E2029 ',' or ':' expected but 'OF' found 
[DCC Error] ApplicationWizard01.pas(215): E2029 'THEN' expected but identifier 'wProductType' found 
[DCC Error] ApplicationWizard01.pas(217): E2029 'THEN' expected but identifier 'wSuiteMask' found 
[DCC Error] ApplicationWizard01.pas(219): E2029 'THEN' expected but identifier 'wSuiteMask' found 
[DCC Error] ApplicationWizard01.pas(246): E2029 ':=' expected but ':' found 
[DCC Error] ApplicationWizard01.pas(247): E2029 Expression expected but 'BEGIN' found 
[DCC Error] ApplicationWizard01.pas(263): E2029 ':=' expected but ':' found 
[DCC Error] ApplicationWizard01.pas(264): E2010 Incompatible types: 'Integer' and 'string' 
[DCC Error] ApplicationWizard01.pas(270): E2070 Unknown directive: 'TMainForm' 
[DCC Error] ApplicationWizard01.pas(275): E2029 Statement expected but 'INITIALIZATION' found 
[DCC Error] ApplicationWizard01.pas(275): E2029 ';' expected but 'INITIALIZATION' found 
[DCC Error] ApplicationWizard01.pas(13): E2065 Unsatisfied forward or external declaration: 'TMainForm.BitBtn01Click' 
[DCC Fatal Error] ApplicationWizard.dpr(5): F2063 Could not compile used unit 'ApplicationWizard01.pas' 
+0

您在'_OSVERSIONINFOEX'行之前缺少'type'關鍵字。 – TLama 2013-03-25 15:37:37

+0

我已經添加了「類型」,但錯誤是在「_OSVERSIONINFOEX:記錄」行中,作爲「E2029'='期望的','發現'和'szCSDVersion:array [0..127] AnsiChar;」行爲「E2029」=「預期但是」:「找到」。請幫幫我。 – 2013-03-25 15:42:14

+1

啊,我明白了。你只是以某種方式複製了錯誤。無關緊要,你不需要所有這些類型和常量,因爲你已經在'Winapi.Windows.pas'單元中定義了它們。 – TLama 2013-03-25 15:46:16

回答

1

你的問題是你沒有複製@ Ken的答案的代碼。我們試圖調試您製作的抄錄錯誤沒有意義。正確的解決方案是正確使用答案中的代碼。由於@Ken給了你一個完整的單位,就使用它。確保你使用剪貼板。選擇整個單元並將其粘貼到空的編輯器窗口中。然後將其保存到名爲GetWinVersionInfo.pas的文件中。

如果您需要編輯該代碼,那麼我可能會刪除現在可以在Windows單元中找到的所有聲明。它避免了混淆只有這些東西的一個副本。例如,我認爲,類型和常量都可以被刪除。

+0

我想你說的是像「TOSVersion」被翻譯成「System.SysUtils」,「TOSVersionInfoEx」翻譯成「Winapi.Windows」。如果是的話如何實施它。 – 2013-03-25 15:46:37

+0

是的,這是正確的。你能告訴我們你想用這個代碼嗎? – 2013-03-25 15:47:30

+0

我希望此代碼能夠更具體地顯示Windows版本。其次,如果操作系統是Vista Ultimate或Seven Ultimate,只有一些按鈕將被啓用,我的程序將運行。 – 2013-03-25 15:56:33