Is a Program Running in Compatibility Mode

好久不见. 提交于 2019-12-18 13:30:42

问题


Is there a C++ .NET function that I can call that will detect if my program is running in compatibility mode? If there is not one, could someone show me the code for one? Thanks.

For example:

Program loads up Compatibility Mode check if true then exit else run


回答1:


From another forum

After a few google searches went in vain, I decided to experiment myself. I found that the compatibility settings for each executable are stored - as I thought it would be - in the windows registry.

The key where the settings are stored is
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

For each application that has its compatibility settings specified, there exists a value under that key whose name is the path to the executable and the data is a string consisting of the compatibility settings.

The keywords in the string that specify the compatibility settings are: WIN95 WIN98 NT4SP5
WIN2000 256COLOR 640X480
DISABLETHEMES DISABLECICERO

If multiple settings are specified (or are to be specified), the data consists of the settings above separated by a space each. The first four settings are mutually exclusive, i.e. only one of them is to be specified (if at all). I haven't tested the consequences of specifying multiple operating systems.

So, back to addressing your problem. To check if an executable (let's say, "C:\path\executable.exe") is set to run in 256 color mode, there would be a value named "C:\path\executable.exe" (without the quotes, even if the path contains spaces) under the key [HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers], and the data associated with the value would contain the string "256COLOR". If it is also set to run in compatibility mode under Windows 98/ME, the data would be "WIN98 256COLOR".

So, the approach is simple. Test if there is a value with the full path of the executable under the key I mentioned above. If there isn't, the executable has not been specified any compatibility settings. If the value exists, retrieve its data and check for the presence of "256COLOR" in the data. Accordingly, the presence of "WIN95" or "WIN98" or "NT4SP5" or "WIN2000" would mean that the executable is set to run in compatibility mode for that particular operating system.




回答2:


Get the version of the operation system that is returned from GetVersionEx and compare it to the file version on kernel32.dll. When in application compatibility mode GetVersionEx will always return the version of the operating system that is being 'emulated'. If both versions are different then you are in application compatibility mode.




回答3:


The answer above helped me to get a "solution" for the question at hand. It is probably not the most elegant, but seems to work. Obviously you can get a bit more creative on the return type. Booleon does not suffice here. I think a native API would be good.

typedef VOID (NTAPI* TRtlGetNtVersionNumbers)(LPDWORD pdwMajorVersion, LPDWORD pdwMinorVersion, LPDWORD pdwBuildNumber);

bool IsRunningCompatMode()
{
    TRtlGetNtVersionNumbers RtlGetNtVersionNumbers = (TRtlGetNtVersionNumbers)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlGetNtVersionNumbers");

    assert(RtlGetNtVersionNumbers);

    if(RtlGetNtVersionNumbers)
    {
        OSVERSIONINFO osInfo = {0};
        osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        GetVersionEx(&osInfo);

        DWORD dwMajorVersion;
        DWORD dwMinorVersion;
        DWORD dwBuildNumber;

        RtlGetNtVersionNumbers(&dwMajorVersion, &dwMinorVersion, &dwBuildNumber);

        dwBuildNumber &= 0x0000FFFF;

        if(osInfo.dwBuildNumber != dwBuildNumber)
        {
            return true;
        }
    }
    return false;
};


来源:https://stackoverflow.com/questions/3444997/is-a-program-running-in-compatibility-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!