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.

379 lines
9.7 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. // tlhelp32.h does something "interesting". There is no MODULEENTRY32A version,
  128. // thus if you're compiled UNICODE (and we care) there is no way to have an ANSI
  129. // version of the struct defined. So, we're defining one.
  130. //
  131. typedef struct tagMODULEENTRY32A
  132. {
  133. DWORD dwSize;
  134. DWORD th32ModuleID; // This module
  135. DWORD th32ProcessID; // owning process
  136. DWORD GlblcntUsage; // Global usage count on the module
  137. DWORD ProccntUsage; // Module usage count in th32ProcessID's context
  138. BYTE * modBaseAddr; // Base address of module in th32ProcessID's context
  139. DWORD modBaseSize; // Size in bytes of module starting at modBaseAddr
  140. HMODULE hModule; // The hModule of this module in th32ProcessID's context
  141. char szModule[MAX_MODULE_NAME32 + 1];
  142. char szExePath[MAX_PATH];
  143. } MODULEENTRY32A;
  144. typedef MODULEENTRY32A * PMODULEENTRY32A;
  145. typedef MODULEENTRY32A * LPMODULEENTRY32A;
  146. //+----------------------------------------------------------------------------
  147. //
  148. // Func: CmGetModule9x
  149. //
  150. // Desc: Utility function to get module name on Win9x systems
  151. //
  152. // Args: [hInst] -- IN, instance handle
  153. // [szModule] -- OUT, buffer to return module name
  154. //
  155. // Return: BOOL
  156. //
  157. // Notes:
  158. //
  159. // History: 30-Apr-2000 SumitC Created
  160. //
  161. //-----------------------------------------------------------------------------
  162. BOOL
  163. CmGetModule9x(HINSTANCE hInst, LPTSTR szModule)
  164. {
  165. BOOL fRet = FALSE;
  166. HMODULE hModLib = NULL;
  167. HANDLE hSnap = NULL;
  168. typedef HANDLE (WINAPI* PFN_TH32SS) (DWORD, DWORD);
  169. typedef BOOL (WINAPI* PFN_MOD32F) (HANDLE, LPMODULEENTRY32A);
  170. typedef BOOL (WINAPI* PFN_MOD32N) (HANDLE, LPMODULEENTRY32A);
  171. PFN_TH32SS pfnSnapshot = NULL;
  172. PFN_MOD32F pfnModule32First = NULL;
  173. PFN_MOD32N pfnModule32Next = NULL;
  174. //
  175. // Load the library
  176. //
  177. hModLib = LoadLibraryA("kernel32.dll");
  178. if (NULL == hModLib)
  179. {
  180. CMTRACE(TEXT("9x - could not load kernel32.dll"));
  181. goto Cleanup;
  182. }
  183. //
  184. // Get the necessary function(s)
  185. //
  186. pfnSnapshot = (PFN_TH32SS) GetProcAddress(hModLib, "CreateToolhelp32Snapshot");
  187. pfnModule32First = (PFN_MOD32F) GetProcAddress(hModLib, "Module32First");
  188. pfnModule32Next = (PFN_MOD32N) GetProcAddress(hModLib, "Module32Next");
  189. if (NULL == pfnModule32Next || NULL == pfnModule32First || NULL == pfnSnapshot)
  190. {
  191. CMTRACE(TEXT("9x - couldn't get ToolHelp functions"));
  192. goto Cleanup;
  193. }
  194. //
  195. // Get the module name
  196. //
  197. hSnap = pfnSnapshot(TH32CS_SNAPMODULE, 0);
  198. if (INVALID_HANDLE_VALUE == hSnap)
  199. {
  200. CMTRACE(TEXT("9x - could not get ToolHelp32Snapshot"));
  201. goto Cleanup;
  202. }
  203. else
  204. {
  205. MODULEENTRY32A moduleentry;
  206. BOOL fDone = FALSE;
  207. CHAR szModuleAnsi[13];
  208. moduleentry.dwSize = sizeof(MODULEENTRY32A);
  209. for (fDone = pfnModule32First(hSnap, &moduleentry);
  210. fDone;
  211. fDone = pfnModule32Next(hSnap, &moduleentry))
  212. {
  213. if ((HMODULE)moduleentry.hModule == hInst)
  214. {
  215. lstrcpynA(szModuleAnsi, moduleentry.szModule, 13);
  216. fRet = TRUE;
  217. break;
  218. }
  219. }
  220. SzToWz(szModuleAnsi, szModule, 13);
  221. }
  222. Cleanup:
  223. if (hSnap)
  224. {
  225. CloseHandle(hSnap);
  226. }
  227. if (hModLib)
  228. {
  229. FreeLibrary(hModLib);
  230. }
  231. return fRet;
  232. }
  233. //+----------------------------------------------------------------------------
  234. //
  235. // Func: CmGetModuleBaseName
  236. //
  237. // Desc: Utility function to figure out our module name
  238. //
  239. // Args: [hInst] -- IN, instance handle
  240. // [szModule] -- OUT, buffer to return module name
  241. //
  242. // Return: BOOL
  243. //
  244. // Notes:
  245. //
  246. // History: 30-Apr-2000 SumitC Created
  247. //
  248. //-----------------------------------------------------------------------------
  249. BOOL
  250. CmGetModuleBaseName(HINSTANCE hInst, LPTSTR szModule)
  251. {
  252. BOOL fRet = FALSE;
  253. if (OS_NT)
  254. {
  255. fRet = CmGetModuleNT(hInst, szModule);
  256. }
  257. else
  258. {
  259. fRet = CmGetModule9x(hInst, szModule);
  260. }
  261. if (fRet)
  262. {
  263. // trim the string to just the basename
  264. for (int i = 0; i < lstrlenU(szModule); ++i)
  265. {
  266. if (TEXT('.') == szModule[i])
  267. {
  268. szModule[i] = TEXT('\0');
  269. break;
  270. }
  271. }
  272. }
  273. return fRet;
  274. }
  275. //+----------------------------------------------------------------------------
  276. //
  277. // Func: CmGetDateTime
  278. //
  279. // Desc: Utility function to get system-formatted date and/or time
  280. //
  281. // Args: [ppszDate] -- OUT, ptr to where to put the date (NULL=>don't want the date)
  282. // [ppszTime] -- OUT, ptr to where to put the time (NULL=>don't want the time)
  283. //
  284. // Return: void
  285. //
  286. // Notes:
  287. //
  288. // History: 17-Nov-2000 SumitC Created
  289. //
  290. //-----------------------------------------------------------------------------
  291. void
  292. CmGetDateTime(LPTSTR * ppszDate, LPTSTR * ppszTime)
  293. {
  294. int iRet;
  295. if (ppszDate)
  296. {
  297. iRet = GetDateFormatU(LOCALE_SYSTEM_DEFAULT, DATE_SHORTDATE, NULL, NULL, NULL, 0);
  298. if (iRet)
  299. {
  300. *ppszDate = (LPTSTR) CmMalloc(iRet * sizeof(TCHAR));
  301. if (*ppszDate)
  302. {
  303. iRet = GetDateFormatU(LOCALE_SYSTEM_DEFAULT, DATE_SHORTDATE, NULL, NULL, *ppszDate, iRet);
  304. }
  305. }
  306. else
  307. {
  308. MYDBGASSERT(!"CmGetDateTime - GetDateFormat failed");
  309. *ppszDate = NULL;
  310. }
  311. }
  312. if (ppszTime)
  313. {
  314. iRet = GetTimeFormatU(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, NULL, 0);
  315. if (iRet)
  316. {
  317. *ppszTime = (LPTSTR) CmMalloc(iRet * sizeof(TCHAR));
  318. if (*ppszTime)
  319. {
  320. iRet = GetTimeFormatU(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT|TIME_NOTIMEMARKER, NULL, NULL, *ppszTime, iRet);
  321. }
  322. }
  323. else
  324. {
  325. MYDBGASSERT(!"CmGetDateTime - GetTimeFormat failed");
  326. *ppszTime = NULL;
  327. }
  328. }
  329. }
  330. #undef CMLOG_IMPLEMENTATION