Question about hooks?

Question about hooks?

Post by » Fri, 24 Apr 2009 04:38:21


I am using the spy++ utility to observe LVM_INSERTITEM messages.
I want to create my own hook so that I can print out the messages
that are sent from the ListView_InsertItem api. My hook is global
and my problem is that sometimes I do not get messages whereas
spy++ does, and other times when I do get mesages spy++ doesn't
get them at all, so here is my code

LRESULT CALLBACK CallWndProc (int nCode, WPARAM wParam, LPARAM lParam )
{
CWPSTRUCT *lpCWP;
LVITEM FAR *lplvi;
FILE *f1;

if ( nCode >= 0 )
{
lpCWP = (CWPSTRUCT *) lParam;

if ( lpCWP->message == LVM_INSERTITEM )
{
lplvi= (LV_ITEM FAR*) lpCWP->lParam;
f1 = fopen( "c:\\log.txt", "a" );
if(f1==NULL) MessageBox(0,L"cannot open log.txt",L"",0);
fprintf(f1,L"%s \n\n", lplvi->pszText);
fclose (f1);
}
}
//
// We looked at the message ... sort of processed it but since we are
// looking we will pass all messages on to CallNextHookEx.
//
return CallNextHookEx(hhookHook, nCode, wParam, lParam);
}
 
 
 

Question about hooks?

Post by JJ » Fri, 24 Apr 2009 18:29:30


I assume you are using a WH_CALLWNDPROC hook type. The WH_CALLWNDPROC
hook only monitors window messages that will be SENT. All POSTED window
messages will not be catched using WH_CALLWNDPROC hook type. You'll need
to have another hook of type WH_GETMESSAGE to catch posted window messages.

Also, ListView_InsertItem is not a function. It's a macro defined in
commctrl.h that calls SendMessage with LVM_INSERTITEM message.