The MASM Forum

General => The Workshop => Windows API => Topic started by: kkurkiewicz on November 09, 2024, 04:06:55 AM

Title: Switching console modes
Post by: kkurkiewicz on November 09, 2024, 04:06:55 AM
I just came across the following code in the Microsoft's Windows-classic-samples (https://github.com/microsoft/Windows-classic-samples) GitHub repository:

//
// Only set the console mode if input has not been redirected.
//

if (Redirected  == FALSE) {
    GetConsoleMode(InputConsole, &PreviousMode);
    Result = SetConsoleMode(InputConsole, ENABLE_PROCESSED_INPUT);
    if (Result == FALSE) {
        goto GetPasswordEnd;
    }

     ConsoleModeSet = TRUE;
}
Windows-classic-samples/Samples/Win7Samples/winbase/WHEA/DumpRec/exe/dumprec.c

Given that ENABLE_PROCESSED_INPUT is only a bit flag, why is it not being ORed with PreviousMode similarly to how the variable dwNewMode is initialized in this (https://devblogs.microsoft.com/oldnewthing/20130506-00/?p=4453) article on processing mouse events? Can ENABLE_PROCESSED_INPUT really act as a standalone mode?
Title: Re: Switching console modes
Post by: kkurkiewicz on November 09, 2024, 04:13:03 AM
Also, is there any sort of Windows Console best practices guide?
Title: Re: Switching console modes
Post by: zedd151 on November 09, 2024, 04:23:34 AM
Maybe this will offer some help
https://masm32.com/board/index.php?topic=9738.0 (https://masm32.com/board/index.php?topic=9738.0)
Seems related...
Title: Re: Switching console modes
Post by: NoCforMe on November 09, 2024, 05:34:08 AM
Quote from: kkurkiewicz on November 09, 2024, 04:06:55 AMGiven that ENABLE_PROCESSED_INPUT is only a bit flag, why is it not being ORed with PreviousMode similarly to how the variable dwNewMode is initialized in this (https://devblogs.microsoft.com/oldnewthing/20130506-00/?p=4453) article on processing mouse events? Can ENABLE_PROCESSED_INPUT really act as a standalone mode?

The thing to do is to go to the source, in this case the documentation on the SetConsoleMode() function (https://learn.microsoft.com/en-us/windows/console/setconsolemode?redirectedfrom=MSDN) in the Microsoft Learn repository:

QuotedwMode [in]:
The input or output mode to be set.

If the hConsoleHandle parameter is an input handle, the mode can be one or more of the following values. When a console is created, all input modes except ENABLE_WINDOW_INPUT and ENABLE_VIRTUAL_TERMINAL_INPUT are enabled by default.

ENABLE_PROCESSED_INPUT (0x0001):
CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system.

So it looks like it's OK to use this flag by itself. Depends, of course, on what you're trying to do here.
Title: Re: Switching console modes
Post by: kkurkiewicz on November 09, 2024, 08:05:39 AM
Hmm... the snippet comes from a function named DrGetPassword, so I think the only reason for calling SetConsoleMode here is to disable echoing.
Title: Re: Switching console modes
Post by: NoCforMe on November 09, 2024, 08:49:02 AM
That would make total sense.