c# - CallWndProc Example -


this first time experimenting hooks.

i'm looking resources implementing callwndproc hook. msdn stuff bit overwhelming.

i have discovered type of hook external dll needs injected. that's i'm stuck.

not sure needs in dll , needs in .net app.

any dll examples?

you cannot write wh_callwndproc hook in managed language c#. need more external dll, need external dll written in language compiles down native code, c or c++.

the msdn documentation pretty good, overview. there's example on using hooks page.

i don't mean sound discouraging, if find overwhelming, you're going have fair bit of trouble getting work right. hooks advanced technique in windows programming. need understand window procedures, message loops, , other basics of windows applications before undertake project one. helps know either c or c++ languages well, since that's you'll using!

anyway, happen have hook dll i've written in c handy, i'll try pull out of relevant code. installs wh_callwndretproc hook, 2 quite similar. hook procedure 1 called after window procedure has processed message; 1 you're talking called before window procedure has processed message.

/* handle hook stored shared global variable ,  * same hooked processes. achieve placing in  * shared data segment of dll.  *  * note shared global variables must explicitly initialized.  *  * , note not ideal way of doing this; it's  * easy way going. better solution use memory-mapped file.  * see also: http://msdn.microsoft.com/en-us/library/h90dkhs0.aspx  */ #pragma comment(linker, "/section:.shared,rws") #pragma data_seg(".shared") /* begin shared data segment */    hhook g_hhkcallwndprocret = null; #pragma data_seg()          /* end shared data segment , default normal behavior */   lresult callback callwndretproc(int ncode, wparam wparam, lparam lparam) {    /* if ncode greater or equal hc_action,     * should process message. */    if (ncode >= hc_action)    {       /* retrieve pointer structure contains details        * message, , see if 1 want handle. */       const lpcwpretstruct lpcwprs = (lpcwpretstruct)lparam;       switch (lpcwprs->message)       {          /* ...snip: process messages we're interested in ... */       }    }     /* @ point, either not processing message     * (because ncode less hc_action),     * or we've finished processing it.     * either way, pass message on. */    return callnexthookex(g_hhkcallwndprocret, ncode, wparam, lparam); }   bool __stdcall installhook(void) {    /* try install wh_callwndprocret hook,     * if not installed. */    if (!g_hhkcallwndprocret)    {       g_hhkcallwndprocret = setwindowshookex(wh_callwndprocret,                                              callwndretproc,                                              g_hinstdll,                                              0);       if (!g_hhkcallwndprocret)       {          /* ...snip: handle failure condition ... */          return false;       }    }     return true;  /* return success */ }  bool __stdcall removehook(void) {    /* try remove wh_callwndprocret hook, if installed. */    if (g_hhkcallwndprocret)    {       if (!unhookwindowshookex(g_hhkcallwndprocret))       {          /* ...snip: handle failure condition ... */          return false;       }       g_hhkcallwndprocret = null;    }     return true;  /* return success */ } 

Comments