Solved

subclassing procedure

Posted on 1997-04-21
1
328 Views
Last Modified: 2013-12-04
Hello.

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?

Problem #2:
        The hook does not seem to catch WM_INITDIALOG.
        In fact, the first message it gets is WM_MOUSEMOVE (0x200).

0
Comment
Question by:zvior
1 Comment
 
LVL 2

Accepted Solution

by:
sapek earned 160 total points
ID: 1334708
1. Make sure you set DllMain() the entry point for your dll. Otherwise your DllMain() would never be excuted and you wouldn't have rigth hInstance.2. Try using CBT hook instead. This what Microsoft originaly did in ctl3d.dll that subclassed dialog controls to give them 3d look.Adam
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Standards for file storage. 3 25
Run Program using VBScript 3 65
Is COM supported from Apache 1 55
Please explain "Multi-Tenant Services" 5 86
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question