Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this window subclassing code crash?
    text
    copied!<p>I am trying to subclass the window that currently has focus. I do this by monitoring for <code>HCBT_ACTIVATE</code> events using a CBT hook, and set and unset the <code>WndProc</code> of the focused and previously focused windows.</p> <p><strong>The problem is that it only works whenever I have a breakpoint set somewhere in the code</strong>. </p> <p>If there is no breakpoint, once my application exits, all the windows that I have subclassed crashes in order, even though I have removed the subclassing and restored the original WndProc.</p> <p>I have verified that <code>Unsubclass()</code> is called whenever my application shuts down.</p> <pre><code>// code extracts HINSTANCE hInst; HHOOK hHook; #pragma data_seg(".shared") HWND hWndSubclass = 0; FARPROC lpfnOldWndProc = NULL; #pragma data_seg() #pragma comment(linker, "/section:.shared,rws") void Unsubclass() { // if the window still exists if (hWndSubclass != 0 &amp;&amp; IsWindow(hWndSubclass)) { SetWindowLongPtr(hWndSubclass, GWLP_WNDPROC, (LPARAM)lpfnOldWndProc); hWndSubclass = 0; } } static LRESULT CALLBACK SubClassFunc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_MOVING) { // this is just test code so I can see it works (it does) RECT* r = (RECT*)lParam; r-&gt;right = r-&gt;left + 500; r-&gt;bottom = r-&gt;top + 500; return TRUE; } else if (message == WM_DESTROY) { Unsubclass(); } return CallWindowProc((WNDPROC)lpfnOldWndProc, hWndSubclass, message, wParam, lParam); } void SubclassWindow(HWND hWnd) { // remove the subclassing for the old window Unsubclass(); // subclass the new window lpfnOldWndProc = (FARPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LPARAM)SubClassFunc); hWndSubclass = hWnd; } static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HCBT_ACTIVATE) { SubclassWindow((HWND)wParam); } return 0; } // ... code that initializes the CBT proc __declspec(dllexport) BOOL Setup() { hHook = SetWindowsHookEx(WH_CBT, CBTProc, hInst, 0); } __declspec(dllexport) BOOL Teardown() { UnhookWindowsHookEx(hHook); Unsubclass(); } BOOL APIENTRY DllMain( HINSTANCE hInstance, DWORD Reason, LPVOID Reserved ) { switch(Reason) { case DLL_PROCESS_ATTACH: hInst = hInstance; return TRUE; case DLL_PROCESS_DETACH: Unsubclass(); return TRUE; } return 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