1

I am trying to disable quick edit mode in the console with winapi. The documentation says:

To enable this mode, use ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS. To disable this mode, use ENABLE_EXTENDED_FLAGS without this flag.

I expected this code to work:

#include <Windows.h>
#include <iostream>

int main() {
    HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(console);

    if (!SetConsoleMode(console, ENABLE_EXTENDED_FLAGS))
        std::cout << GetLastError() << std::endl;

    for (;;);

    return 0;
}

But I can still select in the console and it prints 87 which is ERROR_INVALID_PARAMETER.

I also tried this answer but I got the same results.

I am using clang version 10.0.0

Joyal Mathew
  • 614
  • 7
  • 24
  • 1
    `ENABLE_QUICK_EDIT_MODE` is a valid flag "*if the hConsoleHandle parameter is an input handle*" but your `console` is a console screen buffer, instead. See the first paragraph under [SetConsoleMode parameters](https://learn.microsoft.com/en-us/windows/console/setconsolemode#parameters). – dxiv Nov 03 '20 at 21:57
  • 1
    @dxiv you're right I had to use `SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_EXTENDED_FLAGS)`. If you post an answer I can accept it. – Joyal Mathew Nov 03 '20 at 22:04

1 Answers1

3

The first argument to SetConsoleMode is a handle to either "the console input buffer or a console screen buffer" per the linked docs.

ENABLE_QUICK_EDIT_MODE is part of the set of flags which apply to an input handle, only, but the console handle in the posted code is a console screen buffer, instead.

To set or change the quick edit mode, the call must use an input buffer handle, which is usually obtained with GetStdHandle(STD_INPUT_HANDLE), or CreateFile with the reserved name "CONIN$" which works even in case the standard input was redirected.

dxiv
  • 16,984
  • 2
  • 27
  • 49