Hook Fails under Win95 anyone knows why

c++ / delphi package - dll injection and api hooking
Post Reply
cool_tester
Posts: 75
Joined: Sun Oct 31, 2004 5:45 am

Hook Fails under Win95 anyone knows why

Post by cool_tester »

Hello there,
First is it possible to Use MadCodeHook to hook WH_JournalRecord, basically i want to Monitor the Mouse and Keyboard activity, i just want to know if the mouse moved, clicked, and if the keyboard is typing or no, and I'm not interested in what keys are used, just want to know the activity, basically i'm trying to calculate how long is the system idle without any users interactivity, so i learned about WH_JournalRecord, which seems to do what i want but it fails if i run the same application Twice, the first one Bombs on me under Win95... here is the code I'm using:

Code: Select all

function Record(Code: integer; wParam, lParam: Longint):Longint; stdcall;
begin
  //Result := CallNextHookEx(Hook, Code, wParam, lParam);
  case Code of
    HC_ACTION: begin
                 MessageBuffer:=PEventMsg(lParam)^;
                 if MessageBuffer.message=wm_KeyDown then
                 begin
                    Form1.Memo1.Text:=Form1.Memo1.Text+
                                     chr(MessageBuffer.paraml);
                 //Pass the message to other application to process this is where i get the problem. i think.
                  Result := CallNextHookEx(Hook, Code, wParam, lParam);
                 end;
               end;
    else begin
            Result := CallNextHookEx(Hook, Code, wParam, lParam);
    end;
  end;
end;

//and to hook it
Hook:=SetWindowsHookEx(wh_journalrecord,Record,HInstance,0);
Now this works fine when i run the first instance of the application under win95, it shows me the KeyDown activity and i get the typed letter but when i run the second instance of the same application, the first one crashes and the second one looses the hook anyone know why and how can i get this to work in multiple application where all would get the message...

i would prefer to use MadCodehook but i don't know how, if possible please let me know how..
Thanks,
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

That code looks strange to me. Is that part of a hook dll? Then why are you referencing Form1.Memo in there? That's definately not available in any other process than yours. If you're not using a hook dll, then you shouldn't call SetWindowsHookEx like that.

Using madCodeHook for a similar purpose might be possible, but really in this case I'd recommend to use SetWindowsHookEx. Just use the right tool for the right task.
cool_tester
Posts: 75
Joined: Sun Oct 31, 2004 5:45 am

Post by cool_tester »

Thanks for the fast reply,
the code is not in a dll hook but it is in a exe and it works fine under Win2k and up, but it causes an issue under win95, even if i remove the form1.memo1.lines.... refrence i still get a problem .... but it works fine if i don't call CallNextHookEx in my code... i wonder why....

even this causes the problem if two instances of the application is started:

Code: Select all

function Record(Code: integer; wParam, lParam: Longint):Longint; stdcall;
begin
  Result := CallNextHookEx(Hook, Code, wParam, lParam);
  case Code of
    HC_ACTION:
  begin

  end;
end;
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Well, I've no idea why that happens.

Maybe this one helps?

http://www.mustangpeak.net/hooks_fix.htm
cool_tester
Posts: 75
Joined: Sun Oct 31, 2004 5:45 am

Post by cool_tester »

i actually figured it out......

the wh_journalrecord is NOT A REALLY IN A HOOK CHAIN, and only one application can Hook at the time.....

and that is why when the second instance hooks the first looses it... too bad now i have to find another Method to detect inactivity.. sine this could be broken if other application are using it....

thanks.,
uall
Posts: 254
Joined: Sun Feb 20, 2005 1:24 pm

Post by uall »

WH_KEYBOARD & WH_MOUSE hook?
cool_tester
Posts: 75
Joined: Sun Oct 31, 2004 5:45 am

Post by cool_tester »

Thanks Uall, that was my next step the only issue with that is it have to be in a DLL which i don't want to do, i want it to be in an EXE File without any DLL's,
Any Ideas, on how to set a global hook without using a DLL in Disk.... maybe patch the DLL in Memory and load it from there is that Possible, kind of like loading the Dll from a Stream instead of Disk..

Thanks.
uall
Posts: 254
Joined: Sun Feb 20, 2005 1:24 pm

Post by uall »

some guys hate me if i post this now:

GetKeyboardState

http://msdn.microsoft.com/library/defau ... dstate.asp

call it every 10 seconds and compare it with old result so u can see if the user did something but dont check for value 255

getcursorpos to see if the cursor is moved
cool_tester
Posts: 75
Joined: Sun Oct 31, 2004 5:45 am

Post by cool_tester »

Thanks i will look into that
Post Reply