Mouse and Keyboard hook problem!

Just starting out? Need help? Post your questions and find answers here.
Joestes
User
User
Posts: 25
Joined: Thu Mar 19, 2009 12:42 pm
Location: Utrecht, The Netherlands

Mouse and Keyboard hook problem!

Post by Joestes »

I've made two DLL's, one to hook the keyboard and one to hook the mouse.

They work both fine on their own, but when I want to use them in the same program, I get an "Invalid memory access error" when I press a key.

Does anyone know how to fix this problem. I guess it has something to do with the way I exit the hook procedure...?

Keyboard hook DLL:

Code: Select all

Global addr.l, KeyboardHook

Procedure Attachprocess(instance)
 CompilerIf Defined(KBDLLHOOKSTRUCT, #PB_Structure) = 0
  Structure KBDLLHOOKSTRUCT
   vkCode.l
   scanCode.l
   flags.l
   time.l
   dwExtraInfo.l
  EndStructure
 CompilerEndIf
EndProcedure

ProcedureDLL KeyboardProc(ncode,wparam,lparam)
 Static position
 
 *keycode.KBDLLHOOKSTRUCT=lparam
 
 If wParam = #WM_RBUTTONDOWN
  k.l=1001
  PokeL(addr,k)
 EndIf
   
 If wParam = #WM_LBUTTONDOWN
  k.l=1002
  PokeL(addr,k)
 EndIf
   
 If wParam = #WM_KEYDOWN
  vk = *keycode\vkCode
  k.l = MapVirtualKeyEx_(vk,2,0) 
  If vk=160
    k = 999
  EndIf
  PokeL(addr,k)
 EndIf
 
 If wParam = #WM_KEYUP
  vk = *keycode\vkCode
  If vk=160
    k = 1000
  Else
    k = 0
  EndIf
  PokeL(addr,k)
 EndIf
 
 ProcedureReturn CallNextHookEx_(KeyboardHook, Code, wParam, lParam )
EndProcedure

ProcedureDLL InitKeyHook(value, hookproc)
 InitMouse()
 addr = value
 KeyboardHook=hookproc
EndProcedure
Mouse hook DLL:

Code: Select all

Global addr.l, MouseHook

Procedure Attachprocess(instance)
 CompilerIf Defined(MSLLHOOKSTRUCT, #PB_Structure) = 0
  Structure MSLLHOOKSTRUCT
    pt.POINT;
    mouseData.l;
    flags.l;
    time.l;
    dwExtraInfo.l;
  EndStructure
 CompilerEndIf
EndProcedure


ProcedureDLL MouseProc(ncode,wparam,lparam)
 Static mMouseInput.MSLLHOOKSTRUCT
 CopyMemory(lparam,@mMouseInput,SizeOf(MSLLHOOKSTRUCT))  
    
 k=0
 If ncode = #HC_ACTION And wParam
  Select wParam
   Case #WM_LBUTTONDOWN
    k=1000
   Case #WM_LBUTTONUP
    k=1001
   Case #WM_RBUTTONDOWN
    k=1002
   Case #WM_RBUTTONUP
    k=1003
   Case #WM_MOUSEMOVE
    k=1004
   Case #WM_MOUSEWHEEL
    k=1005
  EndSelect
 EndIf 
 
 PokeL(addr,k)
 ProcedureReturn CallNextHookEx_(MouseHook, nCode, wParam, lParam )
EndProcedure

ProcedureDLL InitMouseHook(value, hookproc)
 addr = value
 MouseHook=hookproc
EndProcedure
This is how I 'install' the hooks:

Code: Select all

KeyHookLib = OpenLibrary(0,"C:\Documents and Settings\janbeek\Mijn documenten\GetKeyb.dll")
If KeyHookLib
  KeyProc=GetFunction(0, "KeyboardProc")
  KeyboardHook=SetWindowsHookEx_(#WH_KEYBOARD_LL, KeyProc, KeyHookLib, 0)
  CallFunction(0, "InitKeyHook", @AKey, KeyboardHook) 
Else
 End
EndIf

MouseHookLib = OpenLibrary(0,"C:\Documents and Settings\janbeek\Mijn documenten\GetMouse.dll")
If MouseHookLib
  MouseProc=GetFunction(0, "MouseProc")
  MouseHook=SetWindowsHookEx_(#WH_MOUSE_LL, MouseProc, MouseHookLib, 0)
  CallFunction(0, "InitMouseHook", @AMouse, MouseHook)    
Else
  End
EndIf
Thanks!!
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Mouse and Keyboard hook problem!

Post by tinman »

You're using the same library ID when opening the libraries, so your first DLL would get unloaded. I guess that's going to be a problem.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mouse and Keyboard hook problem!

Post by infratec »

And than I miss a 'working example'.

Because when I start your test, it works without fault.

I miss also a define for AKey and AMouse
When I add a repeat forever with a debug AKey, I can see always 0
and I have to reboot my PC, because I come not out of the loop :(

Bernd
Joestes
User
User
Posts: 25
Joined: Thu Mar 19, 2009 12:42 pm
Location: Utrecht, The Netherlands

Re: Mouse and Keyboard hook problem!

Post by Joestes »

tinman wrote:You're using the same library ID when opening the libraries, so your first DLL would get unloaded. I guess that's going to be a problem.
Pfff... You're 100% right, that was the problem... :oops: Thanks!!
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mouse and Keyboard hook problem!

Post by infratec »

The best way is to do it like this:

Code: Select all

KeyHookLib = OpenLibrary(#PB_Any,"GetKeyb.dll")
If KeyHookLib
  KeyProc = GetFunction(KeyHookLib, "KeyboardProc")
  KeyboardHook = SetWindowsHookEx_(#WH_KEYBOARD_LL, KeyProc, LibraryID(KeyHookLib), 0)
  CallFunction(KeyHookLib, "InitKeyHook", @AKey, KeyboardHook)
Else
  End
EndIf
So you are on the safe side.

Btw. there are some strange things inside your code:

Code: Select all

procedure Attachprocess(instance)
for example
And for pointers you should use an *
And you have 2 Globals called Addr

Bernd
Post Reply