I'm trying to hook the creation of the 'Page Setup' dialog box. In particular, I am trying to hook the one that is created in Internet Explorer from the file menu.
I have created a global hook and hook proc in a dll file and have been succesful at testing a capture of the right mouse button and other things but I can't seem to find out what to do to catch the creation of any dialog box including the one that I am interested in.
Here's my code that I'm using to set the hook. pData->g_hHook = SetWindowsHookEx (WH_GETMESSAGE, (HOOKPROC) SecProc, pData->g_hInstance, 0) ;
Is WH_GETMESSAGE the proper way to do this or is it done in another way?
I also don't know exactly which message I should be checking for. I'm assuming it would be WM_CREATE but I then would need to be able to distinghuish what is getting created and disregard it if it's not what I'm looking for. And I haven't been able to hook that message yet.
I know that I'll slap my self in the forehead once I have an answer to this but I'm up against a wall for an answer right now.
Thank you in advance for any input on this.
Last edited by patrickmoe; August 17th, 2006 at 05:30 PM.
I am still having problems intercepting the correct message in my hook procedure.
I am now using the WH_CBT method for setting the hook and I'm assuming that I need to filter for the HCBT_CREATEWND message in my hook procedure.
While testing, I open up a copy of Internet Explorer and click File->Page Setup in hopes of triggering this message and my hook procedure is not seeing it.
Here is the code from my dll file. What am I missing?
pData->g_hHook = SetWindowsHookEx (WH_CBT, (HOOKPROC) SecProc, pData->g_hInstance, 0) ;
/*Should the 3rd argument for SetWindowsHookEx be NULL?*/
/*pData->g_hWnd is set eqaul to the hwnd passed from my test executable*/
if(pData->g_hHook)
{
/*Hook is set.*/
hwndSecHook = hwnd;
return TRUE;
}
//Hook procedure for hooking the 'Page Setup' dialog box
//I have assumed that I need to filter for the HCBT_CREATEWND message but it doesn't seem
//to be getting sent when I open I.E. and open File->Page Setup.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
TCHAR szClassName[64];
int nRet = GetClassName(/*hWnd*/pData->g_hWnd, szClassName, 64);
/*I think that hwnd in the previous line should be the hwnd of the dialog that I am trying to hook*/
/*I need to figure out how to get this so I can check the properties of this dialog and act accordingly*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////
LPMSG lpmsg;
BOOL success = FALSE;
if (nCode <0)
{
/*Pass it on*/
CallNextHookEx(pData->g_hHook, nCode, wParam, lParam);
return 0;
}
lpmsg = (LPMSG) lParam;
/*I eventually need to filter for what ever message it is that creates the Page Setup Dialog.*/
/*I thought this was it but it doesn't seem to be getting triggered.*/
if (lpmsg->message == HCBT_CREATEWND)
{
success = ShowCustomDialog(0);
}
Please, next time use CODE tags. It is very hard to read snippets without this tags.
WH_CBT method? WH_CBT is a hook type, not a method.
HCBT_CREATEWND is the right choice of code for checking window creation.
A question: What is hwndSecHook? Is this variable in the shared memory segment?
Yes WH_CBT hook type. I used the wrong terminology for it. I used the word method pretty loosely not thinking about it's meaning to the programming community. Sorry.
As far as I know. hwndSecHook is in the shared segment if this is the correct way to do that.
This code is just after my includes in the source code for my dll.
I am also aware of the PageSetupDlg API. I'm not having a problem with this part. I've gotten the code created already to create my own Page Setup dialog with the things disabled that I need secured.
I really appreciate the help from you guys.
I have never had to work with hooks before and have done very little with dlls.
I will be modifying my code according to the advice here and re-testing again this morning.
Last edited by patrickmoe; August 21st, 2006 at 09:38 AM.
The hInstance variable gets set to the hModule argument in DLLMain. The other two are getting initialized to NULL.
Should this variable also be in shared memory?
I'm also not familiar with how to tell the linker to use shared, read/write. I'll be taking a look into that.
I will also be taking a closer look at our sample code.
In the mean time, I am cleaning up my code a little bit and getting rid of the stuff I've commented out. I'm confused enough already without that stuff there.
I've also found that debugging dll files can be a PITA.
1. Sample shows how to hook certain process (thread) and uses Notepad as an example.
2. If you want hook to be injected to all running processes set thread ID in SetWindowsHookEx to NULL and pass only DLL’s instance.
3. All variables that are used in a hook procedure must be global in shared, writable and readable memory. Moreover, they must be initialized. If not they are process bound.
4. You have to tell linker to use memory that was marked shared by either using def fiel and setting SECTIONS (def statement) and marking .WhateverSegmentName to SWR.
You can also use project settings and enter /SECTION specifying name of the segment and attribute.
The mos popular is using #pragma comment(linker, "/SECTION:. WhateverSegmentName)
Dot is optional and used by convention.
The reason for using shared memory id that each process is running in different address space and if variables are not shared, they will point into a lala land in other than DLL loading processes. By placing variables in shared chunk of memory, you assure that each process will map variables properly in own address space.
Since initializing custom data (arrays, structures and so on) is not simple, using custom data would not be possible, hence you must use simple types.
There are only 10 types of people in the world: Those who understand binary and those who do not.
hinstance is a global static variable of type HINSTANCE and getting initialized to NULL on creation.
It is getting set equal to hModule from DLLMain.
All I am trying to do is hook the creation of the Page Setup dialog box and open my own dialog in it's place. This has to be hooked and modified no matter what application opens this dialog.
I am out of ideas here and I know that it is something really stupid I am overlooking.
For anyone who cares enough to help a moron, I am attaching a zip file with my project in it.
And I'd appreciate it if you don't laugh to hard looking at it.
Thanks.
Last edited by patrickmoe; August 25th, 2006 at 10:23 AM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.