我有一个应用程序,它使用第三方库(dll - 没有源代码)来保护(加密)某些客户数据,这个dll必须使用不同的params初始化,具体取决于当前的Windows版本。如果我的应用程序在Windows 7下以XP兼容模式执行,则dll加密方法将失败。所以我需要一种方法来检测我的应用程序何时在兼容模式下运行以防止出现此问题。所以How I can detect if my application is running under compatibility mode?
答案 0 :(得分:6)
您可以将GetVersionEx
函数返回的值与Win32_OperatingSystem
WMI类的Version属性进行比较。
试试这个样本
{$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.