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.

289 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1993-2001 Microsoft Corporation
  3. Module Name:
  4. util.cpp
  5. Abstract:
  6. This file implements common utilitarian functions.
  7. Author:
  8. Wesley Witt (wesw) 1-May-1993
  9. Environment:
  10. User Mode
  11. --*/
  12. #include "pch.cpp"
  13. void
  14. GetWinHelpFileName(
  15. _TCHAR *pszHelpFileName,
  16. DWORD len
  17. )
  18. {
  19. _TCHAR szDrive[_MAX_DRIVE];
  20. _TCHAR szDir[_MAX_DIR];
  21. //
  22. // find out the path where DrWatson was run from
  23. //
  24. GetModuleFileName( GetModuleHandle(NULL), pszHelpFileName, len );
  25. //
  26. // take the path and append the help file name
  27. //
  28. _tsplitpath( pszHelpFileName, szDrive, szDir, NULL, NULL );
  29. _stprintf( pszHelpFileName, _T("%s%sdrwtsn32.hlp"), szDrive, szDir );
  30. return;
  31. }
  32. void
  33. GetHtmlHelpFileName(
  34. _TCHAR *pszHelpFileName,
  35. DWORD len
  36. )
  37. {
  38. _TCHAR szDrive[_MAX_DRIVE];
  39. _TCHAR szDir[_MAX_DIR];
  40. //
  41. // Make sure the array is at least initialized to zero.
  42. //
  43. *pszHelpFileName = 0;
  44. //
  45. // find out the path where DrWatson was run from
  46. //
  47. GetModuleFileName( GetModuleHandle(NULL), pszHelpFileName, len );
  48. //
  49. // take the path and append the help file name
  50. //
  51. _tsplitpath( pszHelpFileName, szDrive, szDir, NULL, NULL );
  52. _stprintf( pszHelpFileName, _T("%s%sdrwtsn32.chm"), szDrive, szDir );
  53. return;
  54. }
  55. /***************************************************************************\
  56. * LoadStringOrError
  57. *
  58. * NOTE: Passing a NULL value for lpch returns the string length. (WRONG!)
  59. *
  60. * Warning: The return count does not include the terminating NULL WCHAR;
  61. *
  62. * History:
  63. * 05-Apr-1991 ScottLu Fixed - code is now shared between client and server
  64. * 24-Sep-1990 MikeKe From Win30
  65. \***************************************************************************/
  66. int
  67. MyLoadStringOrError(
  68. HMODULE hModule,
  69. UINT wID,
  70. LPWSTR lpBuffer, // Unicode buffer
  71. int nLenInChars, // cch in Unicode buffer
  72. WORD wLangId
  73. )
  74. {
  75. HRSRC hResInfo;
  76. HANDLE hStringSeg;
  77. LPWSTR lpsz;
  78. int cch;
  79. /*
  80. * Make sure the parms are valid.
  81. */
  82. if (lpBuffer == NULL) {
  83. //RIPMSG0(RIP_WARNING, _T("MyLoadStringOrError: lpBuffer == NULL"));
  84. return 0;
  85. }
  86. cch = 0;
  87. /*
  88. * String Tables are broken up into 16 string segments. Find the segment
  89. * containing the string we are interested in.
  90. */
  91. hResInfo = FindResourceExW(hModule,
  92. MAKEINTRESOURCEW(6), /* RT_STRING */
  93. (LPWSTR)((LONG_PTR)(((USHORT)wID >> 4) + 1)),
  94. wLangId
  95. );
  96. if (hResInfo) {
  97. /*
  98. * Load that segment.
  99. */
  100. hStringSeg = LoadResource(hModule, hResInfo);
  101. if (hStringSeg == NULL)
  102. {
  103. return 0;
  104. }
  105. lpsz = (LPWSTR) (hStringSeg);
  106. /*
  107. * Move past the other strings in this segment.
  108. * (16 strings in a segment -> & 0x0F)
  109. */
  110. wID &= 0x0F;
  111. while (TRUE) {
  112. cch = *((WCHAR *)lpsz++); // PASCAL like string count
  113. // first UTCHAR is count if TCHARs
  114. if (wID-- == 0) break;
  115. lpsz += cch; // Step to start if next string
  116. }
  117. /*
  118. * chhBufferMax == 0 means return a pointer to the read-only resource buffer.
  119. */
  120. if (nLenInChars == 0) {
  121. *(LPWSTR *)lpBuffer = lpsz;
  122. } else {
  123. /*
  124. * Account for the NULL
  125. */
  126. nLenInChars--;
  127. /*
  128. * Don't copy more than the max allowed.
  129. */
  130. if (cch > nLenInChars) {
  131. cch = nLenInChars;
  132. }
  133. /*
  134. * Copy the string into the buffer.
  135. */
  136. CopyMemory(lpBuffer, lpsz, cch*sizeof(WCHAR));
  137. }
  138. }
  139. /*
  140. * Append a NULL.
  141. */
  142. if (nLenInChars != 0) {
  143. lpBuffer[cch] = 0;
  144. }
  145. return cch;
  146. }
  147. /***************************************************************************\
  148. * LoadStringA (API)
  149. * LoadStringW (API)
  150. *
  151. *
  152. * 05-Apr-1991 ScottLu Fixed to work with client/server.
  153. \***************************************************************************/
  154. int
  155. WINAPI
  156. MyLoadString(
  157. HINSTANCE hmod,
  158. UINT wID,
  159. LPWSTR lpBuffer,
  160. int nLenInChars
  161. )
  162. {
  163. return MyLoadStringOrError((HMODULE)hmod,
  164. wID,
  165. lpBuffer,
  166. nLenInChars,
  167. 0);
  168. }
  169. _TCHAR *
  170. LoadRcString(
  171. UINT wId
  172. )
  173. /*++
  174. Routine Description:
  175. Loads a resource string from DRWTSN32 and returns a pointer
  176. to the string.
  177. Arguments:
  178. wId - resource string id
  179. Return Value:
  180. pointer to the string
  181. --*/
  182. {
  183. static _TCHAR buf[1024];
  184. MyLoadString( GetModuleHandle(NULL), wId, buf, sizeof(buf) / sizeof(_TCHAR) );
  185. return buf;
  186. }
  187. void
  188. GetAppName(
  189. _TCHAR *pszAppName,
  190. DWORD len
  191. )
  192. {
  193. MyLoadString( GetModuleHandle(NULL), IDS_APPLICATION_NAME, pszAppName, len );
  194. }
  195. PTSTR
  196. ExpandPath(
  197. PTSTR lpPath
  198. )
  199. /*++
  200. Description
  201. Expands the path passed. Returns the expanded path in an dynamically
  202. allocated string. The dynamically allocated string is always at least
  203. _MAX_PATH is size. Note: size is calculated in characters.
  204. Arguments
  205. lpPath - Path to be expanded.
  206. Returns
  207. Dynamically allocated buffer at least _MAX_PATH in length.
  208. --*/
  209. {
  210. DWORD len;
  211. PTSTR p;
  212. len = ExpandEnvironmentStrings( lpPath, NULL, 0 );
  213. if (!len) {
  214. return NULL;
  215. }
  216. len++; // Null terminator
  217. len = max(len, _MAX_PATH);
  218. p = (PTSTR) calloc( len, sizeof(_TCHAR) );
  219. if (!p) {
  220. return NULL;
  221. }
  222. len = ExpandEnvironmentStrings( lpPath, p, len );
  223. if (!len) {
  224. free( p );
  225. return NULL;
  226. }
  227. return p;
  228. }