Keyboard Hook

Hi All,

I have a question about keyboard hooks. I have developed a hook dll and a
exe to run that dll.i have loaded dll implicitly (load time dynamic
linking).I have set a keyboard hook in this Dll.

HHOOK hhook = NULL;
HINSTANCE g_hInstance = NULL;

_declspec(dllexport) LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam,
LPARAM lParam)
{
essageBox(NULL,“SetWindowsHookEx CALLBACK”,NULL,NULL);
return 0;
}

_declspec(dllexport) void SetKBHook()
{
if (!hhook)
{
hhook = SetWindowsHookEx(WH_KEYBOARD,KBHookProc, g_hInstance , 0);
if( hhook == NULL )
MessageBox(NULL,“SetWindowsHookEx failed”,NULL,NULL);
}
}

_declspec(dllexport) void KillKBHook()
{
if(hhook)
{
UnhookWindowsHookEx(hhook);
MessageBox(NULL,“UnhookWindowsHookEx”,NULL,NULL);
}
}

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
MessageBox(NULL,“Dll main: Process Attech”,NULL,NULL);
// Init
g_hInstance = hInstance;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
MessageBox(NULL,“Dll main: Process Dttech”,NULL,NULL);
}
return(1);
}

When I execute the application control goes to DllMain and MessageBox “Dll
main: Process Attech” gets display.
now Whenever I open a new window and press any key on my keyboard, control
goes to KBHookProc then Dllmain and then again to KBHookProc.if I again
press any key on THIS window then control goes to KBHookProc ONLY.

Why MessageBox of dllmain is executing for EVERY new window or why dll is
loading for every new window ?how can I load this kbhook dll ones in the
application ?

Thanks In Advance,
-John


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

I think u r loading ur driver thru ur app. whenever u run app. it loads dll
everytime, thats why control goes there.

rgds,
Nigam

-----Original Message-----
From: xxxxx@yahoo.co.uk [mailto:xxxxx@yahoo.co.uk]
Sent: Monday, December 03, 2001 8:07 AM
To: NT Developers Interest List
Subject: [ntdev] Keyboard Hook

Hi All,

I have a question about keyboard hooks. I have developed a hook dll and a
exe to run that dll.i have loaded dll implicitly (load time dynamic
linking).I have set a keyboard hook in this Dll.

HHOOK hhook = NULL;
HINSTANCE g_hInstance = NULL;

_declspec(dllexport) LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam,
LPARAM lParam)
{
essageBox(NULL,“SetWindowsHookEx CALLBACK”,NULL,NULL);
return 0;
}

_declspec(dllexport) void SetKBHook()
{
if (!hhook)
{
hhook = SetWindowsHookEx(WH_KEYBOARD,KBHookProc, g_hInstance , 0);
if( hhook == NULL )
MessageBox(NULL,“SetWindowsHookEx failed”,NULL,NULL);
}
}

_declspec(dllexport) void KillKBHook()
{
if(hhook)
{
UnhookWindowsHookEx(hhook);
MessageBox(NULL,“UnhookWindowsHookEx”,NULL,NULL);
}
}

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
MessageBox(NULL,“Dll main: Process Attech”,NULL,NULL);
// Init
g_hInstance = hInstance;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
MessageBox(NULL,“Dll main: Process Dttech”,NULL,NULL);
}
return(1);
}

When I execute the application control goes to DllMain and MessageBox “Dll
main: Process Attech” gets display.
now Whenever I open a new window and press any key on my keyboard, control
goes to KBHookProc then Dllmain and then again to KBHookProc.if I again
press any key on THIS window then control goes to KBHookProc ONLY.

Why MessageBox of dllmain is executing for EVERY new window or why dll is
loading for every new window ?how can I load this kbhook dll ones in the
application ?

Thanks In Advance,
-John


You are currently subscribed to ntdev as: xxxxx@dcmtech.co.in
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

The first problem:
Caused by your programming,you just hooked the input
forget to pass the control to the original processing.
In your Program:

_declspec(dllexport) LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam,
LPARAM lParam)
{
MessageBox(NULL,“SetWindowsHookEx CALLBACK”,NULL,NULL);
return 0;
}
replace the “return 0” with “return CallNextHookEx(hhook,code,wParam,lParam);”

The second Problem:
I think the problem is caused by your program.
You called SetWindowsHookEx with the dwThreadId as 0,
this will install the KBDHook for all existing threads.
You can just specify the dwThreadId as the right ThreadId.

