Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see you call <code>PostMessage</code> from <code>Callback</code> then actively wait using a loop and <code>Sleep</code>. Replace <code>PostMessage</code> by <a href="http://msdn.microsoft.com/en-us/library/ms644950(VS.85).aspx" rel="nofollow noreferrer"><code>SendMessage</code></a>, which will wait for the message processing to complete. That way the processor-intensive loop is gone. Moreover, you can use the return code of the <code>SendMessage</code> call (from the <code>WM_CALLBACK</code> arm of <code>HookProc</code>) to pass error information. Either use simple numeric codes (e.g. non-zero means error) or pass exception objects (in this case you'll have to allocate then on the heap from your exception handler, you can't pass the parameter directly as it is stack-allocated).</p> <p>Last, using the synchronous <code>SendMessage</code> instead of the async <code>PostMessage</code> means you no longer have to build an intermediary <code>callH</code> function object from the <code>HookProc</code>, as you can pass the stack allocated <code>call</code> parameter instead. This results in much simpler code.</p> <p>For example:</p> <pre><code>void Callback(HWND hwnd, boost::function&lt;void(HWND,LPARAM)&gt; call, LPARAM lParam) { //send a message with a pointer to our function object in the WPARAM if (SendMessage(hwnd, WM_CALLBACK, &amp;call, lParam)) { // error handling. } } LRESULT CALLBACK HookProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { //check for our custom message if(msg == WM_CALLBACK) { //retreive the function pointer from the WPARAM boost::function&lt;void(HWND,LPARAM)&gt; *callH = (boost::function&lt;void(HWND,LPARAM)&gt;*)wParam; //call it, catching exceptions in the process. try { (*callH)(hwnd,lParam); return 0; } catch (...) { return 1; } } else //if there was nothing relevant to us, call the old message procedure return CallWindowProc(hooked[hwnd], hwnd, msg, wParam, lParam); } //std::map&lt;HWND, WNDPROC&gt; hooked; </code></pre> <p>And:</p> <pre><code>Graphics::Graphics(IWindow *_window, Size2&lt;unsigned&gt; _size) :lost(false), reset(false), refCnt(0), backCol(0xFF000000), started(false), exited(false), window(_window), size(_size) { window-&gt;AddRef(); HWND hwnd = *((HWND*)window-&gt;GetHandle()); CallbackHook(hwnd); Callback(hwnd, boost::bind(&amp;Graphics::create, this), 0); // No need to wait, as SendMessage is synchronous. } void Graphics::create() { ...code that may throw various exceptions started = true; } </code></pre>
 

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