简体   繁体   English

错误C2440:'type cast':无法从'overloaded-function'转换为'HOOKPROC'

[英]error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'HOOKPROC'

I`m working on keylogger, and this is what I got 我正在研究键盘记录器,这就是我得到的

Keylogger.h: 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: 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: 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;
}

On the following line kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) kl.KeyEvent, GetModuleHandle(NULL), 0); 在以下行kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) kl.KeyEvent, GetModuleHandle(NULL), 0); i get error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'HOOKPROC' 我得到错误C2440:'type cast':无法从'overloaded-function'转换为'HOOKPROC'

Is there a way to fix this? 有没有办法解决这个问题?

WinAPI is mostly a C programming interface, so it doesn't know about classes. WinAPI主要是一个C编程接口,因此它不了解类。 The callback should be static: 回调应该是静态的:

static LRESULT WINAPI KeyEvent(int nCode, WPARAM wParam, LPARAM lParam);

Then 然后

kl.hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC) Keylogger::KeyEvent, GetModuleHandle(NULL), 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c ++:错误C2440:“ <function-style-cast> &#39;:无法从&#39;A转换 <TYPE> &#39;至&#39;B <TYPE> &#39; - c++:error C2440: '<function-style-cast>' : cannot convert from 'A<TYPE>' to 'B<TYPE>' 错误C2440:“类型转换”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“ DWORD” - error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'DWORD' 错误C2440:“类型转换”:无法从“布尔”转换为“ CString” - error C2440: 'type cast' : cannot convert from 'bool' to 'CString' 错误 C2440 '<function-style-cast> ': 无法从 'char' 转换为 'std::string'</function-style-cast> - Error C2440 '<function-style-cast>': cannot convert from 'char' to 'std::string' 错误c2440&#39;=&#39;无法从int *转换为Type <T> * - Error c2440 '=' cannot convert from int * to Type<T> * 错误C2440:“ <function-style-cast> &#39;:无法从“ LinkedList”转换 <int> :: Node *&#39;到&#39;LinkedListIterator <const T> &#39; - error C2440: '<function-style-cast>' : cannot convert from 'LinkedList<int>::Node *' to 'LinkedListIterator<const T>' 计时代码“C2440:”中的一个奇怪错误 <function-style-cast> &#39;:无法从&#39;_CR&#39;转换为&#39;std :: chrono :: milliseconds&#39;“ - A strange error in chrono code “C2440: '<function-style-cast>' : cannot convert from '_CR' to 'std::chrono::milliseconds'” 无法从&#39;overloaded-function&#39;转换为&#39;QLabel *&#39;? - Cannot convert from 'overloaded-function' to 'QLabel *'? C ++嵌套类模板错误C2440&#39;=&#39;:无法从&#39;type&#39;转换为&#39;same type&#39; - C++ Nested class template Error C2440 '=': cannot convert from 'type' to 'same type' 错误C2440:“返回”:无法从“ int [2]”转换为“ int(&amp;&amp;)[2]” - Error C2440: 'return' : cannot convert from 'int [2]' to 'int (&&)[2]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM