Re: Another Hook question
Use WH_CBT hook.
You will be able to check window’s class name; for dialogs, property sheet and property sheets windows this class is #32770.
You will have to think about a method to uniquely identify window you need.
Re: Another Hook question
Thank you so much for the help.
I knew it would be a no brainer.
Re: Another Hook question
Re: Another Hook question
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?
Quote:
__declspec (dllexport) BOOL WINAPI SetHook(HWND hwnd)
{
/*If already hooked,don't hook again*/
if(hwndSecHook != NULL)
return FALSE;
pData->g_hWnd = hwnd;
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 is not set*/
return FALSE;
}
__declspec (dllexport) BOOL WINAPI UnHook(HWND hwnd)
{
BOOL UnHooked;
if(hwnd != hwndSecHook)
return FALSE;
UnHooked = FALSE;
UnHooked = UnhookWindowsHookEx (pData->g_hHook);
if (UnHooked)
hwndSecHook = NULL;
return UnHooked;
}
LRESULT CALLBACK SecProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//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);
}
return CallNextHookEx (pData->g_hHook, nCode, wParam, lParam);
}
BOOL ShowCustomDialog(int Style)
{
/*Style can equal anything right now. It is for future use*/
PAGESETUPDLG lppsd;
int RetVal;
memset(&lppsd, 0, sizeof(lppsd));
lppsd.lStructSize = sizeof(lppsd);
lppsd.Flags = PSD_DISABLEPRINTER;
RetVal = PageSetupDlg(&lppsd);
return RetVal;
}
Re: Another Hook question
Hi patrickmoe,
Your CBT procedure has the wrong format. Try something like this:
Code:
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0)
return CallNextHookEx(NULL, nCode, wParam, lParam);
else if(nCode == HCBT_CREATEWND)
{
HWND hwnd = (HWND)wParam;
CHAR szClassName[255];
CHAR szWindowName[255];
GetClassName(hwnd, szClassName, 255);
GetWindowText(hwnd, szWindowText, 255);
if(!lstrcmp(szClassName, "#32770") && !lstrcmp(szWindowText, "Page Steup"))
{
// Show custom dlg
return 1;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
Re: Another Hook question
Please, next time use CODE tags. It is very hard to read snippets without this tags.
Quote:
Originally Posted by patrickmoe
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.
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?
Re: Another Hook question
The 'Page Setup' dialog box is created by PageSetupDlg API.
Re: Another Hook question
Quote:
Originally Posted by JohnCz
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.
Code:
#pragma data_seg(".SHARED")
static HWND hwndSecHook = NULL;
#pragma data_seg()
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.
Re: Another Hook question
That looks OK.
I have to admit, that first time I have overlooked pData. What is it?
All variables that you are using across processes should be placed in shared, read, write memory segment. Are they?
You have to tell linker to use memory as rws. Did you?
I found my sample in this thread showing mouse hook set up, but you can use it to hook WH_CBT type.
Re: Another Hook question
pData is a pointer to this structure
Code:
typedef struct
{
HHOOK g_hHook;
HWND g_hWnd;
HINSTANCE g_hInstance;
} SCGLOBALDATA;
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.
Re: Another Hook question
Couple of things to point:
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.
Re: Another Hook question
Quote:
Originally Posted by patrickmoe
I've also found that debugging dll files can be a PITA.
Why? it is as simple as debugging exe module.
Re: Another Hook question
I wanted very badly to get to this project again today and try the stuff you guys told me but it got pre-empted by a more important project.
I will let you know how things turn out.
I can't seem to get a break point set inside the Hook Procedure in my DLL or in my Custom Dialog function.
I think some stuff may have gotten out of sync between the exe and the dll.
I'll be working on it some more tonight and tomorrow.
Thanks again.
1 Attachment(s)
Re: Another Hook question
I am still having mixed results with this project.
I can not seem to get my hook procedure to get hit by the creation of a dialog box unless I create that dialog from within my test application.
As far as I know, I have got the hook set as a system hook but the messages aren't getting caught by it.
Code:
hook = SetWindowsHookEx(WH_CBT, (HOOKPROC) HookProc, hinstance, 0);
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. :rolleyes:
Thanks.