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.

634 lines
14 KiB

  1. /*++
  2. Copyright (c) 1993-1998 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 "precomp.h"
  12. #pragma hdrstop
  13. VOID
  14. SetDlgText(
  15. IN HWND hwndDlg,
  16. IN INT iControl,
  17. IN UINT nStartString,
  18. IN UINT nEndString
  19. )
  20. /*++
  21. Routine Description:
  22. This routine concatenates a number of string resources and does a
  23. SetWindowText() for a dialog text control.
  24. Arguments:
  25. hwndDlg - Handle to dialog window
  26. iControl - Dialog control ID to receive text
  27. nStartString - ID of first string resource to concatenate
  28. nEndString - ID of last string resource to concatenate
  29. Return Value:
  30. None.
  31. Remarks:
  32. String IDs must be consecutive.
  33. --*/
  34. {
  35. TCHAR StringBuffer[SDT_MAX_TEXT];
  36. UINT i;
  37. INT Len = 0;
  38. for(i = nStartString;
  39. ((i <= nEndString) && (Len < (SDT_MAX_TEXT - 1)));
  40. i++)
  41. {
  42. Len += LoadString(MyDllModuleHandle,
  43. i,
  44. StringBuffer + Len,
  45. SDT_MAX_TEXT - Len
  46. );
  47. }
  48. if(!Len) {
  49. StringBuffer[0] = TEXT('\0');
  50. }
  51. SetDlgItemText(hwndDlg, iControl, StringBuffer);
  52. }
  53. PTSTR
  54. MyLoadString(
  55. IN UINT StringId
  56. )
  57. /*++
  58. Routine Description:
  59. Retreive a string from the string resources of this module.
  60. Arguments:
  61. StringId - supplies string table identifier for the string.
  62. Return Value:
  63. Pointer to buffer containing string. If the string was not found
  64. or some error occurred retreiving it, this buffer will be empty.
  65. Caller can free the buffer with MyFree().
  66. If NULL is returned, out of memory.
  67. --*/
  68. {
  69. PTSTR Buffer, p;
  70. int Length, RequiredLength;
  71. //
  72. // Start out with a reasonably-sized buffer so that we'll rarely need to
  73. // grow the buffer and retry (Length is in terms of characters, not bytes).
  74. //
  75. Length = LINE_LEN;
  76. while(TRUE) {
  77. Buffer = MyMalloc(Length * sizeof(TCHAR));
  78. if(!Buffer) {
  79. return NULL;
  80. }
  81. RequiredLength = LoadString(MyDllModuleHandle,
  82. StringId,
  83. Buffer,
  84. Length
  85. );
  86. if(!RequiredLength) {
  87. *Buffer = TEXT('\0');
  88. Length = 1;
  89. break;
  90. }
  91. //
  92. // Because of the way LoadString works, there's no way to
  93. // tell for sure whether your buffer was big enough in the case where
  94. // the length returned just fits in the buffer you supplied (the API
  95. // silently truncates in this case). Thus, if RequiredLength is exactly
  96. // the size of our supplied buffer (minus terminating null, which
  97. // LoadString doesn't count), we increase the buffer size by LINE_LEN
  98. // characters and try again, to make sure we get the whole string.
  99. //
  100. if(RequiredLength < (Length - 1)) {
  101. //
  102. // Looks like we got the whole string. Set the length to be the
  103. // required length + 1 character, to accommodate the terminating
  104. // null character.
  105. //
  106. Length = RequiredLength + 1;
  107. break;
  108. } else {
  109. MyFree(Buffer);
  110. Length += LINE_LEN;
  111. }
  112. }
  113. //
  114. // Resize the buffer to its correct size. If this fails (which it shouldn't)
  115. // it's no big deal, it just means we're using a larger buffer for this string
  116. // than we need to.
  117. //
  118. if(p = MyRealloc(Buffer, Length * sizeof(TCHAR))) {
  119. Buffer = p;
  120. }
  121. return Buffer;
  122. }
  123. PTSTR
  124. FormatStringMessageV(
  125. IN UINT FormatStringId,
  126. IN va_list *ArgumentList
  127. )
  128. /*++
  129. Routine Description:
  130. Retreive a string from the string resources of this module and
  131. format it using FormatMessage.
  132. Arguments:
  133. StringId - supplies string table identifier for the string.
  134. ArgumentList - supplies list of strings to be substituted in the
  135. format string.
  136. Return Value:
  137. Pointer to buffer containing formatted message. If the string was not found
  138. or some error occurred retreiving it, this buffer will be empty.
  139. Caller can free the buffer with MyFree().
  140. If NULL is returned, out of memory.
  141. --*/
  142. {
  143. PTSTR FormatString;
  144. va_list arglist;
  145. PTSTR Message;
  146. PTSTR Return;
  147. DWORD d;
  148. //
  149. // First, load the format string.
  150. //
  151. FormatString = MyLoadString(FormatStringId);
  152. if(!FormatString) {
  153. return(NULL);
  154. }
  155. //
  156. // Now format the message using the arguements the caller passed.
  157. //
  158. d = FormatMessage(
  159. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  160. FormatString,
  161. 0,
  162. 0,
  163. (PTSTR)&Message,
  164. 0,
  165. ArgumentList
  166. );
  167. MyFree(FormatString);
  168. if(!d) {
  169. return(NULL);
  170. }
  171. //
  172. // Make duplicate using our memory system so user can free with MyFree().
  173. //
  174. Return = DuplicateString(Message);
  175. LocalFree((HLOCAL)Message);
  176. return(Return);
  177. }
  178. PTSTR
  179. FormatStringMessage(
  180. IN UINT FormatStringId,
  181. ...
  182. )
  183. /*++
  184. Routine Description:
  185. Retreive a string from the string resources of this module and
  186. format it using FormatMessage.
  187. Arguments:
  188. StringId - supplies string table identifier for the string.
  189. Return Value:
  190. Pointer to buffer containing formatted message. If the string was not found
  191. or some error occurred retreiving it, this buffer will be empty.
  192. Caller can free the buffer with MyFree().
  193. If NULL is returned, out of memory.
  194. --*/
  195. {
  196. va_list arglist;
  197. PTSTR p;
  198. va_start(arglist,FormatStringId);
  199. p = FormatStringMessageV(FormatStringId,&arglist);
  200. va_end(arglist);
  201. return(p);
  202. }
  203. PTSTR
  204. FormatStringMessageFromStringV(
  205. IN PTSTR FormatString,
  206. IN va_list *ArgumentList
  207. )
  208. /*++
  209. Routine Description:
  210. Format the input string using FormatMessage.
  211. Arguments:
  212. FormatString - supplies the format string.
  213. ArgumentList - supplies list of strings to be substituted in the
  214. format string.
  215. Return Value:
  216. Pointer to buffer containing formatted message. If some error occurred
  217. formatting the string, this buffer will be empty.
  218. Caller can free the buffer with MyFree().
  219. If NULL is returned, out of memory.
  220. --*/
  221. {
  222. va_list arglist;
  223. PTSTR Message;
  224. PTSTR Return;
  225. DWORD d;
  226. //
  227. // Format the message using the arguements the caller passed.
  228. //
  229. d = FormatMessage(
  230. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  231. FormatString,
  232. 0,
  233. 0,
  234. (PTSTR)&Message,
  235. 0,
  236. ArgumentList
  237. );
  238. if(!d) {
  239. return(NULL);
  240. }
  241. //
  242. // Make duplicate using our memory system so user can free with MyFree().
  243. //
  244. Return = DuplicateString(Message);
  245. LocalFree((HLOCAL)Message);
  246. return(Return);
  247. }
  248. PTSTR
  249. FormatStringMessageFromString(
  250. IN PTSTR FormatString,
  251. ...
  252. )
  253. /*++
  254. Routine Description:
  255. Format the input string using FormatMessage.
  256. Arguments:
  257. FormatString - supplies the format string.
  258. Return Value:
  259. Pointer to buffer containing formatted message. If some error occurred
  260. formatting the string, this buffer will be empty.
  261. Caller can free the buffer with MyFree().
  262. If NULL is returned, out of memory.
  263. --*/
  264. {
  265. va_list arglist;
  266. PTSTR p;
  267. va_start(arglist,FormatString);
  268. p = FormatStringMessageFromStringV(FormatString,&arglist);
  269. va_end(arglist);
  270. return(p);
  271. }
  272. INT
  273. FormatMessageBox(
  274. IN HANDLE hinst,
  275. IN HWND hwndParent,
  276. IN UINT TextMessageId,
  277. IN PCTSTR Title,
  278. IN UINT Style,
  279. ...
  280. )
  281. /*++
  282. Routine Description:
  283. This routine formats two message strings--one containing messagebox text,
  284. and the other containing a messagebox caption. The message box is then
  285. displayed.
  286. The message ids can be either a message in this dll's message table
  287. resources or a win32 error code, in which case a description of
  288. that error is retreived from the system.
  289. Arguments:
  290. hinst - Supplies the handle of the module containing string resources to
  291. be used.
  292. hwndParent - Supplies the handle of window to be the parent of the message box.
  293. TextMessageId - Supplies message-table identifier or win32 error code
  294. for the messagebox text.
  295. TitleMessageId - Supplies message-table identifier or win32 error code
  296. for the messagebox caption.
  297. Style - Supplies style flags for the message box.
  298. ... - Supplies arguments to be inserted in the message text.
  299. Return Value:
  300. The return value is zero if there is not enough memory to create the message box, or
  301. if a failure occurred while creating the message box.
  302. If the function succeeds, the return value is one of the following menu-item values
  303. returned by the dialog box:
  304. IDABORT Abort button was selected.
  305. IDCANCEL Cancel button was selected.
  306. IDIGNORE Ignore button was selected.
  307. IDNO No button was selected.
  308. IDOK OK button was selected.
  309. IDRETRY Retry button was selected.
  310. IDYES Yes button was selected.
  311. If a message box has a Cancel button, the function returns the IDCANCEL value if
  312. either the ESC key is pressed or the Cancel button is selected. If the message box
  313. has no Cancel button, pressing ESC has no effect.
  314. --*/
  315. {
  316. va_list arglist;
  317. PTSTR Text = NULL;
  318. INT ret;
  319. //
  320. // We should never be called if we're not interactive.
  321. //
  322. MYASSERT(!(GlobalSetupFlags & (PSPGF_NONINTERACTIVE|PSPGF_UNATTENDED_SETUP)));
  323. if(GlobalSetupFlags & (PSPGF_NONINTERACTIVE|PSPGF_UNATTENDED_SETUP)) {
  324. return 0;
  325. }
  326. try {
  327. va_start(arglist, Style);
  328. Text = RetreiveAndFormatMessageV(TextMessageId, &arglist);
  329. va_end(arglist);
  330. if(Text) {
  331. //
  332. // We always beep when we display the message
  333. //
  334. MessageBeep(Style & (MB_ICONHAND|MB_ICONEXCLAMATION|MB_ICONQUESTION|MB_ICONASTERISK));
  335. ret = MessageBox(hwndParent, Text, Title, Style);
  336. } else {
  337. ret = 0;
  338. }
  339. } except(EXCEPTION_EXECUTE_HANDLER) {
  340. ret = 0;
  341. }
  342. if(Text) {
  343. MyFree(Text);
  344. }
  345. return ret;
  346. }
  347. PTSTR
  348. RetreiveAndFormatMessageV(
  349. IN UINT MessageId,
  350. IN va_list *ArgumentList
  351. )
  352. /*++
  353. Routine Description:
  354. Format a message string using a message string and caller-supplied
  355. arguments.
  356. The message id can be either a message in this dll's message table
  357. resources or a win32 error code, in which case a description of
  358. that error is retreived from the system.
  359. Arguments:
  360. MessageId - supplies message-table identifier or win32 error code
  361. for the message.
  362. ArgumentList - supplies arguments to be inserted in the message text.
  363. Return Value:
  364. Pointer to buffer containing formatted message. If the message was not found
  365. or some error occurred retreiving it, this buffer will be empty.
  366. Caller can free the buffer with MyFree().
  367. If NULL is returned, out of memory.
  368. --*/
  369. {
  370. DWORD d;
  371. PTSTR Buffer;
  372. PTSTR Message;
  373. TCHAR ModuleName[MAX_PATH];
  374. TCHAR ErrorNumber[24];
  375. PTCHAR p;
  376. PTSTR Args[2];
  377. d = FormatMessage(
  378. FORMAT_MESSAGE_ALLOCATE_BUFFER
  379. | ((MessageId < MSG_FIRST) ? FORMAT_MESSAGE_FROM_SYSTEM : FORMAT_MESSAGE_FROM_HMODULE),
  380. (PVOID)MyDllModuleHandle,
  381. MessageId,
  382. MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
  383. (PTSTR)&Buffer,
  384. 0,
  385. ArgumentList
  386. );
  387. if(!d) {
  388. if(GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
  389. return(NULL);
  390. }
  391. wsprintf(ErrorNumber,TEXT("%x"),MessageId);
  392. Args[0] = ErrorNumber;
  393. Args[1] = ModuleName;
  394. if(GetModuleFileName(MyDllModuleHandle,ModuleName,MAX_PATH)) {
  395. if(p = _tcsrchr(ModuleName,TEXT('\\'))) {
  396. Args[1] = p+1;
  397. }
  398. } else {
  399. ModuleName[0] = 0;
  400. }
  401. d = FormatMessage(
  402. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  403. NULL,
  404. ERROR_MR_MID_NOT_FOUND,
  405. MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL),
  406. (PTSTR)&Buffer,
  407. 0,
  408. (va_list *)Args
  409. );
  410. if(!d) {
  411. //
  412. // Give up.
  413. //
  414. return(NULL);
  415. }
  416. }
  417. //
  418. // Make duplicate using our memory system so user can free with MyFree().
  419. //
  420. Message = DuplicateString(Buffer);
  421. LocalFree((HLOCAL)Buffer);
  422. return(Message);
  423. }
  424. PTSTR
  425. RetreiveAndFormatMessage(
  426. IN UINT MessageId,
  427. ...
  428. )
  429. /*++
  430. Routine Description:
  431. Format a message string using a message string and caller-supplied
  432. arguments.
  433. The message id can be either a message in this dll's message table
  434. resources or a win32 error code, in which case a description of
  435. that error is retreived from the system.
  436. Arguments:
  437. MessageId - supplies message-table identifier or win32 error code
  438. for the message.
  439. ... - supplies arguments to be inserted in the message text.
  440. Return Value:
  441. Pointer to buffer containing formatted message. If the message was not found
  442. or some error occurred retreiving it, this buffer will be empty.
  443. Caller can free the buffer with MyFree().
  444. If NULL is returned, out of memory.
  445. --*/
  446. {
  447. va_list arglist;
  448. PTSTR p;
  449. va_start(arglist,MessageId);
  450. p = RetreiveAndFormatMessageV(MessageId,&arglist);
  451. va_end(arglist);
  452. return(p);
  453. }