vba - Using SetWindowsHookEx. in Excel 2010 -


this function getting me crazy! trying use setwindwoshookex avoid keystrokes user, can't make work properly.

i have been looking around many code on web don't understand why not working me. firstly, because using excel 2010 (64 bit) , code wasn't it, don't know.

basically, have created simple code shows me message when pull "g" happening excel crashes when pull key. doesn't crash when run code step step if pull "g" message appears 3 times!

this code:

#if win64  public declare ptrsafe function unhookwindowshookex lib "user32" (byval hhook longptr) longptr public declare ptrsafe function setwindowshookex lib "user32" alias "setwindowshookexa" (byval idhook longptr, byval lpfn longptr, byval hmod longptr, byval dwthreadid longptr) longprt public declare ptrsafe function callnexthookex lib "user32" (byval hhook longptr, byval ncode longptr, byval wparam longptr, lparam any) longptr public declare ptrsafe function getmodulehandle lib "kernel32" alias "getmodulehandlea" (byval lpmodulename string) longptr public declare ptrsafe function getcurrentthreadid lib "kernel32" () longptr public declare ptrsafe function getkeystate lib "user32" (byval nvirtkey longptr) integer private hwndppt longptr private hookhandle longptr  'adicional private declare function senddlgitemmessage lib "user32" alias "senddlgitemmessagea" (byval hdlg longprt, byval niddlgitem longptr, byval wmsg longptr, byval wparam longptr, byval lparam longptr) longptr private declare function getclassname lib "user32" alias "getclassnamea" (byval hwnd longptr, byval lpclassname string, byval nmaxcount longptr) longptr    #else public declare function unhookwindowshookex lib "user32" (byval hhook long) long public declare function setwindowshookex lib "user32" alias "setwindowshookexa" (byval idhook long, byval lpfn long, byval hmod long, byval dwthreadid long) long public declare function callnexthookex lib "user32" (byval hhook long, byval ncode long, byval wparam long, lparam any) long public declare function getmodulehandle lib "kernel32" alias "getmodulehandlea" (byval lpmodulename string) long public declare function getcurrentthreadid lib "kernel32" () long public declare function getkeystate lib "user32" (byval nvirtkey long) integer private hwndppt long private hookhandle long  'adicional private declare function senddlgitemmessage lib "user32" alias "senddlgitemmessagea" (byval hdlg long, byval niddlgitem long, byval wmsg long, byval wparam long, byval lparam long) long private declare function getclassname lib "user32" alias "getclassnamea" (byval hwnd long, byval lpclassname string, byval nmaxcount long) long  #end if    'constants used in our api functions 'private const em_setpasswordchar = &hcc 'private const wh_cbt = 5 private const wh_keyboard = 2 'private const hcbt_activate = 5 private const hc_action = 0  'private hhook long   public sub removehook()     unhookwindowshookex (hookhandle) end sub  sub sethook() #if win64 dim lthreadid longptr dim lngmodhwnd longptr #else dim lthreadid long dim lngmodhwnd long #end if  lthreadid = getcurrentthreadid lngmodhwnd = getmodulehandle(vbnullstring)  'set local hook hookhandle = setwindowshookex(wh_keyboard, addressof newproc, 0, lthreadid) end sub  public function newproc(byval lngcode longptr, byval wparam longptr, byval lparam longptr) longptr      if lngcode < hc_action       newproc = callnexthookex(hookhandle, lngcode, wparam, lparam)       exit function     end if      if wparam = 71         'msgbox "g"         'newproc = 1         wparam = 70         'exit function     end if      'this line ensure other hooks may in place     'called correctly.     callnexthookex hookhandle, lngcode, wparam, lparam  end function 

the correct declarations 64bit be:

public declare ptrsafe function unhookwindowshookex lib "user32" (byval hhook longptr) long public declare ptrsafe function setwindowshookex lib "user32" alias "setwindowshookexa" (byval idhook long, byval lpfn longptr, byval hmod longptr, byval dwthreadid long) longptr public declare ptrsafe function callnexthookex lib "user32" (byval hhook longptr, byval ncode long, byval wparam longptr, lparam any) longptr public declare ptrsafe function getmodulehandle lib "kernel32" alias "getmodulehandlea" (byval lpmodulename string) longptr public declare ptrsafe function getcurrentthreadid lib "kernel32" () long public declare ptrsafe function getkeystate lib "user32" (byval nvirtkey long) integer private declare function senddlgitemmessage lib "user32" alias "senddlgitemmessagea" (byval hdlg longptr, byval niddlgitem long, byval wmsg long, byval wparam longptr, byval lparam longptr) longptr private declare function getclassname lib "user32" alias "getclassnamea" (byval hwnd longptr, byval lpclassname string, byval nmaxcount long) long 

i can't see how code posted run @ on 64bit.


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -