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.

341 lines
8.4 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: ConnStat.cpp
  4. //
  5. // Module: CMDIAL32.DLL
  6. //
  7. // Synopsis: Implementation of class CConnStatistics
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: Fengsun Created 10/15/97
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. #include "ConnStat.h"
  16. //
  17. // Include the constants describing the reg keys used for perf stats
  18. //
  19. #include "perf_str.h"
  20. //
  21. // Constructor and destructor
  22. //
  23. CConnStatistics::CConnStatistics()
  24. {
  25. MYDBGASSERT(!OS_NT4); // class is never used on NT4
  26. m_hKey = NULL;
  27. m_dwInitBytesRead = -1;
  28. m_dwInitBytesWrite = -1;
  29. m_dwBaudRate = 0;
  30. m_pszTotalBytesRecvd = NULL;
  31. m_pszTotalBytesXmit = NULL;
  32. m_pszConnectSpeed = NULL;
  33. }
  34. CConnStatistics::~CConnStatistics()
  35. {
  36. CmFree( m_pszTotalBytesRecvd );
  37. CmFree( m_pszTotalBytesXmit );
  38. CmFree( m_pszConnectSpeed );
  39. }
  40. //+----------------------------------------------------------------------------
  41. //
  42. // Function: CConnStatistics::GetStatRegValues
  43. //
  44. // Synopsis: Helper method, builds the reg value names using the localized
  45. // form of the word "Dial-up Adapter".
  46. //
  47. // Arguments: HINSTANCE hInst
  48. //
  49. // Returns: Nothing
  50. //
  51. // History: nickball Created 11/14/98
  52. //
  53. //+----------------------------------------------------------------------------
  54. void CConnStatistics::GetStatRegValues(HINSTANCE hInst)
  55. {
  56. //
  57. // bug 149367 The word "Dial-up Adapter" need to be localized.
  58. // Load it from resource if no loaded yet
  59. //
  60. if (m_pszTotalBytesRecvd == NULL)
  61. {
  62. m_pszTotalBytesRecvd = CmLoadString(hInst, IDS_REG_DIALUP_ADAPTER);
  63. CmStrCatAlloc(&m_pszTotalBytesRecvd, m_fAdapter2 ? c_pszDialup_2_TotalBytesRcvd : c_pszDialupTotalBytesRcvd);
  64. m_pszTotalBytesXmit = CmLoadString(hInst, IDS_REG_DIALUP_ADAPTER);
  65. CmStrCatAlloc(&m_pszTotalBytesXmit, m_fAdapter2 ? c_pszDialup_2_TotalBytesXmit : c_pszDialupTotalBytesXmit);
  66. m_pszConnectSpeed = CmLoadString(hInst, IDS_REG_DIALUP_ADAPTER);
  67. CmStrCatAlloc(&m_pszConnectSpeed, m_fAdapter2 ? c_pszDialup_2_ConnectSpeed : c_pszDialupConnectSpeed);
  68. }
  69. }
  70. //+---------------------------------------------------------------------------
  71. //
  72. // Function: InitStatistics()
  73. //
  74. // Synopsis: Retrieves Performance Data. On 9x this data is pulled from
  75. // the registry. Defaults are used NT5. Not used on NT4.
  76. //
  77. // Arguments: None
  78. //
  79. // Returns: TRUE if succeed
  80. // FALSE otherwise
  81. //
  82. // History: byao 07/16/97 created
  83. // fengsun 10/97 make it a member fuction
  84. // nickball 03/04/98 always close key
  85. // nickball 03/04/00 added NT5 support
  86. //
  87. //----------------------------------------------------------------------------
  88. BOOL CConnStatistics::InitStatistics()
  89. {
  90. if (OS_W9X)
  91. {
  92. MYDBGASSERT(NULL == m_hKey); // Not already opened
  93. if (m_hKey)
  94. {
  95. RegCloseKey(m_hKey);
  96. m_hKey = NULL;
  97. }
  98. DWORD dwErrCode;
  99. BOOL bRet = FALSE;
  100. //
  101. // If there is already a connected dial up connection
  102. // use the adapter#2 registry key
  103. //
  104. m_fAdapter2 = RasConnectionExists();
  105. dwErrCode = RegOpenKeyExU(HKEY_DYN_DATA,
  106. c_pszDialupPerfKey,
  107. 0,
  108. KEY_ALL_ACCESS,
  109. &m_hKey );
  110. CMTRACE1(TEXT("OpenDAPPerfKey() RegOpenKeyEx() returned GLE=%u."), dwErrCode);
  111. if ( dwErrCode != ERROR_SUCCESS )
  112. {
  113. m_hKey = NULL;
  114. }
  115. else
  116. {
  117. GetStatRegValues(g_hInst);
  118. //
  119. // Get the initial statistic info
  120. //
  121. if (!GetPerfData(m_dwInitBytesRead, m_dwInitBytesWrite, m_dwBaudRate))
  122. {
  123. //
  124. // No dial-up statistic info
  125. //
  126. RegCloseKey(m_hKey);
  127. m_hKey = NULL;
  128. }
  129. }
  130. return m_hKey != NULL;
  131. }
  132. else
  133. {
  134. //
  135. // On NT5, there is the starting bytes is always zero because
  136. // numbers aren't available to us until the connection is up.
  137. // Note: Adapter2 indicates the reg key to be examined on 9x
  138. // it is a non-issue on NT.
  139. //
  140. m_fAdapter2 = FALSE;
  141. m_dwInitBytesRead = 0;
  142. m_dwInitBytesWrite = 0;
  143. }
  144. return TRUE;
  145. }
  146. //+---------------------------------------------------------------------------
  147. //
  148. // Function: GetPerfData
  149. //
  150. // Synopsis: Get Performance Data from DUN1.2 performance registry
  151. //
  152. // Arguments:
  153. //
  154. // Returns: TRUE: succeed
  155. // FALSE otherwise
  156. //
  157. // History: byao created 7/16/97
  158. // fengsun change it into a member function 10/14/97
  159. //
  160. //----------------------------------------------------------------------------
  161. BOOL CConnStatistics::GetPerfData(DWORD& dwRead, DWORD& dwWrite, DWORD& dwBaudRate) const
  162. {
  163. MYDBGASSERT(m_hKey != NULL);
  164. MYDBGASSERT(m_pszTotalBytesRecvd && *m_pszTotalBytesRecvd);
  165. LONG dwErrCode;
  166. DWORD dwValueSize, dwValueType;
  167. DWORD dwValue;
  168. LPTSTR lpKeyName;
  169. //
  170. // "Dial-up Adapter\TotalBytesRecvd"
  171. //
  172. dwValueSize = sizeof(DWORD);
  173. dwErrCode = RegQueryValueExU(m_hKey,
  174. m_pszTotalBytesRecvd,
  175. NULL,
  176. &dwValueType,
  177. (PBYTE)&dwValue,
  178. &dwValueSize);
  179. if (dwErrCode == ERROR_SUCCESS)
  180. {
  181. dwRead = dwValue;
  182. }
  183. else
  184. {
  185. return FALSE;
  186. }
  187. //
  188. // "Dial-up Adapter\TotalBytesXmit"
  189. //
  190. dwValueSize = sizeof(DWORD);
  191. dwErrCode = RegQueryValueExU(m_hKey,
  192. m_pszTotalBytesXmit,
  193. NULL,
  194. &dwValueType,
  195. (PBYTE)&dwValue,
  196. &dwValueSize);
  197. if (dwErrCode == ERROR_SUCCESS)
  198. {
  199. dwWrite = dwValue;
  200. }
  201. else
  202. {
  203. return FALSE;
  204. }
  205. //
  206. // "Dial-up Adapter\ConnectSpeed"
  207. //
  208. dwValueSize = sizeof(DWORD);
  209. dwErrCode = RegQueryValueExU(m_hKey,
  210. m_pszConnectSpeed,
  211. NULL,
  212. &dwValueType,
  213. (PBYTE)&dwValue,
  214. &dwValueSize);
  215. if (dwErrCode == ERROR_SUCCESS)
  216. {
  217. dwBaudRate = dwValue;
  218. }
  219. else
  220. {
  221. return FALSE;
  222. }
  223. return TRUE;
  224. }
  225. //+----------------------------------------------------------------------------
  226. //
  227. // Function: CConnStatistics::RasConnectionExists
  228. //
  229. // Synopsis: Whether there is a connected ras connection running on Win9x.
  230. //
  231. // Arguments: None
  232. //
  233. // Returns: BOOL - TRUE if there is one up and connected
  234. //
  235. // History: fengsun Created 1/15/98
  236. //
  237. //+----------------------------------------------------------------------------
  238. BOOL CConnStatistics::RasConnectionExists()
  239. {
  240. //
  241. // Try RasEnumConnections to find out active connections
  242. //
  243. HINSTANCE hRasInstance = LoadLibraryExA("RASAPI32", NULL, 0);
  244. MYDBGASSERT(hRasInstance);
  245. if (!hRasInstance)
  246. {
  247. return FALSE;
  248. }
  249. typedef DWORD (WINAPI *PFN_RasEnumConnections)(LPRASCONN, LPDWORD, LPDWORD);
  250. PFN_RasEnumConnections lpRasEnumConnections;
  251. lpRasEnumConnections = (PFN_RasEnumConnections)GetProcAddress(hRasInstance, "RasEnumConnectionsA");
  252. MYDBGASSERT(lpRasEnumConnections);
  253. if (!lpRasEnumConnections)
  254. {
  255. FreeLibrary(hRasInstance);
  256. return FALSE;
  257. }
  258. DWORD dwConnections = 0;
  259. DWORD dwSizeNeeded = 0;
  260. if (lpRasEnumConnections(NULL,&dwSizeNeeded,&dwConnections))
  261. {
  262. MYDBGASSERT(dwConnections < 2);
  263. if (dwConnections > 0)
  264. {
  265. FreeLibrary(hRasInstance);
  266. return TRUE;
  267. }
  268. }
  269. FreeLibrary(hRasInstance);
  270. return FALSE;
  271. }
  272. //+----------------------------------------------------------------------------
  273. //
  274. // Function: CConnStatistics::Close
  275. //
  276. // Synopsis: Stop gathering statistic and close the handle
  277. //
  278. // Arguments: None
  279. //
  280. // Returns: Nothing
  281. //
  282. // History: Anonymous Created Header 10/15/97
  283. // nickball Reduced to close key 03/04/98
  284. //
  285. //+----------------------------------------------------------------------------
  286. void CConnStatistics::Close()
  287. {
  288. if (m_hKey)
  289. {
  290. DWORD dwErrCode = RegCloseKey(m_hKey);
  291. CMTRACE1(TEXT("Close() RegCloseKey() returned GLE=%u."), dwErrCode);
  292. m_hKey = NULL;
  293. }
  294. }