----- Original Message -----
From:
To: “NT Developers Interest List”
Sent: Monday, December 03, 2001 2:36 AM
Subject: [ntdev] Keyboard Hook

> Hi All,
>
> I have a question about keyboard hooks. I have developed a hook dll and a
> exe to run that dll.i have loaded dll implicitly (load time dynamic
> linking).I have set a keyboard hook in this Dll.
>
> HHOOK hhook = NULL;
> HINSTANCE g_hInstance = NULL;
>
> _declspec(dllexport) LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam,
> LPARAM lParam)
> {
> essageBox(NULL,“SetWindowsHookEx CALLBACK”,NULL,NULL);
> return 0;
> }
>
> _declspec(dllexport) void SetKBHook()
> {
> if (!hhook)
> {
> hhook = SetWindowsHookEx(WH_KEYBOARD,KBHookProc, g_hInstance , 0);
> if( hhook == NULL )
> MessageBox(NULL,“SetWindowsHookEx failed”,NULL,NULL);
> }
> }
>
> _declspec(dllexport) void KillKBHook()
> {
> if(hhook)
> {
> UnhookWindowsHookEx(hhook);
> MessageBox(NULL,“UnhookWindowsHookEx”,NULL,NULL);
> }
> }
>
> BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
> lpReserved)
> {
> if (dwReason == DLL_PROCESS_ATTACH)
> {
> MessageBox(NULL,“Dll main: Process Attech”,NULL,NULL);
> // Init
> g_hInstance = hInstance;
> }
> else if (dwReason == DLL_PROCESS_DETACH)
> {
> MessageBox(NULL,“Dll main: Process Dttech”,NULL,NULL);
> }
> return(1);
> }
>
> When I execute the application control goes to DllMain and MessageBox “Dll
> main: Process Attech” gets display.
> now Whenever I open a new window and press any key on my keyboard, control
> goes to KBHookProc then Dllmain and then again to KBHookProc.if I again
> press any key on THIS window then control goes to KBHookProc ONLY.
>
> Why MessageBox of dllmain is executing for EVERY new window or why dll is
> loading for every new window ?how can I load this kbhook dll ones in the
> application ?
>
> Thanks In Advance,
> -John
>
> —
> You are currently subscribed to ntdev as: sunsetyang@8848.net
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
???y˫???+.n?+???u?ڲ˛??^r*D???kN???r??zǧu??jy???^j???ׯ??? 0?j?b??(??(

Thanks for responses esp. Chen Yang.
Actually I want to write a driver for multimedia
keyboard which have key as a normal keyboard have and
some special keys for opening calculator,Dos Prompt
etc .
for setting keyboard hook When I specify the
dwThreadId as the right ThreadId and set a keyboard
hook with it then only this thread capturing the
keyboard events. As I am pasting code here, when I
minimize the dialog box then the application is unable
to capture the keyboard event, which I think is right
behavior.
Now my question is how system will capture the
keyboard event if the dialog box is minimized or not
active ?

DWORD dwThreadID;
unsigned long Keystrokes ;
unsigned long Temp ;
LRESULT CALLBACK KBHookProc(int Code, WPARAM wParam,
LPARAM lParam)
{
if (Code < 0) return(CallNextHookEx(hhook, Code,
wParam, lParam));
Keystrokes = (lParam & 0x00FF0000)>>16;//Get The Scan
Code
Temp = (lParam & 0xF0000000)<<1;

switch(Keystrokes)
{
case 0x23: //Calculator
if( wParam!=0x48 && Temp == 0 )
WinExec(“calc.exe”, SW_SHOW);
break;
}

return(CallNextHookEx(hhook, Code, wParam, lParam));
}

int SetKBHook()
{
if (!hhook)
{
hhook = SetWindowsHookEx(WH_KEYBOARD,KBHookProc, NULL
, dwThreadID);
if( hhook != NULL )
MessageBox(NULL,"Set Windows Hook ",NULL,NULL);
}
if(hhook)
return 1;
else
return 0;
}

void KillKBHook()
{
if(hhook)
{
UnhookWindowsHookEx(hhook);
MessageBox(NULL,“UnhookWindowsHookEx”,NULL,NULL);
}
}

BOOL CALLBACK DlgProc(HWND hDlg, UINT msg, WPARAM
wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
if (!SetKBHook()) EndDialog(hDlg, 0);
SetTimer(hDlg, 0, 10, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_OK: EndDialog(hDlg, 0); break;
}
break;
case WM_TIMER:
switch(wParam)
{
case 0:
HWND hTmp = GetDlgItem(hDlg, IDC_EDIT1);
char str[50];
sprintf(str, “%u”, dwThreadID);
SetWindowText(hTmp, str);
break;
}
break;
case WM_DESTROY:
KillKBHook();
break;
}
return(0);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
dwThreadID = GetCurrentThreadId();
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),
NULL, (DLGPROC)DlgProc);
return(1);
}

