Click here to Skip to main content
15,889,773 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionmutex problem Pin
VC_RYK4-Aug-07 5:56
VC_RYK4-Aug-07 5:56 
AnswerRe: mutex problem Pin
Mark Salsbery4-Aug-07 6:45
Mark Salsbery4-Aug-07 6:45 
GeneralRe: mutex problem Pin
VC_RYK4-Aug-07 7:12
VC_RYK4-Aug-07 7:12 
GeneralRe: mutex problem Pin
Mark Salsbery4-Aug-07 8:32
Mark Salsbery4-Aug-07 8:32 
GeneralRe: mutex problem Pin
JudyL_MD6-Aug-07 2:31
JudyL_MD6-Aug-07 2:31 
QuestionSubclassing and hooking the Microsoft Office 2000/2003/2007 File Open / Save Dialog boxes using Win32 C code Pin
JuggernautMsn4-Aug-07 2:23
JuggernautMsn4-Aug-07 2:23 
AnswerRe: Subclassing and hooking the Microsoft Office 2000/2003/2007 File Open / Save Dialog boxes using Win32 C code Pin
Mark Salsbery4-Aug-07 6:48
Mark Salsbery4-Aug-07 6:48 
AnswerRe: Subclassing and hooking the Microsoft Office 2000/2003/2007 File Open / Save Dialog boxes using Win32 C code Pin
Force Code4-Aug-07 9:28
Force Code4-Aug-07 9:28 
Here are the basic steps as I understand them to "hook" a dialog in some application. I personally never use actual Windows Hooks, as from my limited experience, they are slow, with a lot of overhead, and complicated to discourage their use.

The three basic things needed are:
1) DLL injection
2) Windows Subclassing
3) Hooking the application import table.

The last item goes by different names and does not involve actual Windows Hooks.

Maybe you know some of this already:

First You need to inject your code into the process address space of the target application. This means putting it in a DLL and then calling some function InjectDLL(targ_hwnd,"mydll.dll"). There are numerous downloadable libaries for DLL injection. Do a search on CodeProject.

Then, within your DLLMain, presumably you'll do a search for the main window of the target application, using FindWindow, or whatever. At that point, you subclass that window with your own WndProc. You do this by calling SetWindowLong with the appropriate parameters. You also need to save the previous wndproc it returns so you can call it from your own. Your new wndproc is a message handler, looking at messages before the existing WndProc recieves them.

So your task is to hook into some dialog of this application. Presumably this dialog is activated from some menu item are tool bar button in the main window. Use SpyXX to find out the message being sent to the Main Window when this toolbar button or menu item is selected. Then you will intercept that message in your new WndProc. When you receive that message all you will do is set some Boolean, say bFindFileDlg to TRUE.

That brings us to the next step - hooking the application import table. If you can figure out how to get the Window Handle of the dialog through some message sent to the main window, then this step isn't necessary. All you need is the dialog hwnd so you can subclass it, just like you subclassed the main window procedure. But if there is no such message, then you need to find this dialog hwnd by intercepting the Win32 calls that are made it initialize any dialog.

For examples, you could replace SetDlgItemText (likely to be called in any dialog box init), with your own version, which could go like this:

WINUSERAPI BOOL WINAPI r_SetDlgItemTextA(HWND a,int b,LPCSTR c) {

if (bFindFileDlg) {
char txt[80];
GetWindowText(a, txt, 80};
if !strcmp(txt,"File Open") {
SetProp(a,"PrevWndProc",(HANDLE)
(WNDPROC)SetWindowLong(a, GWL_WNDPROC, (LONG)MyDlgProc));
bFindFileDlg = FALSE;
}

return SetDlgItemTextA( a, b, c);

}

Now you need to inject the function above in the slot of the app's import table for SetDlgITemTextA. Again there are many libraries to do this.

For example, there is HookImportFunctionByName. However, note that this function only hooks the import table of one specific exe or dll you specify. What you want is a recursive version that replaces the entry for every DLL loaded by the application. In all liklihood, the main exe isn't making the call to SetDLgItemText directly, but rather throught some dll (e.g. MFC).

I had to write my own recursive version of HookImportFunctionByName but undoubtedly there's something out there already written that does it for you.

To know what win32 calls are being made, it helps to have a debug version of it, or write your own, hooking every single win32 call.

There's probably a half dozen other crucial issues to be aware of, but I guess you'll have to find those out for yourself. Well, one other thing. In your subclassed WndProc, make sure to restore the original WndProc in the WM_DESTROY message.

GeneralRe: Subclassing and hooking the Microsoft Office 2000/2003/2007 File Open / Save Dialog boxes using Win32 C code Pin
Force Code4-Aug-07 13:41
Force Code4-Aug-07 13:41 
GeneralRe: Subclassing and hooking the Microsoft Office 2000/2003/2007 File Open / Save Dialog boxes using Win32 C code Pin
JuggernautMsn6-Aug-07 10:07
JuggernautMsn6-Aug-07 10:07 
QuestionPC wakeup from hibernation using SetWaitableTimer() Pin
Still learning how to code4-Aug-07 1:36
Still learning how to code4-Aug-07 1:36 
AnswerRe: PC wakeup from hibernation using SetWaitableTimer() Pin
Still learning how to code5-Aug-07 2:17
Still learning how to code5-Aug-07 2:17 
QuestionProblem with OnDrawClipboard Pin
dSolariuM4-Aug-07 1:12
dSolariuM4-Aug-07 1:12 
QuestionTapi reconnection Pin
mehrdadov4-Aug-07 0:06
mehrdadov4-Aug-07 0:06 
QuestionVS 2003 Tab Controls Pin
Code_Ray3-Aug-07 13:08
Code_Ray3-Aug-07 13:08 
AnswerRe: VS 2003 Tab Controls Pin
Mark Salsbery3-Aug-07 13:37
Mark Salsbery3-Aug-07 13:37 
AnswerRe: VS 2003 Tab Controls Pin
Michael Dunn3-Aug-07 14:48
sitebuilderMichael Dunn3-Aug-07 14:48 
AnswerRe: VS 2003 Tab Controls Pin
bob169723-Aug-07 16:15
bob169723-Aug-07 16:15 
QuestionPrinting in MFC [modified] Pin
hxhl953-Aug-07 12:30
hxhl953-Aug-07 12:30 
AnswerRe: Printing in MFC Pin
bob169723-Aug-07 16:02
bob169723-Aug-07 16:02 
QuestionRe: Printing in MFC Pin
hxhl953-Aug-07 19:32
hxhl953-Aug-07 19:32 
AnswerRe: Printing in MFC Pin
bob169724-Aug-07 3:41
bob169724-Aug-07 3:41 
GeneralRe: Printing in MFC Pin
hxhl954-Aug-07 6:36
hxhl954-Aug-07 6:36 
AnswerRe: Printing in MFC Pin
bob169723-Aug-07 16:25
bob169723-Aug-07 16:25 
AnswerRe: Printing in MFC Pin
hxhl954-Aug-07 6:42
hxhl954-Aug-07 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.