Link to home
Start Free TrialLog in
Avatar of myson
myson

asked on

Hooks

Hello!
I am using a keyboard-hook with code in a DLL like this:

#define STRICT
#include <windows.h>

#pragma comment(linker, "/SECTION:.shared,RWS")
#pragma data_seg(".shared")
    HHOOK hHook = NULL;
#pragma data_seg()

HANDLE      hInstance;

BOOL WINAPI DllMain (HINSTANCE hInst, DWORD dwReason, PVOID pvReserved)
{
    if (dwReason == DLL_PROCESS_ATTACH)
        hInstance = hInst;
    return TRUE;
}

LRESULT CALLBACK KeyboardProc(int iCode, WPARAM wParam, LPARAM lParam)
{
    if (iCode >= 0 )
    {
        if ((iCode == HC_ACTION) && (! (lParam & 0x80000000) ))
             MessageBox(NULL, "MsgBox", "A key was pressed", MB_ICONINFORMATION);
    }
    return CallNextHookEx(hHook,iCode,wParam,lParam);
}

__declspec (dllexport) BOOL KeylogOff()
{      
     if (hHook)
          if (UnhookWindowsHookEx(hHook))
               hHook = NULL;
     return (!hHook);
}

__declspec (dllexport) BOOL KeylogOn()
{
     if (hHook)
         KeylogOff();
     hHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) KeyboardProc, (HINSTANCE) hInstance, 0);
     if (!hHook)
     {
         MessageBox(NULL,"Cannot set Keyboard hook","Error",MB_OK);
         return 0;
     }
     return 1;
}


This code works, but if I run another program, that uses a keyboard-hook (even another instance of my program, runned from other directory), he's in front of the hook chain, and I'm not getting messages. Is there a function, that informs me, what is my current position in hook chain? Is my KeyboardProc function passing messages to next hook right?
Another problem is in MS-DOS. If I run MS-DOS prompt in Windows, my program doesn't get keyboard messages from that window. I've tried WH_GETMESSAGE filter, but I don't know, what message I have to capture.

Thanks in advance.
Avatar of balakrishnan_t
balakrishnan_t

This is related with device driver,,try to post the question in some other section to get some answer
Avatar of myson

ASKER

I don't think so, because the code above does the same errors on 3 computers with different Windows 9x.
But thanks. Does anybody know how to solve this problem?
>> Is there a function, that informs me, what is my current position in hook chain?

No.
Avatar of myson

ASKER

OK, if it is no such function, why don't I get messages, when my program isn't in front of the hook chain? Where's the error? And why doesn't work WH_KEYBOARD on DOS windows? So what messages receive DOS windows, when they get keyboard input?
But thanks.
Avatar of myson

ASKER

I have solved the DOS window problem. The message is not a message like WM_KEYDOWN, but a strange, probably undocumented, message 0x41A. Hmm. OK.
But I am still looking for a solution for that problem with hook chains. I have downloaded Microsoft's sample app, and I have copied the .exe to two different dirs and runned....and the file, that was runned first, wasn't working. Is it possible to run 2 programs that are using the, for example, WH_KEYBOARD hooks and works both?
Thanks a lot.
>> Where's the error?

My guess?  Messing up the call to CallNextHookEx().
Avatar of myson

ASKER

Err, I have commented out some code...Now it works. I have tested it with Microsoft's Hook Sample Application and that is the error. Sample Hook App doesn't call another hook in hook chain, but the source codes are good. Hmm.
How can I protect from these programs...If there isn't any function, that indicates my position in chain, is there any algoritm with I can find out that I am not first in hook chain or that messages are not comming to me,  although they are being produced? Then I would only reset the hook and it would be again in front of the chain.
>> How can I protect from these programs...

You cannot.  Unless you want to get ugly and regularly hook and unhook just to see if your hook is the last in the chain.  Something like:

    hHook = SetWindowsHookEx(...);
    if (hHook == hSaved)
        UnhookWindowsHookEx(hHook);
    else
    {
        UnhookWindowsHookEx(hSaved);
        hSaved = hHook;
    }

Not something I'd be happy with but it may work.
Avatar of myson

ASKER

I have probably not understood you code above. I must create a dummy hook, hHook, and see, if it equals with my original hook, hSaved. If equals, it is in front of the chain. But hHook and hSaved are always different hooks, so "if (hHook == hSaved)" will never be true. Right? Or do you mean that I must in periodical times reset my hook? I am probably a little stupid.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of myson

ASKER

Yes, that's exactly what I have been looking for. It is excellent! Thanks VERY MUCH.