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.

358 lines
8.4 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: cmlogutil.cpp
  4. //
  5. // Module: CMLOG.LIB
  6. //
  7. // Synopsis: Utility function for Connection Manager Logging
  8. //
  9. // Copyright (c) 1998-2000 Microsoft Corporation
  10. //
  11. // Author: 25-May-2000 SumitC Created
  12. //
  13. // Note:
  14. //
  15. //-----------------------------------------------------------------------------
  16. #define CMLOG_IMPLEMENTATION
  17. #ifndef UNICODE
  18. #define UNICODE
  19. #endif
  20. #include <windows.h>
  21. #include <psapi.h>
  22. #include <tlhelp32.h>
  23. #include "cmmaster.h"
  24. #include "cmlog.h"
  25. #include "cmlogutil.h"
  26. #if 0
  27. const DWORD c_szFmtSize = 128; // largest format string possible.
  28. /*
  29. //+----------------------------------------------------------------------------
  30. //
  31. // Func: ConvertFormatString
  32. //
  33. // Desc: Utility function, converts %s to %S within a given format string
  34. //
  35. // Args: [pszFmt] - format string with %s's and %c's in it, to be converted
  36. //
  37. // Return: LPTSTR buffer containing new format string
  38. //
  39. // Notes: return value is a static buffer and doesn't have to be freed.
  40. //
  41. // History: 30-Apr-2000 SumitC Created
  42. //
  43. //-----------------------------------------------------------------------------
  44. BOOL
  45. ConvertFormatString(LPTSTR pszFmt)
  46. {
  47. MYDBGASSERT(pszFmt);
  48. if (lstrlenU(pszFmt) > c_szFmtSize)
  49. {
  50. MYDBGASSERT(!"Cmlog format string too large, fix code");
  51. return FALSE;
  52. }
  53. for (int i = 1; i < lstrlenU(pszFmt); i++)
  54. {
  55. if (pszFmt[i - 1] == TEXT('%') && pszFmt[i] == TEXT('s'))
  56. {
  57. // uppercase it.
  58. pszFmt[i] = TEXT('S');
  59. }
  60. if (pszFmt[i - 1] == TEXT('%') && pszFmt[i] == TEXT('c'))
  61. {
  62. // uppercase it.
  63. pszFmt[i] = TEXT('C');
  64. }
  65. }
  66. return TRUE;
  67. }
  68. */
  69. #endif
  70. //+----------------------------------------------------------------------------
  71. //
  72. // Func: CmGetModuleNT
  73. //
  74. // Desc: Utility function to get module name on WinNT systems
  75. //
  76. // Args: [hInst] -- IN, instance handle
  77. // [szModule] -- OUT, buffer to return module name
  78. //
  79. // Return: BOOL
  80. //
  81. // Notes:
  82. //
  83. // History: 30-Apr-2000 SumitC Created
  84. //
  85. //-----------------------------------------------------------------------------
  86. BOOL
  87. CmGetModuleNT(HINSTANCE hInst, LPTSTR szModule)
  88. {
  89. BOOL fRet = FALSE;
  90. HMODULE hModLib = NULL;
  91. typedef DWORD (WINAPI* PFN_GMBN)(HANDLE, HMODULE, LPTSTR, DWORD);
  92. PFN_GMBN pfnGetModuleBaseName = NULL;
  93. //
  94. // Load the library
  95. //
  96. hModLib = LoadLibrary(TEXT("PSAPI.dll"));
  97. if (NULL == hModLib)
  98. {
  99. CMTRACE(TEXT("NT - could not load psapi.dll"));
  100. goto Cleanup;
  101. }
  102. //
  103. // Get the necessary function(s)
  104. //
  105. #ifdef UNICODE
  106. pfnGetModuleBaseName = (PFN_GMBN)GetProcAddress(hModLib, "GetModuleBaseNameW");
  107. #else
  108. pfnGetModuleBaseName = (PFN_GMBN)GetProcAddress(hModLib, TEXT("GetModuleBaseNameA"));
  109. #endif
  110. if (NULL == pfnGetModuleBaseName)
  111. {
  112. CMTRACE(TEXT("NT - couldn't find GetModuleBaseName within psapi.dll !!"));
  113. goto Cleanup;
  114. }
  115. //
  116. // Get the module name
  117. //
  118. fRet = (0 != pfnGetModuleBaseName(GetCurrentProcess(), hInst, szModule, 13));
  119. Cleanup:
  120. if (hModLib)
  121. {
  122. FreeLibrary(hModLib);
  123. }
  124. return fRet;
  125. }
  126. //+----------------------------------------------------------------------------
  127. //
  128. // Func: CmGetModule9x
  129. //
  130. // Desc: Utility function to get module name on Win9x systems
  131. //
  132. // Args: [hInst] -- IN, instance handle
  133. // [szModule] -- OUT, buffer to return module name
  134. //
  135. // Return: BOOL
  136. //
  137. // Notes:
  138. //
  139. // History: 30-Apr-2000 SumitC Created
  140. //
  141. //-----------------------------------------------------------------------------
  142. BOOL
  143. CmGetModule9x(HINSTANCE hInst, LPTSTR szModule)
  144. {
  145. BOOL fRet = FALSE;
  146. HMODULE hModLib = NULL;
  147. HANDLE hSnap = NULL;
  148. typedef HANDLE (WINAPI* PFN_TH32SS) (DWORD, DWORD);
  149. typedef BOOL (WINAPI* PFN_MOD32F) (HANDLE, LPMODULEENTRY32);
  150. typedef BOOL (WINAPI* PFN_MOD32N) (HANDLE, LPMODULEENTRY32);
  151. PFN_TH32SS pfnSnapshot = NULL;
  152. PFN_MOD32F pfnModule32First = NULL;
  153. PFN_MOD32N pfnModule32Next = NULL;
  154. //
  155. // Load the library
  156. //
  157. hModLib = LoadLibraryA("kernel32.dll");
  158. if (NULL == hModLib)
  159. {
  160. CMTRACE(TEXT("9x - could not load kernel32.dll"));
  161. goto Cleanup;
  162. }
  163. //
  164. // Get the necessary function(s)
  165. //
  166. pfnSnapshot = (PFN_TH32SS) GetProcAddress(hModLib, "CreateToolhelp32Snapshot");
  167. pfnModule32First = (PFN_MOD32F) GetProcAddress(hModLib, "Module32First");
  168. pfnModule32Next = (PFN_MOD32N) GetProcAddress(hModLib, "Module32Next");
  169. if (NULL == pfnModule32Next || NULL == pfnModule32First || NULL == pfnSnapshot)
  170. {
  171. CMTRACE(TEXT("9x - couldn't get ToolHelp functions"));
  172. goto Cleanup;
  173. }
  174. //
  175. // Get the module name
  176. //
  177. hSnap = pfnSnapshot(TH32CS_SNAPMODULE, 0);
  178. if (INVALID_HANDLE_VALUE == hSnap)
  179. {
  180. CMTRACE(TEXT("9x - could not get ToolHelp32Snapshot"));
  181. goto Cleanup;
  182. }
  183. else
  184. {
  185. MODULEENTRY32 moduleentry;
  186. BOOL fDone = FALSE;
  187. CHAR szModuleAnsi[13];
  188. moduleentry.dwSize = sizeof(MODULEENTRY32);
  189. for (fDone = pfnModule32First(hSnap, &moduleentry);
  190. fDone;
  191. fDone = pfnModule32Next(hSnap, &moduleentry))
  192. {
  193. if ((HMODULE)moduleentry.hModule == hInst)
  194. {
  195. memcpy(szModuleAnsi, moduleentry.szModule, 12 * sizeof(CHAR));
  196. szModuleAnsi[12] = '\0';
  197. fRet = TRUE;
  198. break;
  199. }
  200. }
  201. SzToWz(szModuleAnsi, szModule, 13);
  202. }
  203. Cleanup:
  204. if (hSnap)
  205. {
  206. CloseHandle(hSnap);
  207. }
  208. if (hModLib)
  209. {
  210. FreeLibrary(hModLib);
  211. }
  212. return fRet;
  213. }
  214. //+----------------------------------------------------------------------------
  215. //
  216. // Func: CmGetModuleBaseName
  217. //
  218. // Desc: Utility function to figure out our module name
  219. //
  220. // Args: [hInst] -- IN, instance handle
  221. // [szModule] -- OUT, buffer to return module name
  222. //
  223. // Return: BOOL
  224. //
  225. // Notes:
  226. //
  227. // History: 30-Apr-2000 SumitC Created
  228. //
  229. //-----------------------------------------------------------------------------
  230. BOOL
  231. CmGetModuleBaseName(HINSTANCE hInst, LPTSTR szModule)
  232. {
  233. BOOL fRet = FALSE;
  234. if (OS_NT)
  235. {
  236. fRet = CmGetModuleNT(hInst, szModule);
  237. }
  238. else
  239. {
  240. fRet = CmGetModule9x(hInst, szModule);
  241. }
  242. if (fRet)
  243. {
  244. // trim the string to just the basename
  245. for (int i = 0; i < lstrlenU(szModule); ++i)
  246. {
  247. if (TEXT('.') == szModule[i])
  248. {
  249. szModule[i] = TEXT('\0');
  250. break;
  251. }
  252. }
  253. }
  254. return fRet;
  255. }
  256. //+----------------------------------------------------------------------------
  257. //
  258. // Func: CmGetDateTime
  259. //
  260. // Desc: Utility function to get system-formatted date and/or time
  261. //
  262. // Args: [ppszDate] -- OUT, ptr to where to put the date (NULL=>don't want the date)
  263. // [ppszTime] -- OUT, ptr to where to put the time (NULL=>don't want the time)
  264. //
  265. // Return: void
  266. //
  267. // Notes:
  268. //
  269. // History: 17-Nov-2000 SumitC Created
  270. //
  271. //-----------------------------------------------------------------------------
  272. void
  273. CmGetDateTime(LPTSTR * ppszDate, LPTSTR * ppszTime)
  274. {
  275. int iRet;
  276. if (ppszDate)
  277. {
  278. iRet = GetDateFormatU(LOCALE_SYSTEM_DEFAULT, DATE_SHORTDATE, NULL, NULL, NULL, 0);
  279. if (iRet)
  280. {
  281. *ppszDate = (LPTSTR) CmMalloc(iRet * sizeof(TCHAR));
  282. if (*ppszDate)
  283. {
  284. iRet = GetDateFormatU(LOCALE_SYSTEM_DEFAULT, DATE_SHORTDATE, NULL, NULL, *ppszDate, iRet);
  285. }
  286. }
  287. else
  288. {
  289. MYDBGASSERT(!"CmGetDateTime - GetDateFormat failed");
  290. *ppszDate = NULL;
  291. }
  292. }
  293. if (ppszTime)
  294. {
  295. iRet = GetTimeFormatU(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, NULL, 0);
  296. if (iRet)
  297. {
  298. *ppszTime = (LPTSTR) CmMalloc(iRet * sizeof(TCHAR));
  299. if (*ppszTime)
  300. {
  301. iRet = GetTimeFormatU(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT|TIME_NOTIMEMARKER, NULL, NULL, *ppszTime, iRet);
  302. }
  303. }
  304. else
  305. {
  306. MYDBGASSERT(!"CmGetDateTime - GetTimeFormat failed");
  307. *ppszTime = NULL;
  308. }
  309. }
  310. }
  311. #undef CMLOG_IMPLEMENTATION