Windows Hook 过程的问题
我写了一个小请求来了解事件何时发生。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)