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.

255 lines
7.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: C O M P I D . C P P
  7. //
  8. // Contents: Functions dealing with compatible ids
  9. //
  10. // Notes:
  11. //
  12. // Author: kumarp 04-September-98
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "kkutils.h"
  18. #include "ncsetup.h"
  19. #include "ncnetcfg.h"
  20. // ----------------------------------------------------------------------
  21. //
  22. // Function: HrGetCompatibleIds
  23. //
  24. // Purpose: Get a list of PnpIds compatible to the given net device
  25. //
  26. // Arguments:
  27. // hdi [in] handle of device info
  28. // pdeid [in] pointer to device info data
  29. // ppmszCompatibleIds [out] pointer to multisz to receive the list
  30. //
  31. // Returns: S_OK on success, otherwise an error code
  32. //
  33. // Author: kumarp 17-March-98
  34. //
  35. // Notes:
  36. //
  37. HRESULT HrGetCompatibleIds(
  38. IN HDEVINFO hdi,
  39. IN PSP_DEVINFO_DATA pdeid,
  40. OUT PWSTR* ppmszCompatibleIds)
  41. {
  42. Assert(IsValidHandle(hdi));
  43. AssertValidReadPtr(pdeid);
  44. AssertValidWritePtr(ppmszCompatibleIds);
  45. // Now we need to build a list of all the possible ids this
  46. // device could have so that netsetup can find the correct match.
  47. //
  48. HRESULT hr;
  49. // First we get the multi sz of hardware ids
  50. //
  51. PWSTR pszHwIds = NULL;
  52. PWSTR pszCompatIds = NULL;
  53. PWSTR pszIdList = NULL;
  54. *ppmszCompatibleIds = NULL;
  55. hr = HrSetupDiGetDeviceRegistryPropertyWithAlloc (hdi, pdeid,
  56. SPDRP_HARDWAREID, NULL, (BYTE**)&pszHwIds);
  57. if (S_OK == hr)
  58. {
  59. // Now we get the multi sz of compatible ids
  60. // Note: we can still attempt to get parameters with
  61. // the hardware ids so if the next call fails, we will
  62. // continue
  63. (void) HrSetupDiGetDeviceRegistryPropertyWithAlloc (hdi, pdeid,
  64. SPDRP_COMPATIBLEIDS, NULL, (BYTE**)&pszCompatIds);
  65. // Get the lengths of the ids
  66. //
  67. // Get the length of the hardware ids without the extra null
  68. Assert (CchOfMultiSzAndTermSafe (pszHwIds) > 0);
  69. ULONG cbHwIds = CchOfMultiSzSafe (pszHwIds) * sizeof (WCHAR);
  70. ULONG cbCompatIds = CchOfMultiSzAndTermSafe (pszCompatIds) *
  71. sizeof (WCHAR);
  72. // If there was a compatible id list we need to make one concatenated list
  73. //
  74. if (cbCompatIds)
  75. {
  76. hr = E_OUTOFMEMORY;
  77. // allocate the buffer
  78. pszIdList = (PWSTR)MemAlloc (cbHwIds + cbCompatIds);
  79. if (pszIdList)
  80. {
  81. // copy the two lists
  82. // The hwids length does not contain the extra null, but
  83. // the compat id list does, so it works out when
  84. // concatenating
  85. //
  86. hr = S_OK;
  87. CopyMemory (pszIdList, pszHwIds, cbHwIds);
  88. Assert (0 == (cbHwIds % sizeof(WCHAR)));
  89. CopyMemory ((BYTE*)pszIdList + cbHwIds, pszCompatIds,
  90. cbCompatIds);
  91. *ppmszCompatibleIds = pszIdList;
  92. }
  93. MemFree (pszCompatIds);
  94. MemFree (pszHwIds);
  95. }
  96. else
  97. {
  98. // only the main (Hardware Ids) list is available so
  99. // just assign to the list variable
  100. *ppmszCompatibleIds = pszHwIds;
  101. }
  102. }
  103. TraceHr (ttidNetSetup, FAL, hr, FALSE, "HrGetCompatibleIds");
  104. return hr;
  105. }
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Function: HrIsAdapterInstalled
  109. //
  110. // Purpose: Find out if the specified adapter is installed
  111. //
  112. // Arguments:
  113. // szAdapterId [in] PnP Id
  114. //
  115. // Returns: S_OK on success, otherwise an error code
  116. //
  117. // Author: kumarp 17-September-98
  118. //
  119. // Notes:
  120. //
  121. HRESULT HrIsAdapterInstalled(IN PCWSTR szAdapterId)
  122. {
  123. DefineFunctionName("HrIsAdapterInstalled2");
  124. AssertValidReadPtr(szAdapterId);
  125. HRESULT hr=S_OK;
  126. HDEVINFO hdi;
  127. DWORD dwIndex=0;
  128. SP_DEVINFO_DATA deid;
  129. WCHAR szInstance[MAX_DEVICE_ID_LEN];
  130. BOOL fFound = FALSE;
  131. hr = HrSetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL,
  132. DIGCF_PRESENT, &hdi);
  133. if (S_OK == hr)
  134. {
  135. while (!fFound &&
  136. SUCCEEDED(hr = HrSetupDiEnumDeviceInfo(hdi, dwIndex, &deid)))
  137. {
  138. dwIndex++;
  139. hr = HrSetupDiGetDeviceInstanceId(hdi, &deid, szInstance,
  140. MAX_DEVICE_ID_LEN, NULL);
  141. if (S_OK == hr)
  142. {
  143. PWSTR pmszCompatibleIds;
  144. hr = HrGetCompatibleIds(hdi, &deid, &pmszCompatibleIds);
  145. if (S_OK == hr)
  146. {
  147. if (FIsSzInMultiSzSafe(szAdapterId, pmszCompatibleIds))
  148. {
  149. fFound = TRUE;
  150. hr = S_OK;
  151. }
  152. MemFree(pmszCompatibleIds);
  153. }
  154. }
  155. }
  156. SetupDiDestroyDeviceInfoList(hdi);
  157. }
  158. if (HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS) == hr)
  159. {
  160. hr = S_FALSE;
  161. }
  162. TraceErrorSkip1(__FUNCNAME__, hr, S_FALSE);
  163. return hr;
  164. }
  165. //+---------------------------------------------------------------------------
  166. //
  167. // Function: HrGetCompatibleIdsOfNetComponent
  168. //
  169. // Purpose: Find compatible PnP IDs of the adapter
  170. // specified by the given INetCfgComponent
  171. //
  172. // Arguments:
  173. // pncc [in] pointer to INetCfgComponent object
  174. // ppmszCompatibleIds [out] pointer to compatible IDs multisz
  175. //
  176. // Returns: S_OK on success, otherwise an error code
  177. //
  178. // Author: kumarp 28-September-98
  179. //
  180. // Notes:
  181. //
  182. HRESULT HrGetCompatibleIdsOfNetComponent(IN INetCfgComponent* pncc,
  183. OUT PWSTR* ppmszCompatibleIds)
  184. {
  185. DefineFunctionName("HrIsAdapterInstalled2");
  186. HRESULT hr=S_OK;
  187. HDEVINFO hdi;
  188. SP_DEVINFO_DATA deid;
  189. tstring strInstance;
  190. PWSTR pszPnpDevNodeId=NULL;
  191. hr = pncc->GetPnpDevNodeId(&pszPnpDevNodeId);
  192. if (S_OK == hr)
  193. {
  194. // Use the instanceID to get the key to the parameters
  195. // using Device Installer APIs (PNP)
  196. //
  197. hr = HrSetupDiCreateDeviceInfoList(&GUID_DEVCLASS_NET, NULL, &hdi);
  198. if (S_OK == hr)
  199. {
  200. // Open the devnode.
  201. //
  202. SP_DEVINFO_DATA deid;
  203. hr = HrSetupDiOpenDeviceInfo(hdi, pszPnpDevNodeId, NULL,
  204. 0, &deid);
  205. if (S_OK == hr)
  206. {
  207. hr = HrGetCompatibleIds(hdi, &deid, ppmszCompatibleIds);
  208. }
  209. SetupDiDestroyDeviceInfoList(hdi);
  210. }
  211. CoTaskMemFree(pszPnpDevNodeId);
  212. }
  213. TraceErrorSkip1(__FUNCNAME__, hr, S_FALSE);
  214. return hr;
  215. }