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.

217 lines
5.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2000.
  5. //
  6. // File: N C U T I L . C P P
  7. //
  8. // Contents: INetCfg utilities. This is all a candidate to be moved into
  9. // nccommon\src\ncnetcfg.cpp.
  10. //
  11. // Notes:
  12. //
  13. // Author: shaunco 28 Mar 1997
  14. //
  15. //----------------------------------------------------------------------------
  16. #include "pch.h"
  17. #pragma hdrstop
  18. #include "ncnetcfg.h"
  19. #include "ncutil.h"
  20. extern const WCHAR c_szInfId_MS_NdisWanIp[];
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Function: HrEnsureZeroOrOneAdapter
  24. //
  25. // Purpose:
  26. //
  27. // Arguments:
  28. // pnc []
  29. // pszComponentId []
  30. // dwFlags []
  31. //
  32. // Returns:
  33. //
  34. // Author: shaunco 5 Dec 1997
  35. //
  36. // Notes:
  37. //
  38. HRESULT
  39. HrEnsureZeroOrOneAdapter (
  40. INetCfg* pnc,
  41. PCWSTR pszComponentId,
  42. DWORD dwFlags)
  43. {
  44. HRESULT hr = S_OK;
  45. if (dwFlags & ARA_ADD)
  46. {
  47. // Make sure we have one present.
  48. //
  49. if (!FIsAdapterInstalled (pnc, pszComponentId))
  50. {
  51. TraceTag (ttidRasCfg, "Adding %S", pszComponentId);
  52. hr = HrAddOrRemoveAdapter (pnc, pszComponentId,
  53. ARA_ADD, NULL, 1, NULL);
  54. }
  55. }
  56. else
  57. {
  58. // Make sure we have none present.
  59. //
  60. TraceTag (ttidRasCfg, "Removing %S", pszComponentId);
  61. hr = HrFindAndRemoveAllInstancesOfAdapter (pnc, pszComponentId);
  62. }
  63. TraceHr (ttidError, FAL, hr, FALSE, "HrEnsureZeroOrOneAdapter");
  64. return hr;
  65. }
  66. //+---------------------------------------------------------------------------
  67. //
  68. // Function: HrGetInstanceGuidAsString
  69. //
  70. // Purpose:
  71. //
  72. // Arguments:
  73. // pncc []
  74. // pszGuid []
  75. // cchGuid []
  76. //
  77. // Returns:
  78. //
  79. // Author: shaunco 14 Jun 1997
  80. //
  81. // Notes:
  82. //
  83. HRESULT
  84. HrGetInstanceGuidAsString (
  85. INetCfgComponent* pncc,
  86. PWSTR pszGuid,
  87. INT cchGuid)
  88. {
  89. GUID guid;
  90. HRESULT hr = pncc->GetInstanceGuid (&guid);
  91. if(SUCCEEDED(hr))
  92. {
  93. if (0 == StringFromGUID2(guid, pszGuid, cchGuid))
  94. {
  95. hr = E_INVALIDARG;
  96. }
  97. }
  98. TraceHr (ttidError, FAL, hr, FALSE, "HrGetInstanceGuidAsString");
  99. return hr;
  100. }
  101. //+---------------------------------------------------------------------------
  102. //
  103. // Function: HrMapComponentIdToDword
  104. //
  105. // Purpose: Maps a component's id to a DWORD value. The mapping is
  106. // specified by the caller through an array of pointers to
  107. // string values and their associated DWORD values.
  108. //
  109. // Arguments:
  110. // pncc [in] pointer to component.
  111. // aMapSzDword [in] array of elements mapping a string to a DWORD.
  112. // cMapSzDword [in] count of elements in the array.
  113. // pdwValue [out] the returned value.
  114. //
  115. // Returns: S_OK if a match was found. If a match wasn't found,
  116. // S_FALSE is returned.
  117. // Other Win32 error codes.
  118. //
  119. // Author: shaunco 17 May 1997
  120. //
  121. // Notes:
  122. //
  123. HRESULT
  124. HrMapComponentIdToDword (
  125. INetCfgComponent* pncc,
  126. const MAP_SZ_DWORD* aMapSzDword,
  127. UINT cMapSzDword,
  128. DWORD* pdwValue)
  129. {
  130. Assert (pncc);
  131. Assert (aMapSzDword);
  132. Assert (cMapSzDword);
  133. Assert (pdwValue);
  134. // Initialize output parameter.
  135. *pdwValue = 0;
  136. PWSTR pszwId;
  137. HRESULT hr = pncc->GetId (&pszwId);
  138. if (SUCCEEDED(hr))
  139. {
  140. hr = S_FALSE;
  141. while (cMapSzDword--)
  142. {
  143. if (FEqualComponentId (pszwId, aMapSzDword->pszValue))
  144. {
  145. *pdwValue = aMapSzDword->dwValue;
  146. hr = S_OK;
  147. break;
  148. }
  149. aMapSzDword++;
  150. }
  151. CoTaskMemFree (pszwId);
  152. }
  153. TraceHr (ttidError, FAL, hr, S_FALSE == hr,
  154. "HrMapComponentIdToDword");
  155. return hr;
  156. }
  157. //+---------------------------------------------------------------------------
  158. //
  159. // Function: HrOpenComponentParamKey
  160. //
  161. // Purpose: Find a component and open its parameter key.
  162. //
  163. // Arguments:
  164. // pnc [in]
  165. // rguidClass [in]
  166. // pszComponentId [in]
  167. // phkey [out]
  168. //
  169. // Returns: S_OK if the component was found the key was opened.
  170. // S_FALSE if the compoennt was not found.
  171. // error code.
  172. //
  173. // Author: shaunco 13 Apr 1997
  174. //
  175. // Notes:
  176. //
  177. HRESULT
  178. HrOpenComponentParamKey (
  179. INetCfg* pnc,
  180. const GUID& rguidClass,
  181. PCWSTR pszComponentId,
  182. HKEY* phkey)
  183. {
  184. Assert (pnc);
  185. Assert (pszComponentId);
  186. Assert (phkey);
  187. // Initialize the output parameter.
  188. *phkey = NULL;
  189. // Find the component.
  190. INetCfgComponent* pncc;
  191. HRESULT hr = pnc->FindComponent ( pszComponentId, &pncc);
  192. if (S_OK == hr)
  193. {
  194. // Open its param key.
  195. hr = pncc->OpenParamKey (phkey);
  196. ReleaseObj (pncc);
  197. }
  198. TraceHr (ttidError, FAL, hr, S_FALSE == hr,
  199. "HrOpenComponentParamKey");
  200. return hr;
  201. }