Win api readConsole ()

I am trying to use the Win API ReadConsole(...), and I want to pass a char delimiter to stop console input. The code below works, but it just stops reading input on \r\n. I would like for him to stop reading console input on '.', for example.

void read(char *cIn, char delim)
{
    HANDLE hFile;
    DWORD charsRead;
    DWORD charsToRead = MAX_PATH;
    CONSOLE_READCONSOLE_CONTROL cReadControl;

    cReadControl.nLength = sizeof(CONSOLE_READCONSOLE_CONTROL);
    cReadControl.nInitialChars = 0;
    cReadControl.dwCtrlWakeupMask = delim;
    cReadControl.dwControlKeyState = NULL;

    DWORD lpMode;



//    char cIn[MAX_PATH];    //-- buffer to hold data from the console

    hFile = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
                    FILE_SHARE_WRITE | FILE_SHARE_READ, NULL,
                    OPEN_EXISTING, 0, NULL);

    GetConsoleMode(hFile,&lpMode);
//    lpMode &= ~ENABLE_LINE_INPUT;   //-- turns off this flag
//    SetConsoleMode(hFile, lpMode);  //-- set the mode with the new flag off

    bool read = ReadConsole(hFile, cIn, charsToRead * sizeof(TCHAR), &charsRead, &cReadControl);
    cIn[charsRead - 2] = '\0';
}

I know that there are other simple ways to do this, but I'm just trying to understand some of the win api functions and how to use them.

Thank.

+4
source share
2 answers

I saw this question and suggested that it would be trivial, but for the last 30 minutes I tried to figure it out and finally do something.

, dwCtrlWakeupMask CONSOLE_READCONSOLE_CONTROL. MSDN : " , ", mask? ULONG TCHAR - ? wchars, , .

: https://groups.google.com/forum/#!topic/golang-codereviews/KSp37ITmcUg Go, , : 1 << '\t'. , !

, dwCtrlWakeupMask - ASCII, ReadConsole. | 1 << ctrl_char, ... , 32- , 1-31 () ( btw , , , , , , ).

, :

cReadControl.dwCtrlWakeupMask = (1 << '\t') | (1 << 0x08);

ReadConsole (\t) backspace (0x08).

, ctrl+ some_ascii_value, , a == 1. , ctrl+d is 4, ctrl+z - 26.

, , ctrl+d ctrl+z:

cReadControl.dwCtrlWakeupMask = (1 << 4) | (1 << 26);

, Linux read, ctrl+d, .

, , ; , . .... tbh, - ReadConsoleInput , .

, - . , >= 32, ... , , .

, wineconsole, , Windows, .

dwControlKeyState BY. ( , ), , . , , ReadConsole 32, numlock . 48, numlock , shift + tab ( numlock). .

MSDN, IMO , !

+6

. , , . ReadFile , , .

, , ReadConsole ReadFile , .

char *cInptr = cIn;
do {
    bool read = ReadConsole(hFile, cInptr, sizeof(TCHAR), &charsRead, &cReadControl);
    if (read) cInptr += charsRead;
} while (read && charsRead > 0 && cInptr[-1] && cInptr[-1] != '.');

- . , ReadConsole.

+1

Source: https://habr.com/ru/post/1676609/


All Articles