Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Data to Windows Console Control Handler
    primarykey
    data
    text
    <p>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.</p> <p>I first call this code to specify my callback function:</p> <pre class="lang-c prettyprint-override"><code>SetConsoleCtrlHandler((PHANDLER_ROUTINE)WindowsSystemManager::ConsoleControlHandler, true); </code></pre> <p>My static-member callback function is defined as:</p> <pre class="lang-c prettyprint-override"><code>bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){ if(controlType == CTRL_CLOSE_EVENT){ MessageBox(NULL, L"Close Event Captured", L"Close Event Captured", NULL); } return true; } </code></pre> <p>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.</p> <p>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?</p> <p>EDIT: I got this functionality working based on MSalters' suggestions. The final code for the console control handler is here:</p> <pre class="lang-c prettyprint-override"><code>bool WINAPI WindowsSystemManager::ConsoleControlHandler(DWORD controlType){ BerserkEngine* engine = (BerserkEngine*)GetWindowLongPtr(GetConsoleWindow(), GWLP_USERDATA); if(controlType == CTRL_CLOSE_EVENT){ engine-&gt;~BerserkEngine(); PostQuitMessage(0); } return true; } </code></pre> <p>Where I set this custom data pointer in the WindowsSystemManager constructor:</p> <pre class="lang-c prettyprint-override"><code>SetWindowLongPtr(GetConsoleWindow(), GWL_USERDATA, (LONG_PTR)this-&gt;engine); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload