#5 Difficult workaround for the user

open
nobody
None
5
2004-11-27
2004-11-27
Steven Weiss
No

Hi John,
few days ago i told you that i wanted to subclass the
menu's window. after some tries i realized that i have to
create a hook which monitors the creation of the window
for a correct subclassing. my first plan was to subclass
the window in response to a WM_ENTERMENULOOP,
WM_INITMENU or some other menu-related message but
that either wasn't possible (window has not been created
yet) or it turned out to be erroneous.

so i took the step with the hook. i created a CBT-hook
and wanted to remap the window in this hook ->

static LRESULT CALLBACK CBTHook(int nCode, WPARAM
wParam, LPARAM lParam)
{
HWND hWnd = 0;
if ( nCode == HCBT_CREATEWND) {
hWnd = reinterpret_cast<HWND>(wParam);
if (is_menu_hwnd(hWnd))
mapping::remap_wnd<menu_wnd> (hWnd);
}
return CallNextHookEx( g_cbt_hook_, nCode, wParam,
lParam);
}

but remap_wnd() throwed an exception. the location was
in get_window_anythread(). i analyzed your code and
came to the conclusion that you also have a cbt-hook
but the windows don't get subclassed immediately but
only after a message. so here's my workaround:

static LRESULT CALLBACK msg_window_hook(int nCode,
WPARAM wParam, LPARAM lParam)
{
static const int wm_register_internal =
get_wm_register_internal();

MSG * msg = reinterpret_cast<MSG*>(lParam);
if ( msg->message == wm_register_internal) {
HWND hWnd = reinterpret_cast<HWND>(msg-
>lParam);
if (is_menu_hwnd(hWnd)) {
bool create = msg->wParam != 0;
if (create)
mapping::remap_wnd<menu_wnd> (hWnd);
}
}
return CallNextHookEx(g_msg_hook_, nCode, wParam,
lParam);
}

i copied the get_wm_register_internal() function from
your code. my hook procedure is a WH_GETMESSAGE-
hook (like yours) and to make sure my hook procedure is
called AFTER win32gui's i have a static object so this
object will be created at program initialization and your
SetWindowsHookEx() will be called after mine -> correct
Hook-Chaining-Order.

you commented that this is necessary for correct
subclassing. but you could create a function which takes
the steps for correct hooking - or you mention the
correct procedure in the docs. it took some time for me
to solve the problem and i don't think unexperienced
programmers will find the solution by themselves

greetz steven

Discussion

  • John Torjo
    John Torjo
    2004-11-30

    Logged In: YES
    user_id=1031729

    Yup - I will deal with this issue - as it's been found by
    other users as well. Hopefully, it'll make it into 1.6.2
    (which should be available next week - that is , beginning
    of Dec)

    Best,
    John