Thanks and Regards,
-John

— Chen Yang wrote: > The
first problem:
> Caused by your programming,you just hooked the
> input
> forget to pass the control to the original
> processing.
> In your Program:
> >_declspec(dllexport) LRESULT CALLBACK
> KBHookProc(int Code, WPARAM wParam,
> >LPARAM lParam)
> >{
> > MessageBox(NULL,“SetWindowsHookEx
> CALLBACK”,NULL,NULL);
> > return 0;
> >}
> replace the “return 0” with “return
> CallNextHookEx(hhook,code,wParam,lParam);”
>
> The second Problem:
> I think the problem is caused by your program.
> You called SetWindowsHookEx with the dwThreadId as
> 0,
> this will install the KBDHook for all existing
> threads.
> You can just specify the dwThreadId as the right
> ThreadId.
>
> ----- Original Message -----
> From:
> To: “NT Developers Interest List”
>
> Sent: Monday, December 03, 2001 2:36 AM
> Subject: [ntdev] Keyboard Hook
>
>
> > Hi All,
> >
> > I have a question about keyboard hooks. I have
> developed a hook dll and a
> > exe to run that dll.i have loaded dll implicitly
> (load time dynamic
> > linking).I have set a keyboard hook in this Dll.
> >
> > HHOOK hhook = NULL;
> > HINSTANCE g_hInstance = NULL;
> >
> > _declspec(dllexport) LRESULT CALLBACK
> KBHookProc(int Code, WPARAM wParam,
> > LPARAM lParam)
> > {
> > essageBox(NULL,“SetWindowsHookEx
> CALLBACK”,NULL,NULL);
> > return 0;
> > }
> >
> > _declspec(dllexport) void SetKBHook()
> > {
> > if (!hhook)
> > {
> > hhook = SetWindowsHookEx(WH_KEYBOARD,KBHookProc,
> g_hInstance , 0);
> > if( hhook == NULL )
> > MessageBox(NULL,“SetWindowsHookEx
> failed”,NULL,NULL);
> > }
> > }
> >
> > _declspec(dllexport) void KillKBHook()
> > {
> > if(hhook)
> > {
> > UnhookWindowsHookEx(hhook);
> >
> MessageBox(NULL,“UnhookWindowsHookEx”,NULL,NULL);
> > }
> > }
> >
> > BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD
> dwReason, LPVOID
> > lpReserved)
> > {
> > if (dwReason == DLL_PROCESS_ATTACH)
> > {
> > MessageBox(NULL,“Dll main: Process
> Attech”,NULL,NULL);
> > // Init
> > g_hInstance = hInstance;
> > }
> > else if (dwReason == DLL_PROCESS_DETACH)
> > {
> > MessageBox(NULL,“Dll main: Process
> Dttech”,NULL,NULL);
> > }
> > return(1);
> > }
> >
> > When I execute the application control goes to
> DllMain and MessageBox “Dll
> > main: Process Attech” gets display.
> > now Whenever I open a new window and press any key
> on my keyboard, control
> > goes to KBHookProc then Dllmain and then again to
> KBHookProc.if I again
> > press any key on THIS window then control goes to
> KBHookProc ONLY.
> >
> > Why MessageBox of dllmain is executing for EVERY
> new window or why dll is
> > loading for every new window ?how can I load this
> kbhook dll ones in the
> > application ?
> >
> > Thanks In Advance,
> > -John
> >
> > —
> > You are currently subscribed to ntdev as:
> sunsetyang@8848.net
> > To unsubscribe send a blank email to
> leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com
> >
>
b‹š­ç.®·§¶\¬¹»®&ÞvÚ’µ×¯jÉš•Ê&Žˆgɨh¡Ê.‘:.žË›±ÊâmëÖ›•©äzf¢–Ú%y«Þž×^¿žúØ
b²Û(²·(

________________________________________________________________
Nokia 5510 looks weird sounds great.
Go to http://uk.promotions.yahoo.com/nokia/ discover and win it!
The competition ends 16 th of December 2001.


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com