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.

245 lines
6.2 KiB

  1. /* File: C:\WACKER\TDLL\error_box.c (Created: 22-Dec-1993)
  2. *
  3. * Copyright 1990,1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 10 $
  7. * $Date: 4/17/02 10:00a $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. #include "stdtyp.h"
  12. #include "assert.h"
  13. #include "tdll.h"
  14. #include "htchar.h"
  15. #include "globals.h"
  16. #include "errorbox.h"
  17. #include "misc.h"
  18. #include <term\res.h>
  19. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  20. * Global Variables:
  21. *
  22. * These global variables are used to tell if the timer expored. At some
  23. * future time it may become necessary to lock them, but not yet.
  24. */
  25. #define TMB_IDLE 0
  26. #define TMB_ACTIVE 1
  27. #define TMB_EXPIRED 2
  28. static int nState = TMB_IDLE;
  29. static LPCTSTR pszMsgTitle;
  30. static HWND hwndMsgOwner;
  31. static BOOL CALLBACK TMTPproc(HWND hwnd, LPARAM lP);
  32. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  33. * FUNCTION:
  34. * TimedMessageTimerProc
  35. *
  36. * DESCRIPTION:
  37. * This is the function that is passed to SetTimer when TimedMessageBox is
  38. * called.
  39. *
  40. * PARAMETERS:
  41. * As per the Win32 documentation.
  42. *
  43. * RETURNS:
  44. * As per the Win32 documentation.
  45. */
  46. VOID CALLBACK TimedMessageTimerProc(HWND hwnd,
  47. UINT uMsg,
  48. UINT_PTR idEvent,
  49. DWORD dwTime)
  50. {
  51. // EnumThreadWindows(GetWindowThreadProcessId(hwndMsgOwner, NULL),
  52. // TMTPproc, 0);
  53. /*
  54. * This works, but it is a very chancy thing to be doing.
  55. * TODO: figure out a better way to kill the sucker.
  56. */
  57. EnumWindows(TMTPproc, 0);
  58. }
  59. static BOOL CALLBACK TMTPproc(HWND hwnd, LPARAM lP)
  60. {
  61. TCHAR cBuffer[128];
  62. cBuffer[0] = TEXT('\0');
  63. /* Get the title of the window */
  64. GetWindowText(hwnd, cBuffer, 128);
  65. /* Compare to what we are looking for */
  66. if (StrCharCmp(cBuffer, pszMsgTitle) == 0)
  67. {
  68. /* TODO: remove this after debugging is done */
  69. mscMessageBeep(MB_ICONHAND);
  70. nState = TMB_EXPIRED;
  71. /* Take that, you rogue window ! */
  72. PostMessage(hwnd, WM_CLOSE, 0, 0);
  73. return FALSE;
  74. }
  75. return TRUE;
  76. }
  77. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  78. * FUNCTION:
  79. * TimedMessageBox
  80. *
  81. * DESCRIPTION:
  82. * This function is a replacement for MessageBox. It has the added feature
  83. * of having a timeout feature. The timeout feature is necessary for any
  84. * type of HOST mode or script feature.
  85. *
  86. * PARAMETERS:
  87. * The parameters are the same as those passed to MessageBox plus our own
  88. * timeout value. If the timeout value is greater than ZERO, then the
  89. * timeout feature is active. If it is less than or equal to ZERO, the
  90. * timeout feature is disabled.
  91. *
  92. * RETURNS:
  93. * This function returns the usual MessageBox return values. It can also
  94. * return a new value for a timeout. Each instance of calling this box
  95. * should take care to handle the timeout return in an useful manner.
  96. *
  97. * For now, the timeout return value is set to (-1)
  98. */
  99. int TimedMessageBox(HWND hwndOwner,
  100. LPCTSTR lpszText,
  101. LPCTSTR lpszTitle,
  102. UINT fuStyle,
  103. int nTimeout)
  104. {
  105. int nRet = 0;
  106. UINT_PTR uTimer = 0;
  107. TCHAR acTitle[256];
  108. LPTSTR pTitle = (LPTSTR)lpszTitle;
  109. if (lpszText == NULL || StrCharGetStrLength(lpszText) == 0)
  110. {
  111. UINT uiBeep;
  112. if (fuStyle & MB_ICONASTERISK)
  113. {
  114. uiBeep = MB_ICONASTERISK;
  115. }
  116. else if (fuStyle & MB_ICONEXCLAMATION)
  117. {
  118. uiBeep = MB_ICONEXCLAMATION;
  119. }
  120. else if (fuStyle & MB_ICONHAND)
  121. {
  122. uiBeep = MB_ICONHAND;
  123. }
  124. else if (fuStyle & MB_ICONQUESTION)
  125. {
  126. uiBeep = MB_ICONQUESTION;
  127. }
  128. else if(fuStyle & MB_OK)
  129. {
  130. uiBeep = MB_OK;
  131. }
  132. else
  133. {
  134. uiBeep = -1;
  135. }
  136. mscMessageBeep(uiBeep);
  137. return nRet;
  138. }
  139. if ((lpszTitle == 0 || StrCharGetStrLength(lpszTitle) == 0) &&
  140. LoadString(glblQueryDllHinst(), IDS_MB_TITLE_ERR, acTitle, 256) != 0)
  141. {
  142. pTitle = acTitle;
  143. }
  144. /*
  145. * A small hack because of the way the timeout stuff works.
  146. * TODO: get this to work better.
  147. */
  148. if (nTimeout > 0)
  149. {
  150. nTimeout *= 1000L; /* Convert seconds to milliseconds */
  151. /* TODO: put something more unique into the title for ID purposes */
  152. /* something like the parent window handle or similar */
  153. pszMsgTitle = lpszTitle; /* Used to ID the window */
  154. hwndMsgOwner = hwndOwner;
  155. nState = TMB_ACTIVE;
  156. if ((uTimer = SetTimer(NULL, 0, nTimeout, TimedMessageTimerProc)) == 0)
  157. {
  158. assert(FALSE);
  159. /* Return failure */
  160. return nRet;
  161. }
  162. }
  163. fuStyle |= MB_SETFOREGROUND;
  164. // TODO: May have to use MessageBoxEx() which provides a way of including
  165. // a language specification so that pre-defined buttons appear with the
  166. // correct language on them, OR MessageBoxIndirect() which allows for a
  167. // definition of a hook-proc which can process the HELP messages - jac.
  168. //
  169. // REV: 3/27/2002 -- The following is a clipping from the MSDN documentation:
  170. //
  171. // The MessageBox function creates, displays, and operates a message box.
  172. // The message box contains an application-defined message and title, plus
  173. // any combination of predefined icons and push buttons.
  174. //
  175. // The MessageBoxEx function creates, displays, and operates a message box.
  176. // The message box contains an application-defined message and title, plus
  177. // any combination of predefined icons and push buttons. The buttons are in
  178. // the language of the system user interface.
  179. //
  180. // Currently MessageBoxEx and MessageBox work the same way.
  181. //
  182. // TODO: We still need to make sure the sounds are transmitted when running
  183. // in a Terminal Service session (Remote Desktop Connection) when the sound
  184. // is anything other than -1.
  185. //
  186. nRet = MessageBox(hwndOwner,
  187. lpszText,
  188. pTitle,
  189. fuStyle);
  190. switch (nState)
  191. {
  192. case TMB_ACTIVE:
  193. /*
  194. * Everything is OK, no problem
  195. */
  196. if (uTimer != 0)
  197. {
  198. KillTimer(NULL, uTimer);
  199. }
  200. break;
  201. case TMB_EXPIRED:
  202. /*
  203. * Timer expired and killed MessageBox
  204. */
  205. nRet = (-1);
  206. if (uTimer != 0)
  207. {
  208. KillTimer(NULL, uTimer);
  209. }
  210. break;
  211. case TMB_IDLE:
  212. default:
  213. if (uTimer != 0)
  214. {
  215. KillTimer(NULL, uTimer);
  216. }
  217. break;
  218. }
  219. nState = TMB_IDLE;
  220. return nRet;
  221. }