Note that there are some explanatory texts on larger screens.

plurals
  1. POWin32 Message Handler Error Propagation
    text
    copied!<p>I'm writing a (C++) application that utilizes a single dialog box. After setting up a message pump and handler I started wondering how I would go about propagating C++ exceptions to my original code (i.e., the code that calls <code>CreateDialogParam</code>, for instance).</p> <p>Here's a skeleton example of what I mean:</p> <pre><code>BOOL CALLBACK DialogProc(HWND, UINT msg, WPARAM, LPARAM) { if(msg == WM_INITDIALOG) //Or some other message { /* Load some critical resource(s) here. For instnace: const HANDLE someResource = LoadImage(...); if(someResource == NULL) { ---&gt; throw std::runtime_error("Exception 1"); &lt;--- The exception handler in WinMain will never see this! Maybe PostMessage(MY_CUSTOM_ERROR_MSG)? } */ return TRUE; } return FALSE; } //====================== void RunApp() { const HWND dlg = CreateDialog(...); //Using DialogProc if(dlg == NULL) { throw std::runtime_error("Exception 2"); //Ok, WinMain will see this. } MSG msg = {}; BOOL result = 0; while((result = GetMessage(&amp;msg, ...)) != 0) { if(result == -1) { throw std::runtime_error("Exception 3"); //Ok, WinMain will see this. } //Maybe check msg.message == MY_CUSTOM_ERROR_MSG and throw from here? TranslateMessage(&amp;msg); DispatchMessage(&amp;msg); } } //====================== int WINAPI WinMain(...) { try { RunApp(); //Some other init routines go here as well. } catch(const std::exception&amp; e) { //log the error return 1; } catch(...) { //log the error return 1; } return 0; } </code></pre> <p>As you can see, <code>WinMain</code> will handle "Exception 2" and "3", but <strong>not</strong> "Exception 1".</p> <p><strong>My fundemental question is simple; what would be an elegant way to propagate these sorts of errors to the original "calling" code?</strong> </p> <p>I thought of maybe using custom messages and moving the actual <code>throw</code>-statements out to the message pump (in <code>RunApp()</code>), but I'm not sure how that would work yet as I have relatively little experience with Windows in general.</p> <p>Perhaps I'm looking at this situation all wrong. How do <em>you</em> usually bail out when something fatal (i.e., an acquisition of a critical resource fails, and there's no chance for recovery) when you're in the message handler?</p>
 

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