• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

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.
0
myson
Asked:
myson
  • 6
  • 4
1 Solution
 
balakrishnan_tCommented:
This is related with device driver,,try to post the question in some other section to get some answer
0
 
mysonAuthor Commented:
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?
0
 
alexoCommented:
>> Is there a function, that informs me, what is my current position in hook chain?

No.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mysonAuthor Commented:
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.
0
 
mysonAuthor Commented:
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.
0
 
alexoCommented:
>> Where's the error?

My guess?  Messing up the call to CallNextHookEx().
0
 
mysonAuthor Commented:
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.
0
 
alexoCommented:
>> 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.
0
 
mysonAuthor Commented:
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.
0
 
alexoCommented:
>> Right?

Hmmm...  Let's try again:

Creade a dummy hook proc:

    LRESULT CALLBACK f(int code, WPARAM wParam, LPARAM lParam)
    {
        return 0;
    }

And a function to return an ID based on the last hook in the chain:

    DWORD GetId()
    {
        HHOOK x = SetWindowsHookEx(WH_KEYBOARD, f, NULL, 0);
        // If doesn't work, try replacing last '0' with GetCurrentThreadId()
        UnhookWindowsHookEx(x);
        return (DWORD)x;
    }

Now, to check:

    if (saved != GetId())
    {
        UnhookWindowsHookEx(hHook); // remove from chain
        hHook = SetWindowsHookEx(...); // re-hook
        saved = GetId();
    }

Effectively, we just devised a method to see if your hook is the last in the chain.

>> Or do you mean that I must in periodical times reset my hook?

That is a possibility too.

>> I am probably a little stupid.

No you're not.
0
 
mysonAuthor Commented:
Yes, that's exactly what I have been looking for. It is excellent! Thanks VERY MUCH.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now