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.

516 lines
11 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. resource.c
  5. Abstract:
  6. Routines that manipulate resources (strings, messages, etc).
  7. Author:
  8. Ted Miller (tedm) 6-Feb-1995
  9. Revision History:
  10. --*/
  11. #include "setupp.h"
  12. #pragma hdrstop
  13. PWSTR
  14. MyLoadString(
  15. IN UINT StringId
  16. )
  17. /*++
  18. Routine Description:
  19. Retrieve a string from the string resources of this module.
  20. Arguments:
  21. StringId - supplies string table identifier for the string.
  22. Return Value:
  23. Pointer to buffer containing string. If the string was not found
  24. or some error occurred retrieving it, this buffer will bne empty.
  25. Caller can free the buffer with MyFree().
  26. If NULL is returned, out of memory.
  27. --*/
  28. {
  29. WCHAR Buffer[4096];
  30. UINT Length;
  31. Length = LoadString(MyModuleHandle,StringId,Buffer,sizeof(Buffer)/sizeof(WCHAR));
  32. if(!Length) {
  33. Buffer[0] = 0;
  34. }
  35. return(pSetupDuplicateString(Buffer));
  36. }
  37. PWSTR
  38. FormatStringMessageV(
  39. IN UINT FormatStringId,
  40. IN va_list *ArgumentList
  41. )
  42. /*++
  43. Routine Description:
  44. Retrieve a string from the string resources of this module and
  45. format it using FormatMessage.
  46. Arguments:
  47. StringId - supplies string table identifier for the string.
  48. ArgumentList - supplies list of strings to be substituted in the
  49. format string.
  50. Return Value:
  51. Pointer to buffer containing formatted message. If the string was not found
  52. or some error occurred retrieving it, this buffer will bne empty.
  53. Caller can free the buffer with MyFree().
  54. If NULL is returned, out of memory.
  55. --*/
  56. {
  57. PWSTR FormatString;
  58. va_list arglist;
  59. PWSTR Message;
  60. PWSTR Return;
  61. DWORD d;
  62. //
  63. // First, load the format string.
  64. //
  65. FormatString = MyLoadString(FormatStringId);
  66. if(!FormatString) {
  67. return(NULL);
  68. }
  69. //
  70. // Now format the message using the arguments the caller passed.
  71. //
  72. d = FormatMessage(
  73. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  74. FormatString,
  75. 0,
  76. 0,
  77. (PWSTR)&Message,
  78. 0,
  79. ArgumentList
  80. );
  81. MyFree(FormatString);
  82. if(!d) {
  83. return(NULL);
  84. }
  85. //
  86. // Make duplicate using our memory system so user can free with MyFree().
  87. //
  88. Return = pSetupDuplicateString(Message);
  89. LocalFree((HLOCAL)Message);
  90. return(Return);
  91. }
  92. PWSTR
  93. FormatStringMessage(
  94. IN UINT FormatStringId,
  95. ...
  96. )
  97. /*++
  98. Routine Description:
  99. Retrieve a string from the string resources of this module and
  100. format it using FormatMessage.
  101. Arguments:
  102. StringId - supplies string table identifier for the string.
  103. Return Value:
  104. Pointer to buffer containing formatted message. If the string was not found
  105. or some error occurred retrieving it, this buffer will bne empty.
  106. Caller can free the buffer with MyFree().
  107. If NULL is returned, out of memory.
  108. --*/
  109. {
  110. va_list arglist;
  111. PWSTR p;
  112. va_start(arglist,FormatStringId);
  113. p = FormatStringMessageV(FormatStringId,&arglist);
  114. va_end(arglist);
  115. return(p);
  116. }
  117. PWSTR
  118. RetrieveAndFormatMessageV(
  119. IN PCWSTR MessageString,
  120. IN UINT MessageId, OPTIONAL
  121. IN va_list *ArgumentList
  122. )
  123. /*++
  124. Routine Description:
  125. Format a message string using a message string and caller-supplied
  126. arguments.
  127. The message id can be either a message in this dll's message table
  128. resources or a win32 error code, in which case a description of
  129. that error is retrieved from the system.
  130. Arguments:
  131. MessageString - supplies the message text. If this value is NULL,
  132. MessageId is used instead
  133. MessageId - supplies message-table identifier or win32 error code
  134. for the message.
  135. ArgumentList - supplies arguments to be inserted in the message text.
  136. Return Value:
  137. Pointer to buffer containing formatted message. If the message was not found
  138. or some error occurred retrieving it, this buffer will bne empty.
  139. Caller can free the buffer with MyFree().
  140. If NULL is returned, out of memory.
  141. --*/
  142. {
  143. DWORD d;
  144. PWSTR Buffer;
  145. PWSTR Message;
  146. WCHAR ModuleName[MAX_PATH];
  147. WCHAR ErrorNumber[24];
  148. PWCHAR p;
  149. PWSTR Args[2];
  150. DWORD Msg_Type;
  151. UINT Msg_Id = MessageId;
  152. if(MessageString > SETUPLOG_USE_MESSAGEID) {
  153. d = FormatMessage(
  154. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  155. MessageString,
  156. 0,
  157. 0,
  158. (PWSTR)&Buffer,
  159. 0,
  160. ArgumentList
  161. );
  162. } else {
  163. if( Msg_Id & 0x0FFF0000 )
  164. Msg_Type = FORMAT_MESSAGE_FROM_SYSTEM; // If the facility bits are set this is still Win32
  165. else{
  166. Msg_Id &= 0x0000FFFF; // Mask out Severity and Facility bits so that we do the right thing
  167. Msg_Type = ((Msg_Id < MSG_FIRST) ? FORMAT_MESSAGE_FROM_SYSTEM : FORMAT_MESSAGE_FROM_HMODULE);
  168. }
  169. d = FormatMessage(
  170. FORMAT_MESSAGE_ALLOCATE_BUFFER | Msg_Type,
  171. (PVOID)MyModuleHandle,
  172. MessageId,
  173. MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
  174. (PWSTR)&Buffer,
  175. 0,
  176. ArgumentList
  177. );
  178. }
  179. if(!d) {
  180. if(GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  181. return(NULL);
  182. }
  183. wsprintf(ErrorNumber,L"%x",MessageId);
  184. Args[0] = ErrorNumber;
  185. Args[1] = ModuleName;
  186. if(MyGetModuleFileName(MyModuleHandle,ModuleName,MAX_PATH)) {
  187. if(p = wcsrchr(ModuleName,L'\\')) {
  188. Args[1] = p+1;
  189. }
  190. } else {
  191. ModuleName[0] = 0;
  192. }
  193. d = FormatMessage(
  194. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  195. NULL,
  196. ERROR_MR_MID_NOT_FOUND,
  197. MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
  198. (PWSTR)&Buffer,
  199. 0,
  200. (va_list *)Args
  201. );
  202. if(!d) {
  203. //
  204. // Give up.
  205. //
  206. return(NULL);
  207. }
  208. }
  209. //
  210. // Make duplicate using our memory system so user can free with MyFree().
  211. //
  212. Message = pSetupDuplicateString(Buffer);
  213. LocalFree((HLOCAL)Buffer);
  214. return(Message);
  215. }
  216. PWSTR
  217. RetrieveAndFormatMessage(
  218. IN PCWSTR MessageString,
  219. IN UINT MessageId, OPTIONAL
  220. ...
  221. )
  222. /*++
  223. Routine Description:
  224. Format a message string using a message string and caller-supplied
  225. arguments.
  226. The message id can be either a message in this dll's message table
  227. resources or a win32 error code, in which case a description of
  228. that error is retrieved from the system.
  229. Arguments:
  230. MessageString - supplies the message text. If this value is NULL,
  231. MessageId is used instead
  232. MessageId - supplies message-table identifier or win32 error code
  233. for the message.
  234. ... - supplies arguments to be inserted in the message text.
  235. Return Value:
  236. Pointer to buffer containing formatted message. If the message was not found
  237. or some error occurred retrieving it, this buffer will bne empty.
  238. Caller can free the buffer with MyFree().
  239. If NULL is returned, out of memory.
  240. --*/
  241. {
  242. va_list arglist;
  243. PWSTR p;
  244. va_start(arglist,MessageId);
  245. p = RetrieveAndFormatMessageV(MessageString,MessageId,&arglist);
  246. va_end(arglist);
  247. return(p);
  248. }
  249. int
  250. MessageBoxFromMessageExV (
  251. IN HWND Owner, OPTIONAL
  252. IN LogSeverity Severity, OPTIONAL
  253. IN PCWSTR MessageString,
  254. IN UINT MessageId, OPTIONAL
  255. IN PCWSTR Caption, OPTIONAL
  256. IN UINT CaptionStringId, OPTIONAL
  257. IN UINT Style,
  258. IN va_list ArgumentList
  259. )
  260. /*++
  261. Routine Description:
  262. Creates a dialog box containing a specified message
  263. Arguments:
  264. Severity - Severity and flags for the error message. Currently only the
  265. flags are significant.
  266. Onwer - handle to parent window
  267. MessageId - ID of message to display
  268. Caption - string to use as caption for dialog box
  269. CaptionStringId - ID of string to use as caption for dialog box (but not
  270. used if Caption is specified)
  271. Style - flags to specify the type of dialog box
  272. ArgumentList - parameters to MessageId
  273. Return Value:
  274. return status from MessageBox
  275. --*/
  276. {
  277. static SETUPLOG_CONTEXT Context = {0};
  278. PCWSTR Message;
  279. PCWSTR Title;
  280. int i;
  281. BOOL b;
  282. if(!Context.AllocMem) {
  283. Context.AllocMem = MyMalloc;
  284. Context.FreeMem = MyFree;
  285. Context.Format = RetrieveAndFormatMessageV;
  286. }
  287. Message = SetuplogFormatMessageWithContextV(
  288. &Context,
  289. Severity,
  290. (PTSTR)MessageString,
  291. MessageId,
  292. &ArgumentList);
  293. b = FALSE;
  294. i = IDOK;
  295. if(Message) {
  296. if(Title = Caption ? Caption : MyLoadString(CaptionStringId)) {
  297. b = TRUE;
  298. i = MessageBox(Owner,Message,Title,Style);
  299. if(Title != Caption) {
  300. MyFree(Title);
  301. }
  302. }
  303. MyFree(Message);
  304. }
  305. if(!b) {
  306. pSetupOutOfMemory(Owner);
  307. }
  308. return(i);
  309. }
  310. int
  311. MessageBoxFromMessageEx (
  312. IN HWND Owner, OPTIONAL
  313. IN LogSeverity Severity, OPTIONAL
  314. IN PCWSTR MessageString,
  315. IN UINT MessageId, OPTIONAL
  316. IN PCWSTR Caption, OPTIONAL
  317. IN UINT CaptionStringId, OPTIONAL
  318. IN UINT Style,
  319. ...
  320. )
  321. /*
  322. Wrapper for MessageBoxFromMessageExV
  323. */
  324. {
  325. va_list ArgumentList;
  326. int Status;
  327. va_start(ArgumentList,Style);
  328. Status = MessageBoxFromMessageExV (
  329. Owner, Severity, MessageString, MessageId, Caption,
  330. CaptionStringId, Style, ArgumentList);
  331. va_end(ArgumentList);
  332. return Status;
  333. }
  334. int
  335. MessageBoxFromMessage(
  336. IN HWND Owner, OPTIONAL
  337. IN UINT MessageId,
  338. IN PCWSTR Caption, OPTIONAL
  339. IN UINT CaptionStringId, OPTIONAL
  340. IN UINT Style,
  341. ...
  342. )
  343. {
  344. PCWSTR Message;
  345. PCWSTR Title;
  346. va_list ArgumentList;
  347. int i;
  348. BOOL b;
  349. va_start(ArgumentList,Style);
  350. Message = RetrieveAndFormatMessageV(NULL,MessageId,&ArgumentList);
  351. va_end(ArgumentList);
  352. b = FALSE;
  353. i = IDOK;
  354. if(Message) {
  355. if(Title = Caption ? Caption : MyLoadString(CaptionStringId)) {
  356. b = TRUE;
  357. i = MessageBox(Owner,Message,Title,Style);
  358. if(Title != Caption) {
  359. MyFree(Title);
  360. }
  361. }
  362. MyFree(Message);
  363. }
  364. if(!b) {
  365. pSetupOutOfMemory(Owner);
  366. }
  367. return(i);
  368. }