1
  1. What happens if SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING) is invoked on a Windows version that does not support the mode (i.e. prior to version 10.0.14393)?
  2. How do we differentiate between Windows not supporting ENABLE_VIRTUAL_TERMINAL_PROCESSING versus the mode being supported but SetConsoleMode() failing for a different reason?
Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

1

Answering my own question:

Seeing as the specification does not provide a concrete answer we are forced to use the Windows version to make a decision:

/**
 * @param majorVersion a major version number
 * @param minorVersion a minor version number
 * @param buildVersion a build version number
 * @return true if the Windows version is greater than or equal to the specified number
 */
bool isWindowsVersionOrGreater(WORD majorVersion, WORD minorVersion, WORD buildVersion)
{
    // See http://stackoverflow.com/a/36545162/14731 and https://github.com/DarthTon/Blackbone/blob/master/contrib/VersionHelpers.h#L78
    RTL_OSVERSIONINFOW verInfo = { 0 };
    verInfo.dwOSVersionInfoSize = sizeof(verInfo);

    static auto RtlGetVersion = (RtlGetVersionPtr) GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion");

    if (RtlGetVersion != 0 && RtlGetVersion(&verInfo) == 0)
    {
        if (verInfo.dwMajorVersion > majorVersion)
            return true;
        else if (verInfo.dwMajorVersion < majorVersion)
            return false;

        if (verInfo.dwMinorVersion > minorVersion)
            return true;
        else if (verInfo.dwMinorVersion < minorVersion)
            return false;

        if (verInfo.dwBuildNumber >= buildVersion)
            return true;
    }
    return false;
}

If the Windows version is equal to or greater than 10.0.14393 then ENABLE_VIRTUAL_TERMINAL_PROCESSING is supported.

Gili
  • 86,244
  • 97
  • 390
  • 689