//Keyboard Hook Procedure inside in the DLL which posts a message to my application
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(ncode>=0)
{
if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
{
hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
}
}
return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}
//the keyboard callback function in my application which received
//WPARAM and LPARAM. During the multiple number of times this function
//is getting called it is always going to the line if(w == VK_SPACE || w == VK_DOWN). It is not resulting any KEYUP/KEYREPEAT events
LRESULT CKeyexeDlg::processkey(WPARAM w, LPARAM l)//This block processes the keystroke info.
{
if (l & 0x80000000) // check bit 31 for up/down
{
//KEY UP
}
else
{
if (l & 0x40000000) // check bit 30 for previous up/down
//KEY REPEAT
else
{
if(w == VK_SPACE || w == VK_DOWN)
{
//the virtual character
}
}
}
//I tried the other alternative. But it doesn't even enter into the switch block.
LPMSG m=(LPMSG)l;
switch(m->message)
{
case WM_KEYDOWN:
l=l;
break;
case WM_KEYUP:
break;
case WM_CHAR:
break;
};
//Tried this as you indicated. No messages are Posted at all, either for keyup or keydown.
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(0 > ncode || PM_NOREMOVE == wparam)
return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
PMSG msg = (PMSG) lparam;
if(msg->message == WM_KEYDOWN || msg->message == WM_KEYUP)
{
hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
}
return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}
//thought of checking where the control is going and tested with. Messages are posted. So, the checks of WM_KEYUP/DOWN are not being sensed!!! and I am not understanding why!
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(0 > ncode || PM_NOREMOVE == wparam)
return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
PMSG msg = (PMSG) lparam;
if(msg->message == WM_KEYDOWN || msg->message == WM_KEYUP)
{
//hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
//PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
}
hwnd = FindWindow("#32770","Keylogger Exe");//Find application window handle
PostMessage(hwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
return ( CallNextHookEx(hook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}
//For your reference below is the function which installs hook in DLL
KEYDLL3_API void installhook(HWND h)
{
hook = NULL;
hwnd = h;
hook = SetWindowsHookEx(WH_KEYBOARD,hookproc,hinstance,NULL);
if(hook==NULL)
MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
}
//in my application which receives event message is mapped as
#define WM_KEYSTROKE (WM_USER + 755)
//all are defined well and working well, except the sensing of
//WM_KEYUP / WM_KEYDOWN
//This is what I am doing inside my hook handler
LRESULT CAI_TalapatraDlg::ProcessKeyboard(WPARAM w, LPARAM l)
{
PMSG msg = (PMSG)lparam;
if(msg->message == WM_KEYDOWN) //access violation error with message.
//debugger shows invalid data for msg
{
if(msg->wParam == VK_SPACE || msg->wParam == VK_DOWN)
//do stuff
}
}
//here is the message posting function inside the DLL. Here everything works fine and the procedure above inside my application is called only once, but not able to process the virtual key code.
KEYHOOK_API LRESULT CALLBACK Hookproc(int nCode, WPARAM wparam, LPARAM lparam)
{
if(0 > nCode || PM_NOREMOVE == wparam)
return ( CallNextHookEx(hook,nCode,wparam,lparam) );//pass control to next hook in the hook chain.
PMSG msg = (PMSG) lparam;
if(msg->message == WM_KEYDOWN)
{
hwnd=FindWindow(NULL, _T("AI Talapatra"));
if(hwnd!=NULL)
PostMessage(hwnd, WM_USER+3, wparam, lparam);
}
return (CallNextHookEx(hook, nCode, wparam, lparam));
}
//This is what I am doing inside my hook handler
LRESULT CAI_TalapatraDlg::ProcessKeyboard(WPARAM w, LPARAM l)
{
PMSG msg = (PMSG)l;
if(msg->message == WM_KEYDOWN) //access violation error with message.
//debugger shows invalid data for msg
{
if(msg->wParam == VK_SPACE || msg->wParam == VK_DOWN)
//do stuff
}
}
KEYHOOK_API void Installhook(HWND h)
{
hook = NULL;
hwnd = h;
hook = SetWindowsHookEx(WH_GETMESSAGE, Hookproc, hinstance, NULL);
if(hook == NULL)
MessageBox(NULL, _T("Unable to Install Hook"), _T("Error"), MB_OK);
}
KEYHOOK_API LRESULT CALLBACK Hookproc(int nCode, WPARAM wparam, LPARAM lparam)
{
if(0 > nCode || PM_NOREMOVE == wparam)
return ( CallNextHookEx(hook,nCode,wparam,lparam) );//pass control to next hook in the hook chain.
PMSG msg = (PMSG) lparam;
if(msg->message == WM_KEYDOWN)
{
hwnd=FindWindow(NULL, _T("AI Talapatra"));
if(hwnd!=NULL)
PostMessage(hwnd, WM_USER+3, msg->wParam, msg->lParam);
}
return (CallNextHookEx(hook, nCode, wparam, lparam));
}
LRESULT CAI_TalapatraDlg::ProcessKeyboard(WPARAM w, LPARAM l)
{
BYTE ks[256];
WCHAR buf[10];
UINT scan=0;
GetKeyboardState(ks);
if(ToUnicodeEx(w, scan, ks, buf, 1, 1, m_hklval))
{
......//even if pressed 'a' or SHIFT + 'a' buf contains only 'a'
}
}
PMSG msg = (PMSG) lparam;
if(msg->message == WM_CHAR)
{
hwnd=FindWindow(NULL, _T("AI Talapatra"));
if(hwnd!=NULL)
PostMessage(hwnd, WM_USER+3, msg->wParam, msg->lParam);
}
//in the above code (when using WM_CHAR) what does wParam and lParam indicate?
//How do I specify that my WM_USER+3 message is posted only when Key is down but not otherwise
//wParam the unicode character?
//then which variable indicates other key hits like VK_SPACE, VK_BACK, etc...
KEYHOOK_API LRESULT CALLBACK Hookproc(int nCode, WPARAM wparam, LPARAM lparam)
{
if(0 > nCode || PM_NOREMOVE == wparam)
return ( CallNextHookEx(hook,nCode,wparam,lparam) ); //pass control to next hook in the hook chain.
PMSG msg = (PMSG) lparam;
if(msg->message == WM_KEYDOWN)
{
hwnd=FindWindow(NULL, _T("AI Talapatra"));
if(hwnd!=NULL)
PostMessage(hwnd, WM_USER+3, msg->wParam, msg->lParam);
}
return (CallNextHookEx(hook, nCode, wparam, lparam));
}
// HookDll.cpp
//
// This is the DLL which both sets the hook and
// subclasses the edit window for the notepad app
// which is hooked
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
HINSTANCE hMod;
HHOOK hHook;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
hMod = (HINSTANCE) hModule;
return TRUE;
}
#ifdef __cplusplus
extern "C" {
#endif
LRESULT CALLBACK HookProc(int code, WPARAM wParam, LPARAM lParam)
{
LRESULT ret = 0;
if (code == HC_ACTION)
{
MSG * pMsg = (MSG *) lParam;
if (pMsg->message == WM_CHAR)
{
if (pMsg->wParam == 'x')
{
pMsg->wParam = 'y';
}
}
}
return(CallNextHookEx(hHook, code, wParam, lParam));
}
__declspec(dllexport) BOOL SetHook(DWORD threadId)
{
hHook = SetWindowsHookEx(WH_GETMESSAGE, HookProc, hMod, threadId);
return(hHook ? TRUE : FALSE);
}
__declspec(dllexport) void ClearHook(void)
{
if (hHook)
{
UnhookWindowsHookEx(hHook);
hHook = 0;
}
}
#ifdef __cplusplus
}
#endif
// GlobalHook.cpp
//
// This is a console application which find an instance of notepad.exe
// and sets a hook into it's process
//
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllimport) BOOL SetHook(DWORD threadId);
__declspec(dllimport) void ClearHook(void);
#ifdef __cplusplus
}
#endif
int _tmain(int argc, _TCHAR* argv[])
{
HWND hEdit;
/*
* Find an instance of notepad to hook
*/
if (hEdit = FindWindow(_T("Notepad"), 0))
{
DWORD threadId = 0;
DWORD processId;
/*
* Get the thread ID for the window
*/
if (threadId = GetWindowThreadProcessId(hEdit, &processId))
{
printf("Setting hook\n");
/*
* Set a hook for this thread
*/
if (SetHook(threadId))
{
printf("Hook set, press any key to clear hook\n");
getch();
printf("Clearing hook\n");
ClearHook();
}
else
{
printf("Cannot set hook\n");
}
}
}
else
{
printf("Cannot find an instance of word\n");
}
return 0;
}
Open in new window