新的XE有TOSVersion.ToString 这个函数可以直接显示出完整的操作系统版本号和SP号。还有是多少位的系统。但是有个缺陷就是不能显示是专业版,还是家庭版,还是旗舰版。。查找了一下百度好像没有发现相关的资料。没头绪了!.在Winapi.Windows.pas 文件里发现有 // // Product types // This list grows with each OS release. // // There is no ordering of values to ensure callers // do an equality test i.e. greater-than and less-than // comparisons are not useful. // // NOTE: Values in this list should never be deleted. // When a product-type 'X' gets dropped from a // OS release onwards, the value of 'X' continues // to be used in the mapping table of GetProductInfo. //
楼上大哥提供的这个只能查看版本号,不能详细到专业版和旗舰版区分。 我找到另外一个函数。GetProductInfo 这个好像可以。自己写了一下。但是好像判断还是不准确。 procedure TForm1.Button20Click(Sender: TObject); var rpt: cardinal; begin if GetProductInfo(6, 1, 0, 0, &rpt) then ShowMessage(IntToStr(rpt)); end;
function GetWindowsVersion: string; var OsVerInfo: TOSVersionInfoEx; ExVerExist: Boolean; ProductType: string; begin Result := 'Microsoft Windows'; ExVerExist := True; OsVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx); if not GetVersionEx(POSVersionInfo(@OsVerInfo)^) then begin OsVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo); GetVersionEx(POSVersionInfo(@OsVerInfo)^); ExVerExist := False; end; with OsVerInfo do begin case dwPlatformId of VER_PLATFORM_WIN32s: Result := Result + Format(' %d.%d', [dwMajorVersion, dwMinorVersion]); VER_PLATFORM_WIN32_WINDOWS: { Windows 9x/ME } begin if (dwMajorVersion = 4) and (dwMinorVersion = 0) then begin Result := Result + ' 95'; if szCSDVersion[1] in ['B', 'C'] then Result := Result + ' OSR2'; end else if (dwMajorVersion = 4) and (dwMinorVersion = 10) then begin Result := Result + ' 98'; if (OsVerInfo.szCSDVersion[1] = 'A') then Result := Result + ' Second Edition'; end else if (dwMajorVersion = 4) and (dwMinorVersion = 90) then Result := Result + ' Millenium Edition'; end; VER_PLATFORM_WIN32_NT: { Windows NT/2000 } begin case dwMajorVersion of 3, 4: Result := Result + Format(' NT %d.%d', [dwMajorVersion, dwMinorVersion]); 5: begin if dwMinorVersion = 0 then Result := Result + ' 2000' else if dwMinorVersion = 1 then Result := Result + ' XP' else if dwMinorVersion = 2 then Result := Result + ' 2003 Server'; end; // 6: Windows Server "Longhorn" ,Windows Vista end; if ExVerExist then begin if wProductType = VER_NT_WORKSTATION then begin if dwMajorVersion = 4 then Result := Result + ' Workstation' else if wSuiteMask and VER_SUITE_PERSONAL <> 0 then Result := Result + ' Home Edition' else if dwMajorVersion = 6 then Result := Result + ' Vista' else Result := Result + ' Professional'; end else if wProductType = VER_NT_SERVER then begin if (dwMajorVersion = 5) and (dwMinorVersion = 2) then begin if wSuiteMask and VER_SUITE_DATACENTER <> 0 then Result := Result + ' Datacenter Edition' else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then Result := Result + ' Enterprise Edition' else if wSuiteMask and VER_SUITE_BLADE <> 0 then Result := Result + ' Web Edition' else if wSuiteMask and VER_SUITE_STORAGE_SERVER <> 0 then Result := Result + ' R2' else Result := Result + ' Standard Edition'; end else if (dwMajorVersion = 5) and (dwMinorVersion = 0) then begin if wSuiteMask and VER_SUITE_DATACENTER <> 0 then Result := Result + ' Datacenter Server' else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then Result := Result + ' Advanced Server' else Result := Result + ' Server' end else if (dwMajorVersion = 6) and (dwMinorVersion = 0) then begin if wSuiteMask and VER_SUITE_DATACENTER <> 0 then Result := Result + 'Server "Longhorn" Datacenter Edition' else if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then Result := Result + 'Server "Longhorn" Enterprise Edition' end else begin Result := Result + ' Server'; if wSuiteMask and VER_SUITE_ENTERPRISE <> 0 then Result := Result + ' Enterprise Edition'; end; end; end else begin with TRegistry.Create do begin try RootKey := HKEY_LOCAL_MACHINE; if OpenKey('\SYSTEM\CurrentControlSet\Control\ProductOptions', False) then begin if ValueExists('ProductType') then begin ProductType := ReadString('ProductType'); if SameText(ProductType, 'WinNT') then Result := Result + ' Workstation' else if SameText(ProductType, 'LanManNT') then Result := Result + ' Server' else if SameText(ProductType, 'ServerNT') then Result := Result + ' Advance Server'; end; CloseKey; end; finally Free; end; end; end; Result := Result + ' ' + szCSDVersion; if (dwMajorVersion = 4) and SameText(szCSDVersion, 'Service Pack 6') then begin with TRegistry.Create do begin try RootKey := HKEY_LOCAL_MACHINE; if OpenKey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\Q246009', False) then begin Result := Result + 'a'; CloseKey; end; finally Free; end; end; end; end; end; Result := Format(Result + ' (%d.%d Build %d)', [ dwMajorVersion, dwMinorVersion, dwBuildNumber and $FFFF]); end end;
function GetWindowsVersion: TWindowsVersion; function GetWindowsEdition: TWindowsEdition; function NtProductType: TNtProductType; function GetWindowsVersionString: string; function GetWindowsEditionString: string; function GetWindowsProductString: string; function GetWindowsProductName: string;
引用:GetVersionEx在新版Windows(>=8.1)下已经不准了。 其实也不是说不准,只是后续版本的 Windows 增加了机制,可执行文件需要增加一个资源,申明此可执行文件需要用到的 Windows 版本特性。其实就是一个 XML 文件。类似于如下: <!-- Windows 10 --> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!-- Windows 10 --> <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/> <!-- Windows 8.1 --> <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/> <!-- Windows Vista --> <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> <!-- Windows 7 --> <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/> <!-- Windows 8 --> <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/> </application> </compatibility> 添加了此资源以后,Windows 的相关 API 返回的数据才正确
----------------------------------------------
-
这是 windows 的版本兼容功能的一部分,在初始化应用环境的时候,不需要把较新操作系统的特性全部加载,因为对较为古老的应用来说无用负荷过多,但是对于较新的应用而言,延后加载可能会较大影响用户体验。所以 windows 才使用这个方式来实现兼容,应用自身申明需要使用的操作系统特性和运行的版本环境;对于不采用这种申明方式的应用,windows 可以判定为使用较老 windows 特性的应用程序,所以它较为贴心的为此种应用程序提供较老的 windows 版本号,也从某个方面避免此种应用程序可能发生的崩溃:比如没编写对较高系统版本号的处理代码。这从另一个方面而言也是 windows 的兼容模型的较好体现
----------------------------------------------
-