如果SetConsoleMode()不支持ENABLE_VIRTUAL_TERMINAL_PROCESSING会发生什么?

时间:2017-01-17 20:28:39

标签: windows console

  1. 如果在不支持该模式的Windows版本(即版本10.0.14393之前)上调用SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING),会发生什么情况?
  2. 我们如何区分不支持ENABLE_VIRTUAL_TERMINAL_PROCESSING的Windows与支持的模式,但SetConsoleMode()因其他原因而失败?

1 个答案:

答案 0 :(得分:0)

回答我自己的问题:

由于规范没有提供具体答案,我们不得不使用Windows版本做出决定:

/**
 * @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;
}

如果Windows版本等于或大于10.0.14393,则支持ENABLE_VIRTUAL_TERMINAL_PROCESSING