2

可能重复:
如何检测真正的 Windows 版本

我有一个应用程序使用第三方库(dll - 无源代码)来保护(加密)一些客户数据,这个 dll 必须使用不同的参数初始化,具体取决于当前的 Windows 版本。如果我的应用程序在 Windows 7 下以 XP 兼容模式执行,则 dll 加密方法失败。所以我需要一种方法来检测我的应用程序何时在兼容模式下运行以防止此问题。所以How I can detect if my application is running under compatibility mode?

4

1 回答 1

7

您可以将函数返回的值与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.
于 2012-05-24T20:21:51.993 回答