我正在尝试编写一个拦截键盘输入的DLL,并在将值呈现给进程事件循环之前修改scan / vkey代码。简化的DLL代码(由VS 2010生成):
#include "stdafx.h"
#include "hookdll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static const char *LogFileName = "c:/tmp/hook_report.txt";
#define LogOpen() \
{\
freopen(LogFileName, "w", stdout); \
}
#define Log(fmt, ...) if ( 1 )\
{\
fprintf(stdout, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
fflush(stdout); \
}
static HHOOK HookHandle;
char*
DbHookToString(LPARAM lParam)
{
static char tmp[128];
KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
sprintf(tmp,
"vkCode=0x%02X"
", scanCode=0x%02X"
", flags=0x%08X"
", time=%d"
", dwExtraInfo=0x%08X",
hs->vkCode
, hs->scanCode
, hs->flags
, hs->time
, hs->dwExtraInfo);
return tmp;
}
LRESULT CALLBACK
keyHandler(int nCode, WPARAM wParam, LPARAM lParam)
{
// Checks whether params contain action about keystroke
if(nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
Log("IN: %d: %s", wParam, DbHookToString(lParam));
switch(hs->vkCode)
{
default:
break;
case 0x31:
++hs->vkCode;
++hs->scanCode;
break;
case 0x32:
//--hs->vkCode;
//--hs->scanCode;
break;
}
Log("OUT: %d: %s\n", wParam, DbHookToString(lParam));
}
return CallNextHookEx(HookHandle, nCode, wParam, lParam);
}
BEGIN_MESSAGE_MAP(ChookdllApp, CWinApp)
END_MESSAGE_MAP()
ChookdllApp::ChookdllApp()
{
}
ChookdllApp theApp;
BOOL ChookdllApp::InitInstance()
{
LogOpen();
CWinApp::InitInstance();
if (!AfxSocketInit())
{
Log("AfxSocketInit failed");
return FALSE;
}
HookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, NULL, 0);
if(HookHandle == NULL)
{
Log("SetWindowsHookEx failed");
return FALSE;
}
return TRUE;
}
应用程序代码,以骨架形式,是一个C ++窗体表单应用程序,包含单个表单中的单个文本框;问题是输入字符,特别是数字“1”键,即使DLL在日志文件中报告它正在被修改,也会没有改变。帮助任何人?
... hookdll.cpp:84 IN:256:vkCode = 0x31,scanCode = 0x02,flags = 0x00000000,time = 11326265,dwExtraInfo = 0x00000000 ... \ hookdll.cpp:100 OUT:256:vkCode = 0x32,scanCode = 0x03,flags = 0x00000000,time = 11326265,dwExtraInfo = 0x00000000
... \ hookdll.cpp:84 IN:257:vkCode = 0x31,scanCode = 0x02,flags = 0x00000080,time = 11326374,dwExtraInfo = 0x00000000 ... \ hookdll.cpp:100 OUT:257:vkCode = 0x32,scanCode = 0x03,flags = 0x00000080,time = 11326374,dwExtraInfo = 0x00000000
...剪断..
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
HINSTANCE hinstDLL = LoadLibrary("hookdll.dll");
if ( hinstDLL == NULL )
{
volatile int err = GetLastError();
err += 1;
}
}
... ...剪断 非常感谢。