如何检测我的应用程序是否在兼容模式下运行? [英] How I can detect if my application is running under compatibility mode?

查看:315
本文介绍了如何检测我的应用程序是否在兼容模式下运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何检测真实的Windows版本

我有一个使用第三方库(dll-无源代码)保护(加密)某些客户数据的应用程序,此dll必须根据当前Windows版本使用不同的参数进行初始化。如果我的应用在Windows 7下以XP兼容模式执行,则dll加密方法将失败。因此,我需要一种方法来检测我的应用何时在兼容模式下运行,以防止出现此问题。因此如何检测我的应用程序是否在兼容模式下运行?

I have an application which uses a third-party library(dll - no source code) which protect (encrypt) some customer data, this dll must be initializated using different params depending of the current Windows Version. If my app is executed in XP compatibility mode under Windows 7, the dll encrypt method fails. So i need a way to detect when My App is running under compatibility mode to prevent this issue. So How I can detect if my application is running under compatibility mode?

推荐答案

您可以比较 GetVersionEx 函数对 Win32_OperatingSystem WMI类。

You can compare the value returned by the GetVersionEx function against the Version property of the Win32_OperatingSystem WMI class.

尝试此示例

{$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.

这篇关于如何检测我的应用程序是否在兼容模式下运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