Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.0 KiB

  1. title WINHOOK.ASM - SetWindowsHook() and friends
  2. ifdef WOW
  3. NOEXTERNS equ 1
  4. SEGNAME equ <TEXT>
  5. endif
  6. .xlist
  7. include user.inc
  8. .list
  9. swappro = 0
  10. ; include NEW_SEG1 struc used in GetProcModule
  11. include newexe.inc
  12. ExternFP <GetCodeInfo>
  13. ExternFP <GetExePtr>
  14. ExternFP <GetCurrentTask>
  15. ExternFP <SetWindowsHookInternal>
  16. createSeg _%SEGNAME,%SEGNAME,WORD,PUBLIC,CODE
  17. createSeg _DATA,DATA,WORD,PUBLIC,DATA,DGROUP
  18. defgrp DGROUP,DATA
  19. ;===========================================================================
  20. sBegin %SEGNAME
  21. assumes CS,%SEGNAME
  22. assumes DS,DATA
  23. assumes ES,NOTHING
  24. ;============================================================================
  25. ;
  26. ; HANDLE FAR PASCAL GetProcModule(FARPROC lpfn);
  27. ;
  28. ; This function returns the module handle corresponding to a given
  29. ; hook proc address.
  30. ;
  31. ; CAUTION: This uses the undocumented feature of "GetCodeInfo()" that it
  32. ; returns the module handle in ES register.
  33. ;
  34. cProc GetProcModule, <PUBLIC, FAR>
  35. ParmD lpfn
  36. LocalV seginfofromkernel, %size NEW_SEG1+2
  37. cBegin
  38. ; Turns out GetCodeInfo
  39. ;
  40. pushd lpfn ; GetCodeInfo(lpfn, &seginfo)
  41. lea ax,seginfofromkernel
  42. push ss
  43. push ax
  44. call GetCodeInfo
  45. or ax,ax ; AX is BOOL fSuccess (even though windows.h says void)
  46. mov ax,es ; ES contains the module handle according
  47. ; to David Weise...
  48. jnz gpmexit ; We're ok
  49. ; hack. Excel global allocs some memory, puts
  50. ; code into it and passes it to us. The GetCodeInfo fails in this
  51. ; case so we need to get the module handle for a globalalloced
  52. ; chunk of memory.
  53. push word ptr lpfn+2
  54. call GetExePtr
  55. gpmexit:
  56. ifdef DEBUG
  57. or ax,ax
  58. jnz gpm900
  59. DebugErr DBF_ERROR, "Invalid Hook Proc Addr"
  60. xor ax,ax
  61. gpm900:
  62. endif
  63. cEnd
  64. ;==============================================================================
  65. ;
  66. ; FARPROC FAR PASCAL SetWindowsHook(int idHook, FARPROC lpfn)
  67. ; {
  68. ; SetWindowsHookEx2(idHook,
  69. ; (HOOKPROC)lpfn,
  70. ; GetProcModule(lpfn),
  71. ; (idHook == WH_MSGFILTER ? GetCurrentTask() : NULL));
  72. ; }
  73. ;
  74. cProc ISetWindowsHook,<FAR, PUBLIC, LOADDS>
  75. ParmW idHook
  76. ParmD lpfn
  77. LocalW hmodule
  78. LocalW htask
  79. cBegin
  80. ; Check if some apps are trying to unhook a hook using SetWindowsHook()
  81. mov bx, seg_lpfn
  82. cmp bx, HHOOK_MAGIC
  83. jz swhHookMagic
  84. cmp idHook,WH_MSGFILTER
  85. jnz swh10
  86. call GetCurrentTask
  87. jmp swhMakeCall
  88. swhHookMagic:
  89. ; Now, it is clear that this app wants to unhook by calling SetWindowsHook()
  90. ; All Micrographix apps do this trick to unhook their keyboard hooks.
  91. ; Fix for Bug #7972 -- SANKAR -- 05/30/91 --
  92. ; Let us unhook the node passed in thro lpfn;
  93. ifdef DEBUG
  94. DebugErr <DBF_WARNING>, "SetWindowsHook called to unhook: use UnhookWindowsHook"
  95. endif
  96. xor ax,ax
  97. jmps swhMakeCall
  98. swh10:
  99. pushd lpfn
  100. call GetProcModule
  101. swhMakeCall:
  102. push ax
  103. push idHook
  104. pushd lpfn
  105. call SetWindowsHookInternal
  106. swhExit:
  107. cEnd
  108. sEnd %SEGNAME
  109. END