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.

387 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C N E T C F G . C P P
  7. //
  8. // Contents: Common routines for dealing with INetCfg interfaces.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "ncbase.h"
  18. #include "ncdebug.h"
  19. #include "ncnetcfg.h"
  20. #include "netcfgx.h"
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Function: HrCreateAndInitializeINetCfg
  24. //
  25. // Purpose: Cocreate and initialize the root INetCfg object. This will
  26. // optionally initialize COM for the caller too.
  27. //
  28. // Arguments:
  29. // pfInitCom [in,out] TRUE to call CoInitialize before creating.
  30. // returns TRUE if COM was successfully
  31. // initialized FALSE if not. If NULL, means
  32. // don't initialize COM.
  33. // ppnc [out] The returned INetCfg object.
  34. // fGetWriteLock [in] TRUE if a writable INetCfg is needed
  35. // cmsTimeout [in] See INetCfg::AcquireWriteLock
  36. // pszClientDesc [in] See INetCfg::AcquireWriteLock
  37. // ppszClientDesc [out] See INetCfg::AcquireWriteLock
  38. //
  39. // Returns: S_OK or an error code.
  40. //
  41. // Author: shaunco 7 May 1997
  42. //
  43. // Notes:
  44. //
  45. HRESULT
  46. HrCreateAndInitializeINetCfg (
  47. BOOL* pfInitCom,
  48. INetCfg** ppnc,
  49. BOOL fGetWriteLock,
  50. DWORD cmsTimeout,
  51. PCWSTR pszClientDesc,
  52. PWSTR* ppszClientDesc)
  53. {
  54. Assert (ppnc);
  55. // Initialize the output parameters.
  56. *ppnc = NULL;
  57. if (ppszClientDesc)
  58. {
  59. *ppszClientDesc = NULL;
  60. }
  61. // Initialize COM if the caller requested.
  62. HRESULT hr = S_OK;
  63. if (pfInitCom && *pfInitCom)
  64. {
  65. hr = CoInitializeEx( NULL,
  66. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  67. if (RPC_E_CHANGED_MODE == hr)
  68. {
  69. hr = S_OK;
  70. *pfInitCom = FALSE;
  71. }
  72. }
  73. if (SUCCEEDED(hr))
  74. {
  75. // Create the object implementing INetCfg.
  76. //
  77. INetCfg* pnc;
  78. hr = CoCreateInstance(
  79. CLSID_CNetCfg,
  80. NULL,
  81. CLSCTX_INPROC_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  82. IID_INetCfg,
  83. reinterpret_cast<void**>(&pnc));
  84. if (SUCCEEDED(hr))
  85. {
  86. INetCfgLock * pnclock = NULL;
  87. if (fGetWriteLock)
  88. {
  89. // Get the locking interface
  90. hr = pnc->QueryInterface(IID_INetCfgLock,
  91. reinterpret_cast<LPVOID *>(&pnclock));
  92. if (SUCCEEDED(hr))
  93. {
  94. // Attempt to lock the INetCfg for read/write
  95. hr = pnclock->AcquireWriteLock(cmsTimeout, pszClientDesc,
  96. ppszClientDesc);
  97. if (S_FALSE == hr)
  98. {
  99. // Couldn't acquire the lock
  100. hr = NETCFG_E_NO_WRITE_LOCK;
  101. }
  102. }
  103. }
  104. if (SUCCEEDED(hr))
  105. {
  106. // Initialize the INetCfg object.
  107. //
  108. hr = pnc->Initialize (NULL);
  109. if (SUCCEEDED(hr))
  110. {
  111. *ppnc = pnc;
  112. AddRefObj (pnc);
  113. }
  114. else
  115. {
  116. if (pnclock)
  117. {
  118. pnclock->ReleaseWriteLock();
  119. }
  120. }
  121. // Transfer reference to caller.
  122. }
  123. ReleaseObj(pnclock);
  124. ReleaseObj(pnc);
  125. }
  126. // If we failed anything above, and we've initialized COM,
  127. // be sure an uninitialize it.
  128. //
  129. if (FAILED(hr) && pfInitCom && *pfInitCom)
  130. {
  131. CoUninitialize ();
  132. }
  133. }
  134. TraceError("HrCreateAndInitializeINetCfg", hr);
  135. return hr;
  136. }
  137. //+---------------------------------------------------------------------------
  138. //
  139. // Function: HrUninitializeAndReleaseINetCfg
  140. //
  141. // Purpose: Unintialize and release an INetCfg object. This will
  142. // optionally uninitialize COM for the caller too.
  143. //
  144. // Arguments:
  145. // fUninitCom [in] TRUE to uninitialize COM after the INetCfg is
  146. // uninitialized and released.
  147. // pnc [in] The INetCfg object.
  148. // fHasLock [in] TRUE if the INetCfg was locked for write and
  149. // must be unlocked.
  150. //
  151. // Returns: S_OK or an error code.
  152. //
  153. // Author: shaunco 7 May 1997
  154. //
  155. // Notes: The return value is the value returned from
  156. // INetCfg::Uninitialize. Even if this fails, the INetCfg
  157. // is still released. Therefore, the return value is for
  158. // informational purposes only. You can't touch the INetCfg
  159. // object after this call returns.
  160. //
  161. HRESULT
  162. HrUninitializeAndReleaseINetCfg (
  163. BOOL fUninitCom,
  164. INetCfg* pnc,
  165. BOOL fHasLock)
  166. {
  167. Assert (pnc);
  168. HRESULT hr = S_OK;
  169. if (fHasLock)
  170. {
  171. hr = HrUninitializeAndUnlockINetCfg(pnc);
  172. }
  173. else
  174. {
  175. hr = pnc->Uninitialize ();
  176. }
  177. ReleaseObj (pnc);
  178. if (fUninitCom)
  179. {
  180. CoUninitialize ();
  181. }
  182. TraceError("HrUninitializeAndReleaseINetCfg", hr);
  183. return hr;
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // Function: HrUninitializeAndUnlockINetCfg
  188. //
  189. // Purpose: Uninitializes and unlocks the INetCfg object
  190. //
  191. // Arguments:
  192. // pnc [in] INetCfg to uninitialize and unlock
  193. //
  194. // Returns: S_OK if success, OLE or Win32 error otherwise
  195. //
  196. // Author: danielwe 13 Nov 1997
  197. //
  198. // Notes:
  199. //
  200. HRESULT
  201. HrUninitializeAndUnlockINetCfg (
  202. INetCfg* pnc)
  203. {
  204. HRESULT hr = S_OK;
  205. hr = pnc->Uninitialize();
  206. if (SUCCEEDED(hr))
  207. {
  208. INetCfgLock * pnclock;
  209. // Get the locking interface
  210. hr = pnc->QueryInterface(IID_INetCfgLock,
  211. reinterpret_cast<LPVOID *>(&pnclock));
  212. if (SUCCEEDED(hr))
  213. {
  214. // Attempt to lock the INetCfg for read/write
  215. hr = pnclock->ReleaseWriteLock();
  216. ReleaseObj(pnclock);
  217. }
  218. }
  219. TraceError("HrUninitializeAndUnlockINetCfg", hr);
  220. return hr;
  221. }
  222. //+---------------------------------------------------------------------------
  223. //
  224. // Function: HrIsLanCapableAdapter
  225. //
  226. // Purpose: Returns whether the given component (adapter) is capable of
  227. // being associated with a LAN connection
  228. //
  229. // Arguments:
  230. // pncc [in] Component to test
  231. //
  232. // Returns: S_OK if it is capable, S_FALSE if not, OLE or Win32 error code
  233. // otherwise
  234. //
  235. // Author: danielwe 13 Nov 1997
  236. //
  237. // Notes:
  238. //
  239. HRESULT
  240. HrIsLanCapableAdapter (
  241. INetCfgComponent* pncc)
  242. {
  243. Assert(pncc);
  244. INetCfgComponentBindings* pnccb;
  245. HRESULT hr = pncc->QueryInterface(IID_INetCfgComponentBindings,
  246. reinterpret_cast<LPVOID *>(&pnccb));
  247. if (S_OK == hr)
  248. {
  249. // Does it have ndis4?...
  250. extern const WCHAR c_szBiNdis4[];
  251. hr = pnccb->SupportsBindingInterface(NCF_UPPER, c_szBiNdis4);
  252. if (S_FALSE == hr)
  253. {
  254. // ... no.. how about ndisatm?
  255. extern const WCHAR c_szBiNdisAtm[];
  256. hr = pnccb->SupportsBindingInterface(NCF_UPPER, c_szBiNdisAtm);
  257. if (S_FALSE == hr)
  258. {
  259. // .. let's try ndis5 then
  260. extern const WCHAR c_szBiNdis5[];
  261. hr = pnccb->SupportsBindingInterface(NCF_UPPER, c_szBiNdis5);
  262. if (S_FALSE == hr)
  263. {
  264. // .. let's try ndis5_ip then
  265. extern const WCHAR c_szBiNdis5Ip[];
  266. hr = pnccb->SupportsBindingInterface(NCF_UPPER, c_szBiNdis5Ip);
  267. if (S_FALSE == hr)
  268. {
  269. // .. let's try LocalTalk then (this is an adapters lower interface)
  270. extern const WCHAR c_szBiLocalTalk[];
  271. hr = pnccb->SupportsBindingInterface(NCF_LOWER, c_szBiLocalTalk);
  272. // ... no.. how about ndis1394?
  273. if (S_FALSE == hr)
  274. {
  275. extern const WCHAR c_szBiNdis1394[];
  276. hr = pnccb->SupportsBindingInterface(NCF_UPPER,
  277. c_szBiNdis1394);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. ReleaseObj(pnccb);
  284. }
  285. TraceError("HrIsLanCapableAdapter", (hr == S_FALSE) ? S_OK : hr);
  286. return hr;
  287. }
  288. //+---------------------------------------------------------------------------
  289. //
  290. // Function: HrIsLanCapableProtocol
  291. //
  292. // Purpose: Returns whether the given component (protocol) is capable of
  293. // being associated with a LAN connection
  294. //
  295. // Arguments:
  296. // pncc [in] Component to test
  297. //
  298. // Returns: S_OK if it is capable, S_FALSE if not, OLE or Win32 error code
  299. // otherwise
  300. //
  301. // Author: danielwe 13 Nov 1997
  302. //
  303. // Notes:
  304. //
  305. HRESULT
  306. HrIsLanCapableProtocol (
  307. INetCfgComponent* pncc)
  308. {
  309. Assert(pncc);
  310. INetCfgComponentBindings* pnccb;
  311. HRESULT hr = pncc->QueryInterface(IID_INetCfgComponentBindings,
  312. reinterpret_cast<LPVOID *>(&pnccb));
  313. if (S_OK == hr)
  314. {
  315. // Does it have ndis4?...
  316. extern const WCHAR c_szBiNdis4[];
  317. hr = pnccb->SupportsBindingInterface(NCF_LOWER, c_szBiNdis4);
  318. if (S_FALSE == hr)
  319. {
  320. // ... no.. how about ndisatm?
  321. extern const WCHAR c_szBiNdisAtm[];
  322. hr = pnccb->SupportsBindingInterface(NCF_LOWER, c_szBiNdisAtm);
  323. if (S_FALSE == hr)
  324. {
  325. // .. let's try ndis5 then
  326. extern const WCHAR c_szBiNdis5[];
  327. hr = pnccb->SupportsBindingInterface(NCF_LOWER, c_szBiNdis5);
  328. if (S_FALSE == hr)
  329. {
  330. // .. let's try ndis5_ip then
  331. extern const WCHAR c_szBiNdis5Ip[];
  332. hr = pnccb->SupportsBindingInterface(NCF_LOWER, c_szBiNdis5Ip);
  333. }
  334. }
  335. }
  336. ReleaseObj(pnccb);
  337. }
  338. // Raid 147474 : NDISUIO: No warning when you uninstall all the protocols.
  339. // mbend 7/20/2000
  340. //
  341. // Don't consider a hidden protocol a valid Lanui protocol.
  342. if(S_OK == hr)
  343. {
  344. DWORD dwChar = 0;
  345. hr = pncc->GetCharacteristics(&dwChar);
  346. if(SUCCEEDED(hr))
  347. {
  348. if(NCF_HIDDEN & dwChar)
  349. {
  350. hr = S_FALSE;
  351. }
  352. }
  353. }
  354. TraceError("HrIsLanCapableAdapter", (hr == S_FALSE) ? S_OK : hr);
  355. return hr;
  356. }