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.

288 lines
8.4 KiB

  1. #include <pch.h>
  2. #pragma hdrstop
  3. #include "nsbase.h"
  4. #include "ncreg.h"
  5. #include "ncsetup.h"
  6. #include "ndispnp.h"
  7. #include "resource.h"
  8. //+---------------------------------------------------------------------------
  9. //
  10. // Function: GetLocationInfo
  11. //
  12. // Purpose: Gets the slot and port number of a device and formats
  13. // a display string into a buffer.
  14. //
  15. // Arguments:
  16. // pszDevNodeId [in] The device isntance id of the adapter.
  17. // pszBuffer [in] Buffer to add location string.
  18. // (must be preallocated)
  19. //
  20. // Returns:
  21. //
  22. // Author: billbe 2 Aug 1999
  23. //
  24. // Notes: Slot and/or port number may not exist so the buffer
  25. // may not be modified.
  26. //
  27. VOID
  28. GetLocationInfo (
  29. IN PCWSTR pszDevNodeId,
  30. OUT PWSTR pszLocation)
  31. {
  32. HDEVINFO hdi;
  33. SP_DEVINFO_DATA deid;
  34. HRESULT hr;
  35. // Create the device info set needed to access SetupDi fcns.
  36. //
  37. hr = HrSetupDiCreateDeviceInfoList (&GUID_DEVCLASS_NET, NULL, &hdi);
  38. if (S_OK == hr)
  39. {
  40. TraceTag (ttidLanUi, "Opening %S", pszDevNodeId);
  41. // Open the device info for the adapter.
  42. //
  43. hr = HrSetupDiOpenDeviceInfo (hdi, pszDevNodeId, NULL, 0, &deid);
  44. if (S_OK == hr)
  45. {
  46. BOOL fHaveSlotNumber;
  47. DWORD dwSlotNumber;
  48. DWORD dwPortNumber;
  49. BOOL fHavePortNumber;
  50. // Slot number is stored as the UINumber registry property.
  51. //
  52. hr = HrSetupDiGetDeviceRegistryProperty (hdi, &deid,
  53. SPDRP_UI_NUMBER, NULL, (BYTE*)&dwSlotNumber,
  54. sizeof (dwSlotNumber), NULL);
  55. TraceTag (ttidLanUi, "Getting ui number result %lX %d",
  56. hr, dwSlotNumber);
  57. fHaveSlotNumber = (S_OK == hr);
  58. // Port information is stored by the class installer in the
  59. // device key.
  60. //
  61. HKEY hkey;
  62. fHavePortNumber = FALSE;
  63. hr = HrSetupDiOpenDevRegKey (hdi, &deid, DICS_FLAG_GLOBAL, 0,
  64. DIREG_DEV, KEY_READ, &hkey);
  65. if (S_OK == hr)
  66. {
  67. hr = HrRegQueryDword(hkey, L"Port", &dwPortNumber);
  68. fHavePortNumber = (S_OK == hr);
  69. RegCloseKey (hkey);
  70. }
  71. // Format the string according to what information
  72. // we were able to retrieve.
  73. //
  74. HINSTANCE hinst = _Module.GetResourceInstance();
  75. if (fHaveSlotNumber && fHavePortNumber)
  76. {
  77. swprintf (pszLocation,
  78. SzLoadString (hinst, IDS_SLOT_PORT_LOCATION),
  79. dwSlotNumber, dwPortNumber);
  80. TraceTag (ttidLanUi, "Found slot and port. %S", pszLocation);
  81. }
  82. else if (fHaveSlotNumber)
  83. {
  84. swprintf (pszLocation,
  85. SzLoadString (hinst, IDS_SLOT_LOCATION),
  86. dwSlotNumber);
  87. TraceTag (ttidLanUi, "Found slot. %S", pszLocation);
  88. }
  89. else if (fHavePortNumber)
  90. {
  91. swprintf (pszLocation,
  92. SzLoadString (hinst, IDS_PORT_LOCATION),
  93. dwPortNumber);;
  94. TraceTag (ttidLanUi, "Found port. %S", pszLocation);
  95. }
  96. }
  97. SetupDiDestroyDeviceInfoList (hdi);
  98. }
  99. }
  100. //+---------------------------------------------------------------------------
  101. //
  102. // Function: AppendMacAddress
  103. //
  104. // Purpose: Appends the MAC address of a LAN adapter to a buffer for
  105. // display in UI.
  106. //
  107. // Arguments:
  108. // pszBindName [in] Bind name of adapter.
  109. // pszBuffer [in] Buffer to add MAC address string.
  110. // (must be preallocated)
  111. //
  112. // Returns:
  113. //
  114. // Author: tongl 17 Sept 1998
  115. // billbe 3 Aug 1999 Modified for datatip
  116. //
  117. // Notes:
  118. //
  119. VOID
  120. AppendMacAddress (
  121. IN PCWSTR pszBindName,
  122. IN OUT PWSTR pszBuffer)
  123. {
  124. Assert (pszBindName);
  125. Assert (pszBuffer);
  126. WCHAR szExport[_MAX_PATH];
  127. if (pszBindName)
  128. {
  129. wcscpy (szExport, L"\\Device\\");
  130. wcscat (szExport, pszBindName);
  131. UNICODE_STRING ustrDevice;
  132. RtlInitUnicodeString(&ustrDevice, szExport);
  133. // Get the Mac Address
  134. UINT uiRet;
  135. UCHAR MacAddr[6];
  136. UCHAR PMacAddr[6];
  137. UCHAR VendorId[3];
  138. uiRet = NdisQueryHwAddress(&ustrDevice, MacAddr, PMacAddr, VendorId);
  139. if (uiRet)
  140. {
  141. // Succeeded
  142. WCHAR pszNumber[32];
  143. *pszNumber = 0;
  144. WCHAR szBuff[4];
  145. for (INT i=0; i<=5; i++)
  146. {
  147. wsprintfW(szBuff, L"%02X", MacAddr[i]);
  148. wcscat(pszNumber, szBuff);
  149. if (i != 5)
  150. {
  151. wcscat(pszNumber, L"-");
  152. }
  153. }
  154. if (*pszBuffer)
  155. {
  156. wcscat (pszBuffer, L"\n");
  157. }
  158. DwFormatString(SzLoadString (_Module.GetResourceInstance(), IDS_MAC_ADDRESS),
  159. pszBuffer + wcslen (pszBuffer),
  160. _MAX_PATH,
  161. pszNumber);
  162. }
  163. }
  164. }
  165. //+---------------------------------------------------------------------------
  166. //
  167. // Function: CreateDeviceDataTip
  168. //
  169. // Purpose: Creates a data tip that will display device specific
  170. // information when the user hovers over nIdTool.
  171. //
  172. // Arguments:
  173. // hwndParent [in] hwnd to parent window.
  174. // phwndDataTip [in] pointer to the hwnd of data tip. Must be
  175. // preallocated and the hwnd assigned to NULL if
  176. // data tip has not been created.
  177. // nIdTool [in] resource is of tool to add datatip to.
  178. // pszDevNodeId [in] The device isntance id of the adapter.
  179. // pszBindName [in] Bind name of adapter.
  180. //
  181. // Returns: nothing
  182. //
  183. // Author: billbe 2 Aug 1999
  184. //
  185. // Notes:
  186. //
  187. VOID
  188. CreateDeviceDataTip (
  189. IN HWND hwndParent,
  190. IN OUT HWND* phwndDataTip,
  191. IN UINT nIdTool,
  192. IN PCWSTR pszDevNodeId,
  193. IN PCWSTR pszBindName)
  194. {
  195. if (!*phwndDataTip)
  196. {
  197. TraceTag (ttidLanUi, "Creating device datatip!!!");
  198. *phwndDataTip = CreateWindowExW (0, TOOLTIPS_CLASS, NULL,
  199. WS_POPUP | TTS_ALWAYSTIP,
  200. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  201. CW_USEDEFAULT, hwndParent, NULL, NULL, NULL);
  202. if (*phwndDataTip)
  203. {
  204. SetWindowPos (*phwndDataTip, HWND_TOPMOST, CW_USEDEFAULT,
  205. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  206. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  207. }
  208. }
  209. if (*phwndDataTip)
  210. {
  211. TTTOOLINFOW toolinfo = {0};
  212. toolinfo.cbSize = sizeof (toolinfo);
  213. toolinfo.uId = (UINT_PTR)GetDlgItem (hwndParent, nIdTool);
  214. toolinfo.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
  215. WCHAR szDataTip[_MAX_PATH] = {0};
  216. // Get location info.
  217. if (pszDevNodeId)
  218. {
  219. GetLocationInfo (pszDevNodeId, szDataTip);
  220. }
  221. // Append Mac address.
  222. if (pszBindName)
  223. {
  224. AppendMacAddress (pszBindName, szDataTip);
  225. }
  226. // If there is anything to display, set the data tip.
  227. //
  228. if (*szDataTip)
  229. {
  230. toolinfo.lpszText = szDataTip;
  231. SendMessage (*phwndDataTip, TTM_ADDTOOL, 0, (LPARAM)&toolinfo);
  232. // In order to use '\n' to move to the next line of the data tip,
  233. // we need to set the width. We will set it to have of the
  234. // promary monitor's screen size.
  235. //
  236. DWORD dwToolTipWidth = GetSystemMetrics (SM_CXSCREEN) / 2;
  237. if (dwToolTipWidth)
  238. {
  239. SendMessage (*phwndDataTip, TTM_SETMAXTIPWIDTH, 0, dwToolTipWidth);
  240. }
  241. // Keep the tip up for 30 seconds.
  242. SendMessage (*phwndDataTip, TTM_SETDELAYTIME, TTDT_AUTOPOP,
  243. MAKELONG (30000, 0));
  244. TraceTag (ttidLanUi, "Creating device datatip complete!!!");
  245. }
  246. }
  247. else
  248. {
  249. TraceTag (ttidError, "Creating datatip failed");
  250. }
  251. }