Click to See Complete Forum and Search --> : Hooks -
.pcbrainbuster
November 20th, 2007, 02:09 PM
Hello again guys,
Do hooks require the knowlegde of working with DLLs? Isn't there any other way? Just in case it matters, the two hooks I'm interested in are mouse and keyboard hooks.
Thanks.
JohnCz
November 20th, 2007, 04:08 PM
You are in luck.
While all hooks require DLL in order to qualify as global hooks, there are two and only two (to the best of my knowledge) that can be placed in an exe and still be global.
They require at least Windows NT 4 and surprise: they are mouse and keyboard hooks: WH_MOUSE_LL and WH_KEYBOARD_LL low level hooks.
See MSDN for more information.
.pcbrainbuster
November 20th, 2007, 05:54 PM
Thanks! I will.
.pcbrainbuster
November 20th, 2007, 06:27 PM
Man! I tried and I tried but couldn't make it work -
hHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHook, NULL, 0);
My compiler said there is something wrong with the second parameter. Any ideas?
Thanks.
JohnCz
November 21st, 2007, 01:32 AM
My compiler said there is something wrong with the second parameter. Any ideas?Yes, there is something wrong with the second parameter. On the other hand my compiler never gives me vague messages.
C’mon .pcbrainbuster, what would you expect anybody to tell you? You are a programmer and you should know how important is to be precise in describing problems.
What exactly was the error message?
How did you declare MouseHook?
.pcbrainbuster
November 21st, 2007, 08:58 AM
Well yeah but I don't usually give the exact error but know will start to :)
The error was - Type mismatch in parameter 'ipfn' (wanted 'int(__stdcall *)()', got 'long(__stdcall *)(int, unisigned int, long)')
And here's the MouseHook function -
LRESULT CALLBACK MouseHook(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{
CallNextHookEx(hHook, nCode, wParam, lParam);
}
switch(nCode)
{
case WM_LBUTTONDOWN :
MessageBox(NULL, "Yes!", "Status", MB_OK);
break;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
Any ideas?
Thanks.
Marc G
November 21st, 2007, 12:55 PM
That should work. Your compiler shouldn't give an error on that line :s
.pcbrainbuster
November 21st, 2007, 01:24 PM
But for some apparent reason it does(BTW the compiler is Borland C++ Builder 6).
Thanks.
JohnCz
November 21st, 2007, 01:38 PM
To tell you the truth, I have no idea. Your hook callback function looks perfectly OK.
Error message does not look like it was generated by regular versions of MS Visual Studio ( I do not know about Express edition).
What environment are you using?
VladimirF
November 21st, 2007, 01:46 PM
But for some apparent reason it does(BTW the compiler is Borland C++ Builder 6).Apparently, the reason is not "apparent". :)
Don't know Borland Builder, but you could try to cast your function pointer to (HOOKPROC).
.pcbrainbuster
November 21st, 2007, 02:26 PM
Hmm, well, my program actually started up but nothing happened when I clicked outside my window :(
Any ideas?
Thanks.
VladimirF
November 21st, 2007, 02:57 PM
Hmm, well, my program actually started up but nothing happened when I clicked outside my window :(
Any ideas?What program?
.pcbrainbuster
November 21st, 2007, 04:29 PM
What do you mean by "What program", I'm talking about my test program?
Thanks.
JohnCz
November 21st, 2007, 04:45 PM
Nothing happens is not a good description of a problem. I assume you are referring to a code not being executed.
That is happening because in your hook callback, you use switch(nCode) and then case WM_LBUTTONDOWN.
Code is not a mouse message it is hook specific code. You should use WPARAM; this parameter is a message identifier.
Check MSDN for description of low level mouse procedure.
By the way what was the compiler problem you indicated in previous post?
Another thing: displaying message box inside of the hook callback is not a good idea; there is certain amount of time that callback has to complete handling a message. If callback exceeds this time, system calls next hook and procedure is removed from a hook chain.
VladimirF
November 22nd, 2007, 05:35 PM
What do you mean by "What program", I'm talking about my test program?I understood that. :)
I was asking - what kind of program? Console? Where do you set your hook? How exactly?
In other words, when I asked "What program" - I meant it would be nice to see the code that caused an issue.
.pcbrainbuster
November 22nd, 2007, 05:39 PM
Well I just demonstrated :) By the way its Win32...
Thanks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.