Using WINAPI ReadConsole

I am trying to use WINAPI ReadConsole()

to wait for any key press at the end of a Win32 console application.

CONSOLE_READCONSOLE_CONTROL tControl;
char pStr[65536];
DWORD dwBufLen = 1;
DWORD dwCtl;

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

pBuf[0] = 0x00;

do
{
   ReadConsole( hConsole_c, pStr, (*pBufLen) * sizeof(TCHAR), pBufLen, &tControl );
}
while ( pStr[0] == 0x00 );

      

The code runs without exception. However, when the function ReadConsole()

executes the error code ERROR_INVALID_HANDLE

(0x06) is flagged. I have checked hConsole_c

as a valid handle. Does anyone have any idea what I am doing wrong? I am using Visual C ++ 2008 Express Edition. Thanks.

+2


a source to share


3 answers


Works great for me. The only way I could knock it with ERROR_INVALID_HANDLE was to pass STD_OUTPUT_HANDLE to it instead of STD_INPUT_HANDLE. Are you sure hConsole_c is an input descriptor?



+3


a source


If you're just trying to wait for a key press at the end of a console application why aren't you trying System("Pause");

?



+1


a source


Your method of waiting for a keypress is very complex. Using single C function calls, you can do several ways:

  • getch();

    (or ISO compatible ISO name, _getch

    ), which is platform independent;
  • system("pause");

    which belongs to Windows.
+1


a source







All Articles