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.

283 lines
8.7 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright (c) 1994-1998 Microsoft Corporation
  4. //*********************************************************************
  5. //
  6. // UTIL.C - common utility functions
  7. //
  8. // HISTORY:
  9. //
  10. // 12/21/94 jeremys Created.
  11. // 96/03/24 markdu Replaced memset with ZeroMemory for consistency.
  12. // 96/04/06 markdu NASH BUG 15653 Use exported autodial API.
  13. // Need to keep a modified SetInternetConnectoid to set the
  14. // MSN backup connectoid.
  15. // 96/05/14 markdu NASH BUG 21706 Removed BigFont functions.
  16. //
  17. #include "wizard.h"
  18. #if 0
  19. #include "string.h"
  20. #endif
  21. #include "winver.h"
  22. // function prototypes
  23. VOID _cdecl FormatErrorMessage(CHAR * pszMsg,DWORD cbMsg,CHAR * pszFmt,LPSTR szArg);
  24. extern GETSETUPXERRORTEXT lpGetSETUPXErrorText;
  25. /*******************************************************************
  26. NAME: MsgBox
  27. SYNOPSIS: Displays a message box with the specified string ID
  28. ********************************************************************/
  29. int MsgBox(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons)
  30. {
  31. CHAR szMsgBuf[MAX_RES_LEN+1];
  32. CHAR szSmallBuf[SMALL_BUF_LEN+1];
  33. LoadSz(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  34. LoadSz(nMsgID,szMsgBuf,sizeof(szMsgBuf));
  35. return (MessageBox(hWnd,szMsgBuf,szSmallBuf,uIcon | uButtons));
  36. }
  37. /*******************************************************************
  38. NAME: MsgBoxSz
  39. SYNOPSIS: Displays a message box with the specified text
  40. ********************************************************************/
  41. int MsgBoxSz(HWND hWnd,LPSTR szText,UINT uIcon,UINT uButtons)
  42. {
  43. CHAR szSmallBuf[SMALL_BUF_LEN+1];
  44. LoadSz(IDS_APPNAME,szSmallBuf,sizeof(szSmallBuf));
  45. return (MessageBox(hWnd,szText,szSmallBuf,uIcon | uButtons));
  46. }
  47. /*******************************************************************
  48. NAME: MsgBoxParam
  49. SYNOPSIS: Displays a message box with the specified string ID
  50. NOTES: //extra parameters are string pointers inserted into nMsgID.
  51. jmazner 11/6/96 For RISC compatability, we don't want
  52. to use va_list; since current source code never uses more than
  53. one string parameter anyways, just change function signature
  54. to explicitly include that one parameter.
  55. ********************************************************************/
  56. int _cdecl MsgBoxParam(HWND hWnd,UINT nMsgID,UINT uIcon,UINT uButtons,LPSTR szParam)
  57. {
  58. BUFFER Msg(3*MAX_RES_LEN+1); // nice n' big for room for inserts
  59. BUFFER MsgFmt(MAX_RES_LEN+1);
  60. //va_list args;
  61. if (!Msg || !MsgFmt) {
  62. return MsgBox(hWnd,IDS_ERROutOfMemory,MB_ICONSTOP,MB_OK);
  63. }
  64. LoadSz(nMsgID,MsgFmt.QueryPtr(),MsgFmt.QuerySize());
  65. //va_start(args,uButtons);
  66. //FormatErrorMessage(Msg.QueryPtr(),Msg.QuerySize(),
  67. // MsgFmt.QueryPtr(),args);
  68. FormatErrorMessage(Msg.QueryPtr(),Msg.QuerySize(),
  69. MsgFmt.QueryPtr(),szParam);
  70. return MsgBoxSz(hWnd,Msg.QueryPtr(),uIcon,uButtons);
  71. }
  72. /*******************************************************************
  73. NAME: LoadSz
  74. SYNOPSIS: Loads specified string resource into buffer
  75. EXIT: returns a pointer to the passed-in buffer
  76. NOTES: If this function fails (most likely due to low
  77. memory), the returned buffer will have a leading NULL
  78. so it is generally safe to use this without checking for
  79. failure.
  80. ********************************************************************/
  81. LPSTR LoadSz(UINT idString,LPSTR lpszBuf,UINT cbBuf)
  82. {
  83. ASSERT(lpszBuf);
  84. // Clear the buffer and load the string
  85. if ( lpszBuf )
  86. {
  87. *lpszBuf = '\0';
  88. LoadString( ghInstance, idString, lpszBuf, cbBuf );
  89. }
  90. return lpszBuf;
  91. }
  92. /*******************************************************************
  93. NAME: GetErrorDescription
  94. SYNOPSIS: Retrieves the text description for a given error code
  95. and class of error (standard, setupx)
  96. ********************************************************************/
  97. VOID GetErrorDescription(CHAR * pszErrorDesc,UINT cbErrorDesc,
  98. UINT uError,UINT uErrorClass)
  99. {
  100. ASSERT(pszErrorDesc);
  101. // set a leading null in error description
  102. *pszErrorDesc = '\0';
  103. switch (uErrorClass) {
  104. case ERRCLS_STANDARD:
  105. if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,
  106. uError,0,pszErrorDesc,cbErrorDesc,NULL)) {
  107. // if getting system text fails, make a string a la
  108. // "error <n> occurred"
  109. CHAR szFmt[SMALL_BUF_LEN+1];
  110. LoadSz(IDS_ERRFORMAT,szFmt,sizeof(szFmt));
  111. wsprintf(pszErrorDesc,szFmt,uError);
  112. }
  113. break;
  114. case ERRCLS_SETUPX:
  115. lpGetSETUPXErrorText(uError,pszErrorDesc,cbErrorDesc);
  116. break;
  117. default:
  118. DEBUGTRAP("Unknown error class %lu in GetErrorDescription",
  119. uErrorClass);
  120. }
  121. }
  122. /*******************************************************************
  123. NAME: FormatErrorMessage
  124. SYNOPSIS: Builds an error message by calling FormatMessage
  125. NOTES: Worker function for DisplayErrorMessage
  126. ********************************************************************/
  127. VOID _cdecl FormatErrorMessage(CHAR * pszMsg,DWORD cbMsg,CHAR * pszFmt,LPSTR szArg)
  128. {
  129. ASSERT(pszMsg);
  130. ASSERT(pszFmt);
  131. // build the message into the pszMsg buffer
  132. DWORD dwCount = FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  133. pszFmt,0,0,pszMsg,cbMsg,(va_list*) &szArg);
  134. ASSERT(dwCount > 0);
  135. }
  136. /*******************************************************************
  137. NAME: DisplayErrorMessage
  138. SYNOPSIS: Displays an error message for given error
  139. ENTRY: hWnd - parent window
  140. uStrID - ID of string resource with message format.
  141. Should contain %1 to be replaced by error text,
  142. additional parameters can be specified as well.
  143. uError - error code for error to display
  144. uErrorClass - ERRCLS_xxx ID of class of error that
  145. uError belongs to (standard, setupx)
  146. uIcon - icon to display
  147. //... - additional parameters to be inserted in string
  148. // specified by uStrID
  149. jmazner 11/6/96 change to just one parameter for
  150. RISC compatability.
  151. ********************************************************************/
  152. VOID _cdecl DisplayErrorMessage(HWND hWnd,UINT uStrID,UINT uError,
  153. UINT uErrorClass,UINT uIcon,LPSTR szArg)
  154. {
  155. // dynamically allocate buffers for messages
  156. BUFFER ErrorDesc(MAX_RES_LEN+1);
  157. BUFFER ErrorFmt(MAX_RES_LEN+1);
  158. BUFFER ErrorMsg(2*MAX_RES_LEN+1);
  159. if (!ErrorDesc || !ErrorFmt || !ErrorMsg) {
  160. // if can't allocate buffers, display out of memory error
  161. MsgBox(hWnd,IDS_ERROutOfMemory,MB_ICONEXCLAMATION,MB_OK);
  162. return;
  163. }
  164. // get a text description based on the error code and the class
  165. // of error it is
  166. GetErrorDescription(ErrorDesc.QueryPtr(),
  167. ErrorDesc.QuerySize(),uError,uErrorClass);
  168. // load the string for the message format
  169. LoadSz(uStrID,ErrorFmt.QueryPtr(),ErrorFmt.QuerySize());
  170. //LPSTR args[MAX_MSG_PARAM];
  171. //args[0] = (LPSTR) ErrorDesc.QueryPtr();
  172. //memcpy(&args[1],((CHAR *) &uIcon) + sizeof(uIcon),(MAX_MSG_PARAM - 1) * sizeof(LPSTR));
  173. //FormatErrorMessage(ErrorMsg.QueryPtr(),ErrorMsg.QuerySize(),
  174. // ErrorFmt.QueryPtr(),(va_list) &args[0]);
  175. FormatErrorMessage(ErrorMsg.QueryPtr(),ErrorMsg.QuerySize(),
  176. ErrorFmt.QueryPtr(),ErrorDesc.QueryPtr());
  177. // display the message
  178. MsgBoxSz(hWnd,ErrorMsg.QueryPtr(),uIcon,MB_OK);
  179. }
  180. /*******************************************************************
  181. NAME: MsgWaitForMultipleObjectsLoop
  182. SYNOPSIS: Blocks until the specified object is signaled, while
  183. still dispatching messages to the main thread.
  184. ********************************************************************/
  185. DWORD MsgWaitForMultipleObjectsLoop(HANDLE hEvent)
  186. {
  187. MSG msg;
  188. DWORD dwObject;
  189. while (1)
  190. {
  191. // NB We need to let the run dialog become active so we have to half handle sent
  192. // messages but we don't want to handle any input events or we'll swallow the
  193. // type-ahead.
  194. dwObject = MsgWaitForMultipleObjects(1, &hEvent, FALSE,INFINITE, QS_ALLINPUT);
  195. // Are we done waiting?
  196. switch (dwObject) {
  197. case WAIT_OBJECT_0:
  198. case WAIT_FAILED:
  199. return dwObject;
  200. case WAIT_OBJECT_0 + 1:
  201. // got a message, dispatch it and wait again
  202. while (PeekMessage(&msg, NULL,0, 0, PM_REMOVE)) {
  203. DispatchMessage(&msg);
  204. }
  205. break;
  206. }
  207. }
  208. // never gets here
  209. }