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.

299 lines
6.5 KiB

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