gpt4 book ai didi

c++ - 未解析的外部符号 public __thiscall 仅在 Debug模式下

转载 作者:行者123 更新时间:2023-11-28 03:07:40 25 4
gpt4 key购买 nike

我四处寻找试图解决这个错误的帖子,但在每种情况下我都已经按照他们的建议去做了。

我的编译输出:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall KeyLogger::~KeyLogger(void)" (??1KeyLogger@@QAE@XZ) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall KeyLogger::KeyLogger(void)" (??0KeyLogger@@QAE@XZ) referenced in function _main

debug\AccipioKeyDemo.exe:-1: error: LNK1120: 2 unresolved externals

我知道这是在说我定义了 KeyLogger 构造函数和析构函数但尚未实现,但实际上我确实实现了所有内容。

主要.cpp

#include <QCoreApplication>
#include "keylogger.h"

int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);

KeyLogger k;

return a.exec();
}

键盘记录器.h

#ifndef KEYLOGGER_H
#define KEYLOGGER_H

#include <Windows.h>

class KeyLogger {

public:
KeyLogger();
~KeyLogger();

void start();
void stop();

private:
HHOOK hook;

LRESULT CALLBACK intercept(int code, WPARAM wparam, LPARAM lparam);
};

#endif // KEYLOGGER_H

键盘记录器.cpp

#include "keylogger.h"
#include <QDebug>

KeyLogger::KeyLogger() : hook(NULL) {
hook = SetWindowsHookEx(WH_KEYBOARD_LL, intercept, NULL,0);

if (hook == NULL) {
qDebug() << "HOOK FAILED";
} else {
qDebug() << "HOOK SUCCESS";
}
}

KeyLogger::~KeyLogger() {

}

void KeyLogger::start() {
qDebug() << "start";
}

void KeyLogger::stop() {
qDebug() << "stop";
}

LRESULT CALLBACK KeyLogger::intercept(int code, WPARAM wparam, LPARAM lparam) {
qDebug() << "Key Pressed";
return CallNextHookEx(hook, code, wparam, lparam);
}

QT Pro 配置

#-------------------------------------------------
#
# Project created by QtCreator 2013-10-10T19:58:51
#
#-------------------------------------------------

QT += core

QT -= gui

TARGET = AccipioKeyDemo
CONFIG += console
CONFIG -= app_bundle

LIBS += user32.lib

TEMPLATE = app

SOURCES += main.cpp \
keylogger.cpp

HEADERS += \
keylogger.h

最佳答案

您的代码已损坏,因为回调方法必须是静态 成员——本质上它们必须是自由函数。由于无法将指向 KeyLogger 实例的指针传递给拦截回调函数,因此您的 Hook 必须是类成员,而不是实例成员。用互斥锁保护钩子(Hook)也许不是一个坏主意,以防你后来忘了自己并试图在多个线程中实例化 KeyLogger

在您的情况下,KeyLogger 对象可复制也是一个错误。在不打算复制的类上使用 Q_DISABLE_COPY 宏。

您可能想删除构建目录并重新构建您的项目,但请记住修复如下所示的错误,否则它将无法运行。

下面的最小化版本(只是一个 main.cpp 文件)工作和编译都很好。它演示了如何正确处理事件队列中的转储数据:您必须在持有互斥锁的同时从队列中复制一小块数据,然后释放互斥锁,然后才将其转储到其他地方。

#include <QCoreApplication>
#include <QMutex>
#include <QDebug>
#include <QQueue>
#include <QDataStream>
#include <windows.h>

struct KeyLoggerEvent {
WPARAM event;
KBDLLHOOKSTRUCT key;
KeyLoggerEvent(WPARAM ev, KBDLLHOOKSTRUCT k) : event(ev), key(k) {}
};
QDataStream & operator<<(QDataStream & s, const KeyLoggerEvent & kev) {
s << kev.event
<< (quint32)kev.key.flags << (quint32)kev.key.scanCode
<< (quint32)kev.key.time << (quint32)kev.key.vkCode;
return s;
}

class KeyLogger {
Q_DISABLE_COPY(KeyLogger)
static QMutex m_hookMutex;
static HHOOK m_hook;
static QQueue<KeyLoggerEvent> m_events;
static LRESULT CALLBACK intercept(int code, WPARAM wparam, LPARAM lparam);
public:
KeyLogger() {
QMutexLocker lock(&m_hookMutex);
Q_ASSERT(!m_hook);
m_hook = SetWindowsHookEx(WH_KEYBOARD_LL, intercept, NULL,0);
if (!m_hook) qDebug() << "HOOK FAILED";
lock.unlock();
}
~KeyLogger() {
QMutexLocker lock(&m_hookMutex);
if (m_hook) UnhookWindowsHookEx(m_hook);
m_hook = NULL;
}
//! Dumps a bunch of events to the stream. Returns false if no more events remain in the
//! log. To avoid lock contention, it keeps the queue lock for a very short amount of time.
bool dump(QDataStream & s) {
int batchCount = 1000;
QQueue<KeyLoggerEvent> dumpQueue;
QMutexLocker lock(&m_hookMutex);
while (batchCount-- && !m_events.empty()) {
dumpQueue.enqueue(m_events.dequeue());
}
bool more = !m_events.empty();
lock.unlock();
// The below could block for a long time, thus it works from a local copy.
while (! dumpQueue.empty()) s << dumpQueue.dequeue();
return more;
}
};

QMutex KeyLogger::m_hookMutex;
HHOOK KeyLogger::m_hook = NULL;
QQueue<KeyLoggerEvent> KeyLogger::m_events;

LRESULT CALLBACK KeyLogger::intercept(int code, WPARAM wparam, LPARAM lparam) {
qDebug() << "Key Event";
QMutexLocker lock(&m_hookMutex);
if (code >= 0) {
KBDLLHOOKSTRUCT * key = reinterpret_cast<KBDLLHOOKSTRUCT*>(lparam);
m_events.enqueue(KeyLoggerEvent(wparam, *key));
}
HHOOK hook = KeyLogger::m_hook;
lock.unlock();
return CallNextHookEx(hook, code, wparam, lparam);
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
KeyLogger k;
return a.exec();
}

关于c++ - 未解析的外部符号 public __thiscall 仅在 Debug模式下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19309201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com