Leaked source code of windows server 2003
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.

179 lines
4.2 KiB

  1. #include "precomp.h"
  2. //
  3. // IM.CPP
  4. // Input Manager (controlling) Code
  5. //
  6. // Copyright(c) Microsoft 1997-
  7. //
  8. //
  9. // FUNCTION: OSI_InstallHighLevelMouseHook
  10. //
  11. // DESCRIPTION:
  12. //
  13. // This function installs the IM high level mouse hook. The mouse hook is
  14. // used to block remote mouse input to non-hosted apps.
  15. //
  16. // PARAMETERS: None.
  17. //
  18. //
  19. BOOL WINAPI OSI_InstallHighLevelMouseHook(BOOL fEnable)
  20. {
  21. BOOL rc = TRUE;
  22. DebugEntry(OSI_InstallHighLevelMouseHook);
  23. if (fEnable)
  24. {
  25. //
  26. // Check the hook is already installed. This is quite possible.
  27. //
  28. if (g_imMouseHook)
  29. {
  30. TRACE_OUT(( "Mouse hook installed already"));
  31. }
  32. else
  33. {
  34. //
  35. // Install the mouse hook
  36. //
  37. g_imMouseHook = SetWindowsHookEx(WH_MOUSE, IMMouseHookProc,
  38. g_hookInstance, 0);
  39. if (!g_imMouseHook)
  40. {
  41. ERROR_OUT(("Failed to install mouse hook"));
  42. rc = FALSE;
  43. }
  44. }
  45. }
  46. else
  47. {
  48. //
  49. // Check the hook is already removed. This is quite possible.
  50. //
  51. if (!g_imMouseHook)
  52. {
  53. TRACE_OUT(("Mouse hook not installed"));
  54. }
  55. else
  56. {
  57. //
  58. // Remove the mouse hook
  59. //
  60. UnhookWindowsHookEx(g_imMouseHook);
  61. g_imMouseHook = NULL;
  62. }
  63. }
  64. DebugExitBOOL(OSI_InstallHighLevelMouseHook, rc);
  65. return(rc);
  66. }
  67. //
  68. // FUNCTION: IMMouseHookProc
  69. //
  70. // DESCRIPTION:
  71. //
  72. //
  73. // PARAMETERS:
  74. //
  75. // See MouseProc documentation
  76. //
  77. // RETURNS:
  78. //
  79. // See MouseProc documentation (FALSE - allow event through, TRUE - discard
  80. // event)
  81. //
  82. //
  83. LRESULT CALLBACK IMMouseHookProc(int code,
  84. WPARAM wParam,
  85. LPARAM lParam)
  86. {
  87. LRESULT rc;
  88. BOOL block = FALSE;
  89. BOOL fShared;
  90. PMOUSEHOOKSTRUCT lpMseHook = (PMOUSEHOOKSTRUCT) lParam;
  91. DebugEntry(IMMouseHookProc);
  92. if (code < 0)
  93. {
  94. //
  95. // Pass the hook on if the code is negative (Windows hooking
  96. // protocol).
  97. //
  98. DC_QUIT;
  99. }
  100. //
  101. // Now decide if we should block this event. We will block this event
  102. // if it is not destined for a hosted window.
  103. //
  104. // Note that on NT screensavers run in a different desktop. We do not
  105. // journalrecord (and can't for winlogon/security reasons) on that
  106. // desktop and therefore never will see an HWND that is a screensaver.
  107. //
  108. fShared = HET_WindowIsHosted(lpMseHook->hwnd);
  109. if (wParam == WM_LBUTTONDOWN)
  110. g_fLeftDownOnShared = fShared;
  111. //
  112. // If this is some kind of mouse message to a window that isn't shared,
  113. // check if the window is the OLE32 dragdrop dude.
  114. //
  115. if (!fShared && g_fLeftDownOnShared)
  116. {
  117. TCHAR szName[HET_CLASS_NAME_SIZE];
  118. if (::GetClassName(lpMseHook->hwnd, szName, CCHMAX(szName)) &&
  119. !lstrcmpi(szName, HET_OLEDRAGDROP_CLASS) &&
  120. (::GetCapture() == lpMseHook->hwnd))
  121. {
  122. //
  123. // Note side-effect of this:
  124. // Mouse moves over non-shared areas when in OLE drag drop mode
  125. // WILL be passed on to non-shared window.
  126. //
  127. // But that's way better than it not working at all.
  128. //
  129. WARNING_OUT(("NMASNT: Hacking OLE drag drop; left click down on shared window then OLE took capture"));
  130. fShared = TRUE;
  131. }
  132. }
  133. block = !fShared;
  134. TRACE_OUT(("MOUSEHOOK: hwnd %08lx -> block: %s",
  135. lpMseHook->hwnd,
  136. block ? "YES" : "NO"));
  137. DC_EXIT_POINT:
  138. //
  139. // Call the next hook.
  140. //
  141. rc = CallNextHookEx(g_imMouseHook, code, wParam, lParam);
  142. if (block)
  143. {
  144. //
  145. // We want to block this event so return a non-zero value.
  146. //
  147. rc = 1;
  148. }
  149. DebugExitDWORD(IMMouseHookProc, (DWORD)rc);
  150. return(rc);
  151. }
  152.