您可以将函数返回的值与WMI 类GetVersionEx
的 Version 属性进行比较。Win32_OperatingSystem
试试这个样本
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
SysUtils,
ActiveX,
ComObj,
Variants;
function WMI_OSVersion:string;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
rgvar : OLEVariant;
LEnum : IEnumVARIANT;
pceltFetched : LongWord;
begin
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
FWbemObjectSet:= FWMIService.ExecQuery('SELECT Version FROM Win32_OperatingSystem','WQL', $00000020);
LEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
if LEnum.Next(1, rgvar, pceltFetched) = S_OK then
Result:=String(rgvar.Version);
end;
function WinApi_OsVersion:string;
var
lpVersionInformation: TOSVersionInfo;
begin
ZeroMemory(@lpVersionInformation, SizeOf(lpVersionInformation));
lpVersionInformation.dwOSVersionInfoSize:=SizeOf(lpVersionInformation);
GetVersionEx(lpVersionInformation);
Result:=Format('%d.%d.%d',[lpVersionInformation.dwMajorVersion, lpVersionInformation.dwMinorVersion, lpVersionInformation.dwBuildNumber]);
end;
function RunningCompatibilityMode : Boolean;
begin
Result:=WMI_OSVersion<>WinApi_OsVersion;
end;
begin
try
CoInitialize(nil);
try
Writeln('Running in Compatibility Mode - '+ BoolToStr(RunningCompatibilityMode, True));
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.