
Subclassing the procedure belonging to a dialog of another application
: After reading a bit about hooks (Richter, etc.), I am trying to implement
: a program that will subclass the procedure belonging to a dialog of
: another application (on a Win95 or NT system).
:
: Unfortunately, I cannot make it work.
: Can you please help me find what's wrong?
:
: What I'm trying to do is:
:
: In my EXE file (console app):
: ------------------------------
: 1) Start the target application using CreateProcess()
:
: 2) Load a DLL with the hook using LoadLibrary() and
: get the hooking procedure address using GetProcAddress().
:
: 3) Call the hooking procedure with the target app's thread ID as a
: parameter.
:
: 4) Wait for the target app to exit (using WaitForSingleObject())
:
: 5) Remove the hook, free the DLL, cleanup and exit.
:
: No message loop or anything fancy...
:
:
: In my DLL:
: ------------
: 1) In the DllMain() - Save the DLL hInstance.
:
: 2) In the hooking proc - call: hHook = SetWindowsHookEx(WH_GETMESSAGE,
: GetMsgProc, hinstDll, dwThreadId); passing the saved DLL hInstance and
: the target app's thread ID (from the EXE step #3).
:
: [problem #1]
:
: 3) In the Hook - do something like:
:
: if (code == HC_ACTION && wParam == PM_REMOVE &&
: ((MSG*)lParam)->message == WM_INITDIALOG &&
: MyCompareDialog(((MSG*)lParam)->hwnd))
: {
: oldDlgProc = SubclassDialog(((MSG*)lParam)->hwnd, MyDlgProc);
: }
: return CallNextHookEx(hHook, code, wParam, lParam);
:
: [problem #2]
:
: 4) In MyCompareDialog() - get the dialog's title using GetWindowText()
: and compare it to the title of the dialog I wish to subclass.
:
: 5) In MyDlgProc() - do something like:
:
: switch (message)
: {
: case WM_COMMAND:
: switch GET_WM_COMMAND_ID(wParam, lParam)
: {
: case 57670: // A button identifier, found with Spy++
: MessageBox(NULL, "Gotcha!", "Info",
: MB_ICONINFORMATION);
: return TRUE; // Message was processed
: }
: }
: // Call previous dialog procedure
: return CallWindowProc((WNDPROC)oldDlgProc, hWnd, message, wParam,
: lParam);
:
: Now the problems:
:
: Problem #1:
: The call to SetWindowsHookEx() sometimes fails with error 87
: (invalid parameter), but if I put a breakpoint on the line and
: then continue, it seems to work OK. Huh?
Don't trust the de{*filter*}. My de{*filter*} (BC 4.02) gets a bit confused with
these kind of calls. Maybe it's because from here 2 copies of the DLL are
running. Or maybe the de{*filter*} uses SetWindowsHookEx() or related functions.
: Problem #2:
: The hook does not seem to catch WM_INITDIALOG.
: In fact, the first message it gets is WM_MOUSEMOVE (0x200).
How does your "MyCompareDialog(((MSG*)lParam)"->hwnd) work?
I suppose it compares to a value you have saved before. Is this a shared
variable? This code is running in the address space of the process being
subclassed, so all your variables are uninitialized, unless they are in a
shared segment.
Hans