使用 WINAPI ReadConsole

发布于 2024-08-30 23:13:44 字数 681 浏览 4 评论 0原文

我正在尝试使用 WINAPI ReadConsole() 来等待 Win32 控制台应用程序末尾的任何按键。

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 );

该代码执行时不会引发异常。但是,当 ReadConsole() 函数执行时,会标记错误代码 ERROR_INVALID_HANDLE (0x06)。我已验证 hConsole_c 是有效的句柄。有谁知道我做错了什么?我使用的是 Visual C++ 2008 Express 版。谢谢。

I am trying to use the WINAPI ReadConsole() to wait for any keypress at the end of my 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 executes without throwing an exception. However, when the ReadConsole() function executes the error code ERROR_INVALID_HANDLE (0x06) is flagged. I have verified hConsole_c to be a valid handle. Does anyone have any insight as to what I am doing wrongly? I am using Visual C++ 2008 Express Edition. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海风掠过北极光 2024-09-06 23:13:44

对我来说效果很好。我可以让它因 ERROR_INVALID_HANDLE 失败的唯一方法是将 STD_OUTPUT_HANDLE 而不是 STD_INPUT_HANDLE 传递给它。您确定 hConsole_c 是输入句柄吗?

Works fine for me. The only way I could get it to fail with ERROR_INVALID_HANDLE was to pass it the STD_OUTPUT_HANDLE instead of the STD_INPUT_HANDLE. Are you sure hConsole_c is the input handle?

情归归情 2024-09-06 23:13:44

如果您只是想在控制台应用程序结束时等待按键
你为什么不尝试 System("Pause");

If you are just trying to wait for keypress at the end of your console app
why dont you try System("Pause"); ?

眼睛会笑 2024-09-06 23:13:44

您等待击键的方法非常复杂。使用单个 C 函数调用,有多种方法可以实现此目的:

  • getch();(或 ISO C++ 符合名称 _getch),它与平台无关;
  • system("pause");,这是 Windows 特定的。

Your method of waiting for a keystroke is very over-complex. Using single C function calls, there are a couple of ways you can do this:

  • getch(); (or the ISO C++ conformant name, _getch), which is platform independent;
  • system("pause");, which is Windows-specific.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文