Windows Hook 过程的问题

发布于 2024-10-23 22:42:28 字数 1502 浏览 1 评论 0原文

我写了一个小请求来了解事件何时发生。我在 main.c 中安装了钩子程序,然后程序等待 10 秒并删除钩子。在暂停期间,我(尝试)生成一些事件,并且对于每个事件,都应该在 log.txt 中打印一个“x”。我在 msdn 中读到了 hook 参数,我正在 Win7 上从命令行使用 Mingw...并且(也许)为我糟糕的英语感到抱歉:'(。 我基本上搜索了教程,但什么也没找到。

// my DLL's code
#include <windows.h>
#include <stdio.h>
#include "dll_header.h"

EXPORT LRESULT CALLBACK hookproc (int nCode, WPARAM wParam, LPARAM lParam){
FILE *fp = fopen ("log.txt", "wb");
fprintf(fp, "\nx");
fclose(fp);
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
FILE *fp = fopen ("log.txt", "wb");
fprintf(fp, "\nOk, the DLL is called");
fclose(fp);
return TRUE;
}

// this is the main.c
#include <windows.h>
#include "dll_header.h"
#include <stdio.h>

EXPORT LRESULT CALLBACK hookproc(int nCode, WPARAM wParam, LPARAM lParam);

int main(){
HMODULE hm = LoadLibrary ("lib.dll");
printf("\n%x", hm); // for my feedback

HHOOK hh = SetWindowsHookEx (WH_KEYBOARD, hookproc, hm, 0);
printf("\n%x", hh); // for my feedback

Sleep(10000);

BOOL b = UnhookWindowsHookEx (hh);
printf("\n%x", b); // for my feedback
return 0;
}

认为有一个明显的错误,因为代码非常简单,所以在 log.txt 中,我只找到针对每种类型的钩子(不仅针对 WH_KEYBOARD)的“好吧,调用 DLL”行。你能帮我吗?

编辑:

好吧,我用w/a更改了“fopen”mod...为什么我必须编写消息循环?这不是一个winows过程,系统不是为每个事件调用钩子过程吗?我不明白为什么我的钩子过程没有被调用,你能修改我的代码以便我知道钩子过程何时被调用吗?

PS:我正在阅读 msdn 上有关 hooking 的文章,你知道还有其他好/更好的地方可以学习吗?

I wrote a little applicantion to know when an event occurrs. I the main.c the hook procedure is being installed, then the program wait 10 second and remove the hook. During the pause i (try to) generate some event, and for every one an "x" shuld be printed in log.txt. I read the hook argument in msdn, i'm working with Mingw from command line, on Win7... and (maybe) sorry for my BAD english :'(.
I searched tutorials as basically, but i found nothing.

// my DLL's code
#include <windows.h>
#include <stdio.h>
#include "dll_header.h"

EXPORT LRESULT CALLBACK hookproc (int nCode, WPARAM wParam, LPARAM lParam){
FILE *fp = fopen ("log.txt", "wb");
fprintf(fp, "\nx");
fclose(fp);
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
FILE *fp = fopen ("log.txt", "wb");
fprintf(fp, "\nOk, the DLL is called");
fclose(fp);
return TRUE;
}

and

// this is the main.c
#include <windows.h>
#include "dll_header.h"
#include <stdio.h>

EXPORT LRESULT CALLBACK hookproc(int nCode, WPARAM wParam, LPARAM lParam);

int main(){
HMODULE hm = LoadLibrary ("lib.dll");
printf("\n%x", hm); // for my feedback

HHOOK hh = SetWindowsHookEx (WH_KEYBOARD, hookproc, hm, 0);
printf("\n%x", hh); // for my feedback

Sleep(10000);

BOOL b = UnhookWindowsHookEx (hh);
printf("\n%x", b); // for my feedback
return 0;
}

I think there is an obvios mistake couse the code is very easy, so in the log.txt i find only the line "Ok, the DLL is called" for every type of hook (not only for WH_KEYBOARD). Can u help me?

EDIT:

Ok, i changed the "fopen" mod with w/a... why do i have to write a message loop? This isn't a winows procedure, doesn't the system call the hook procedure for every event? I can't understood why my hook procedure is not called, can you modify my code so i know when a hook procedure is called??

ps.: i'm reading articles about hooking from msdn, do you know an other good/better place to learning that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-30 22:42:28
  • 您正在安装一个系统范围的挂钩 - 该文件可以由多个进程打开和写入,所有进程都会覆盖其他进程创建的文件。
  • 您没有消息循环,因此不会为您的应用程序调用挂钩。 WH_KEYBOARD 在调用 GetMessage/PeekMessage 时触发
  • You're installing a system-wide hook - The file could be opened and written to by multiple processes, all of them overwriting the file created by the other processes.
  • You don't have a message loop, so the hook will not be called for your application. WH_KEYBOARD is triggered when you call GetMessage/PeekMessage
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文