将数据传递到 Windows 控制台控制处理程序
我正在编写一个简单的游戏引擎,但在处理 Windows 控制台事件时遇到问题;具体来说,我不知道如何将自定义数据传递给回调处理程序。
我首先调用此代码来指定我的回调函数:
SetConsoleCtrlHandler((PHANDLER_ROUTINE)WindowsSystemManager::ConsoleControlHandler, true);
我的静态成员回调函数定义为:
bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){
if(controlType == CTRL_CLOSE_EVENT){
MessageBox(NULL, L"Close Event Captured", L"Close Event Captured", NULL);
}
return true;
}
一切正常 - 当我单击控制台中的关闭按钮时,会弹出此消息框。唯一的问题是,我需要在这种类型的关闭(以及其他清理)中调用将日志记录缓冲区刷新到日志文件的代码,并且 Logger 实例是我的 WindowsSystemManager 中的成员。
我已经成功地使用 SetWindowLongPtr 和 GetWindowLongPtr 处理了将自定义数据传递到窗口句柄的类似问题,但我找不到有关如何使用控制台控制处理程序执行此类操作的任何信息。有什么想法吗?
编辑:我根据 MSalters 的建议使此功能正常工作。控制台控制处理程序的最终代码如下:
bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){
BerserkEngine* engine = (BerserkEngine*)GetWindowLongPtr(GetConsoleWindow(), GWLP_USERDATA);
if(controlType == CTRL_CLOSE_EVENT){
engine->~BerserkEngine();
PostQuitMessage(0);
}
return true;
}
我在 WindowsSystemManager 构造函数中设置此自定义数据指针的位置:
SetWindowLongPtr(GetConsoleWindow(), GWL_USERDATA, (LONG_PTR)this->engine);
I am working on writing a simple game engine and I am having trouble handling Windows console events; specifically, I cannot figure out how to pass custom data to the callback handler.
I first call this code to specify my callback function:
SetConsoleCtrlHandler((PHANDLER_ROUTINE)WindowsSystemManager::ConsoleControlHandler, true);
My static-member callback function is defined as:
bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){
if(controlType == CTRL_CLOSE_EVENT){
MessageBox(NULL, L"Close Event Captured", L"Close Event Captured", NULL);
}
return true;
}
Everything works fine - when I click on the close button in the console, this MessageBox pops up. Only problem is, I need to call code that flushes a logging buffer to a log file on this type of shutdown (as well as other clean-up), and the Logger instance is a member in my WindowsSystemManager.
I have dealt with a similar problem of passing custom data to window handles by using SetWindowLongPtr and GetWindowLongPtr successfully, but I can't find any information on how to do this type of thing with console control handlers. Any thoughts?
EDIT: I got this functionality working based on MSalters' suggestions. The final code for the console control handler is here:
bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){
BerserkEngine* engine = (BerserkEngine*)GetWindowLongPtr(GetConsoleWindow(), GWLP_USERDATA);
if(controlType == CTRL_CLOSE_EVENT){
engine->~BerserkEngine();
PostQuitMessage(0);
}
return true;
}
Where I set this custom data pointer in the WindowsSystemManager constructor:
SetWindowLongPtr(GetConsoleWindow(), GWL_USERDATA, (LONG_PTR)this->engine);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你为什么需要这个。您可以有多个窗口,但只能有一个控制台。
但是,
GetConsoleWindow
将为您提供控制台HWND
,您可以在其中调用SetWindowLongPtr
。不是很干净(你不应该在你不管理的窗口上执行此操作),但它可能会起作用。I'm not sure why you'd need this. You can have multiple windows, but only one console.
However,
GetConsoleWindow
will give you the consoleHWND
, on which you might callSetWindowLongPtr
. Not very clean (you're not supposed to do this on windows that you don't manage), but it might just work.