I am working on writing a simple game engine, and I am having problems processing events in the Windows console; in particular, I cannot figure out how to pass user data to the callback handler.
First I 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 the close button in the console, a MessageBox appears. The only problem is that I need to call code that flushes the log buffer into the log file in this type of shutdown (as well as another cleanup), and the Logger instance is a member of my WindowsSystemManager.
I ran into a similar problem of passing user data to window descriptors using SetWindowLongPtr and GetWindowLongPtr successfully, but I cannot find any information on how to do this with console handlers. Any thoughts?
EDIT: I got this functionality based on MSalters suggestions. Final code for the console handler:
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);