开发者

Problem with Windows Hook Procedure

开发者 https://www.devze.com 2023-02-18 03:16 出处:网络
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) genera

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?


  • 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
0

精彩评论

暂无评论...
验证码 换一张
取 消