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.

339 lines
9.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: devcfg.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "hdwwiz.h"
  11. //
  12. // Define and initialize all device class GUIDs.
  13. // (This must only be done once per module!)
  14. //
  15. #include <initguid.h>
  16. #include <devguid.h>
  17. //
  18. // Define and initialize a global variable, GUID_NULL
  19. // (from coguid.h)
  20. //
  21. DEFINE_GUID(GUID_NULL, 0L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  22. TCHAR szUnknownDevice[64];
  23. USHORT LenUnknownDevice;
  24. TCHAR szUnknown[64];
  25. USHORT LenUnknown;
  26. PTCHAR
  27. BuildFriendlyName(
  28. DEVINST DevInst,
  29. HMACHINE hMachine
  30. )
  31. {
  32. PTCHAR Location;
  33. PTCHAR FriendlyName;
  34. CONFIGRET ConfigRet;
  35. ULONG ulSize;
  36. TCHAR szBuffer[MAX_PATH];
  37. //
  38. // Try the registry for FRIENDLYNAME
  39. //
  40. ulSize = sizeof(szBuffer);
  41. ConfigRet = CM_Get_DevNode_Registry_Property_Ex(DevInst,
  42. CM_DRP_FRIENDLYNAME,
  43. NULL,
  44. szBuffer,
  45. &ulSize,
  46. 0,
  47. hMachine
  48. );
  49. if (ConfigRet != CR_SUCCESS || !*szBuffer) {
  50. //
  51. // Try the registry for DEVICEDESC
  52. //
  53. ulSize = sizeof(szBuffer);
  54. ConfigRet = CM_Get_DevNode_Registry_Property_Ex(DevInst,
  55. CM_DRP_DEVICEDESC,
  56. NULL,
  57. szBuffer,
  58. &ulSize,
  59. 0,
  60. hMachine
  61. );
  62. if (ConfigRet != CR_SUCCESS || !*szBuffer) {
  63. GUID ClassGuid;
  64. //
  65. // Initialize ClassGuid to GUID_NULL
  66. //
  67. CopyMemory(&ClassGuid,
  68. &GUID_NULL,
  69. sizeof(GUID)
  70. );
  71. //
  72. // Try the registry for CLASSNAME
  73. //
  74. ulSize = sizeof(szBuffer);
  75. ConfigRet = CM_Get_DevNode_Registry_Property_Ex(DevInst,
  76. CM_DRP_CLASSGUID,
  77. NULL,
  78. szBuffer,
  79. &ulSize,
  80. 0,
  81. hMachine
  82. );
  83. if (ConfigRet == CR_SUCCESS) {
  84. pSetupGuidFromString(szBuffer, &ClassGuid);
  85. }
  86. if (!IsEqualGUID(&ClassGuid, &GUID_NULL) &&
  87. !IsEqualGUID(&ClassGuid, &GUID_DEVCLASS_UNKNOWN))
  88. {
  89. ulSize = sizeof(szBuffer);
  90. ConfigRet = CM_Get_DevNode_Registry_Property_Ex(DevInst,
  91. CM_DRP_CLASS,
  92. NULL,
  93. szBuffer,
  94. &ulSize,
  95. 0,
  96. hMachine
  97. );
  98. }
  99. else {
  100. ConfigRet = ~CR_SUCCESS;
  101. }
  102. }
  103. }
  104. if (ConfigRet == CR_SUCCESS && *szBuffer) {
  105. FriendlyName = LocalAlloc(LPTR, ulSize);
  106. if (FriendlyName) {
  107. memcpy(FriendlyName, szBuffer, ulSize);
  108. }
  109. }
  110. else {
  111. FriendlyName = NULL;
  112. }
  113. return FriendlyName;
  114. }
  115. void
  116. AddItemToListView(
  117. PHARDWAREWIZ HardwareWiz,
  118. HWND hwndListView,
  119. DEVINST DevInst,
  120. DWORD Problem,
  121. BOOL HiddenDevice,
  122. DEVINST SelectedDevInst
  123. )
  124. {
  125. INT Index;
  126. LV_ITEM lviItem;
  127. PTCHAR FriendlyName;
  128. PTCHAR LocationInfo;
  129. GUID ClassGuid;
  130. ULONG ulSize;
  131. CONFIGRET ConfigRet;
  132. TCHAR szBuffer[MAX_PATH];
  133. lviItem.mask = LVIF_TEXT | LVIF_PARAM;
  134. lviItem.iSubItem = 0;
  135. lviItem.lParam = DevInst;
  136. //
  137. // Devices with problems need to go at the top of the list
  138. //
  139. if (Problem) {
  140. lviItem.iItem = 0;
  141. } else {
  142. lviItem.iItem = ListView_GetItemCount(hwndListView);
  143. }
  144. //
  145. // fetch a name for this device
  146. //
  147. FriendlyName = BuildFriendlyName(DevInst, HardwareWiz->hMachine);
  148. if (FriendlyName) {
  149. lviItem.pszText = FriendlyName;
  150. } else {
  151. lviItem.pszText = szUnknown;
  152. }
  153. //
  154. // Fetch the class icon for this device.
  155. //
  156. ulSize = sizeof(szBuffer);
  157. ConfigRet = CM_Get_DevNode_Registry_Property_Ex(DevInst,
  158. CM_DRP_CLASSGUID,
  159. NULL,
  160. szBuffer,
  161. &ulSize,
  162. 0,
  163. HardwareWiz->hMachine
  164. );
  165. if (ConfigRet == CR_SUCCESS) {
  166. pSetupGuidFromString(szBuffer, &ClassGuid);
  167. } else {
  168. ClassGuid = GUID_DEVCLASS_UNKNOWN;
  169. }
  170. if (SetupDiGetClassImageIndex(&HardwareWiz->ClassImageList,
  171. &ClassGuid,
  172. &lviItem.iImage
  173. ))
  174. {
  175. lviItem.mask |= (LVIF_IMAGE | LVIF_STATE);
  176. if (Problem) {
  177. lviItem.state = (Problem == CM_PROB_DISABLED) ?
  178. INDEXTOOVERLAYMASK(IDI_DISABLED_OVL - IDI_CLASSICON_OVERLAYFIRST + 1) :
  179. INDEXTOOVERLAYMASK(IDI_PROBLEM_OVL - IDI_CLASSICON_OVERLAYFIRST + 1);
  180. } else {
  181. lviItem.state = INDEXTOOVERLAYMASK(0);
  182. }
  183. lviItem.stateMask = LVIS_OVERLAYMASK;
  184. if (HiddenDevice) {
  185. lviItem.state |= LVIS_CUT;
  186. lviItem.stateMask |= LVIS_CUT;
  187. }
  188. }
  189. Index = ListView_InsertItem(hwndListView, &lviItem);
  190. if ((Index != -1) && (SelectedDevInst == DevInst)) {
  191. ListView_SetItemState(hwndListView,
  192. Index,
  193. LVIS_SELECTED|LVIS_FOCUSED,
  194. LVIS_SELECTED|LVIS_FOCUSED
  195. );
  196. }
  197. if (FriendlyName) {
  198. LocalFree(FriendlyName);
  199. }
  200. return;
  201. }
  202. BOOL
  203. BuildDeviceListView(
  204. PHARDWAREWIZ HardwareWiz,
  205. HWND hwndListView,
  206. BOOL ShowHiddenDevices,
  207. DEVINST SelectedDevInst,
  208. DWORD *DevicesDetected,
  209. ADDDEVNODETOLIST_CALLBACK AddDevNodeToListCallBack
  210. )
  211. {
  212. HDEVINFO hDeviceInfo;
  213. DWORD Index;
  214. ULONG DevNodeStatus, DevNodeProblem;
  215. SP_DEVINFO_DATA DevInfoData;
  216. BOOL HiddenDevice;
  217. *DevicesDetected = 0;
  218. hDeviceInfo = SetupDiGetClassDevsEx(NULL, // classguid
  219. NULL, // enumerator
  220. NULL, // hdwnParent
  221. ShowHiddenDevices ? DIGCF_ALLCLASSES : DIGCF_ALLCLASSES | DIGCF_PRESENT,
  222. NULL, // existing HDEVINFO set
  223. HardwareWiz->hMachine ? HardwareWiz->MachineName : NULL,
  224. 0
  225. );
  226. if (hDeviceInfo == INVALID_HANDLE_VALUE) {
  227. return FALSE;
  228. }
  229. DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  230. Index = 0;
  231. while (SetupDiEnumDeviceInfo(hDeviceInfo, Index++, &DevInfoData)) {
  232. if (CM_Get_DevNode_Status_Ex(&DevNodeStatus,
  233. &DevNodeProblem,
  234. DevInfoData.DevInst,
  235. 0,
  236. HardwareWiz->hMachine
  237. ) != CR_SUCCESS) {
  238. DevNodeProblem = 0;
  239. }
  240. HiddenDevice = IsDeviceHidden(HardwareWiz, &DevInfoData);
  241. //
  242. // Only call AddItemToListView if the device is not a hidden device.
  243. //
  244. if (ShowHiddenDevices || !HiddenDevice) {
  245. //
  246. // Check the callback to see if we should add this devnode to the list.
  247. //
  248. if (!AddDevNodeToListCallBack || AddDevNodeToListCallBack(HardwareWiz, &DevInfoData)) {
  249. *DevicesDetected += 1;
  250. //
  251. // Add the item to the ListView
  252. //
  253. AddItemToListView(HardwareWiz,
  254. hwndListView,
  255. DevInfoData.DevInst,
  256. DevNodeProblem,
  257. HiddenDevice,
  258. SelectedDevInst);
  259. }
  260. }
  261. DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  262. }
  263. SetupDiDestroyDeviceInfoList(hDeviceInfo);
  264. return TRUE;
  265. }