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.

320 lines
9.2 KiB

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