Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Detecting Windows Compatability Mode from within my application


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Feb 25, 2016 7:04 AM Last Post By: Nathan Webers
Nathan Webers

Posts: 15
Registered: 8/20/06
Detecting Windows Compatability Mode from within my application  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2016 1:32 PM
I have a need to detect if my application is being run under Windows Compatibility Mode. So far, I have found some code over at stackoverflow that might do the trick.

The website is here:
[http://stackoverflow.com/questions/10744651/how-i-can-detect-if-my-application-is-running-under-compatibility-mode]

Does anyone know of a better way to do this, if there is any?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Detecting Windows Compatability Mode from within my application  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2016 2:01 PM   in response to: Nathan Webers in response to: Nathan Webers
Nathan wrote:

I have a need to detect if my application is being run under Windows
Compatibility Mode.

Why? Maybe there is an alternative solution for whatever it is your are
trying to solve.

The whole point of Compatibilty Mode is to hide the real OS from the app
so it thinks it is running in a different OS, and even to make certain APIs
and stubs act differently depending on which OS is being simulated. Your
app should respect that.

So far, I have found some code over at stackoverflow that might do the
trick.

That question was really asking how to detect the true OS version, not detect
Compatibility Mode itself.

Does anyone know of a better way to do this, if there is any?

AFAIK, there isn't any other way. Detecting when the true OS version is
different than the reported OS version is a pretty good indication that Compatibility
Mode may be in effect (in Windows 8.1+, Manifestation also affects it).
There are several different ways to detect the true OS version when Compatibility
Mode (and Manifestation) is being used, the StackOverflow answer only touches
on one of them.

--
Remy Lebeau (TeamB)
Nathan Webers

Posts: 15
Registered: 8/20/06
Re: Detecting Windows Compatability Mode from within my application  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 25, 2016 6:56 AM   in response to: Nathan Webers in response to: Nathan Webers
Nathan Webers wrote:
I have a need to detect if my application is being run under Windows Compatibility Mode. So far, I have found some code over at stackoverflow that might do the trick.

The website is here:
[http://stackoverflow.com/questions/10744651/how-i-can-detect-if-my-application-is-running-under-compatibility-mode]

Does anyone know of a better way to do this, if there is any?

I have essentially answered my own question. Here is a function that will detect what OS the app thinks it's running in and compare that with the actual OS information. The two strings that consist of the major build number and minor build number are then compared.

////////////////////////////////////////////////////////////////////////////////
function AppIsRunningInCompatibilityMode(): boolean;
{Determines if the application is set to run in a compatability mode by
 comparing what it thinks it is running in to what the operating system version
 actually is.}
var
  FSWbemLocator, FWMIService, FWbemObjectSet, rgvar : OLEVariant;
  LEnum : IEnumVARIANT;
  pceltFetched : LongWord;
  strRunningAsOSResult, strActualOSResult : String;
  tempStr : string;
  index, intStrLimit : integer;
begin
  try
  {Get actual OS information.}
  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
  begin
    tempStr := String(rgvar.Version);
    intStrLimit := Posex('.', tempStr, Pos('.', tempStr))+1;
    for index := 1 to intStrLimit do
      strRunningAsOSResult := strRunningAsOSResult + tempStr[index];
  end;
 
  {Get the OS information that the app thinks it is running in.}
  strActualOSResult :=
    Format('%d.%d',[TOSVersion.Major, TOSVersion.Minor]);
 
  {Compare the two strings (consists of major build number and minor build number
   divided by a '.').  Return 'true' if they are different to indicate
   that the application is running in compatability mode. 'False' if the strings
   are the same (means we're running in the mode of the actual OS).}
  Result := strRunningAsOSResult <> strActualOSResult;
  except
    Result := False;
  end;
end;


Edited by: Nathan Webers on Feb 25, 2016 7:03 AM (added {code} flags :) )
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02