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.

260 lines
7.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: isbound.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include <netcfgx.h>
  12. #include <devguid.h>
  13. // Note: ReleaseObj() checks for NULL before actually releasing the pointer
  14. ULONG APIENTRY
  15. ReleaseObj (
  16. void* punk)
  17. {
  18. return (punk) ? (((IUnknown*)punk)->Release()) : 0;
  19. }
  20. HRESULT HrGetINetCfg(IN BOOL fGetWriteLock,
  21. INetCfg** ppnc)
  22. {
  23. HRESULT hr=S_OK;
  24. // Initialize the output parameters.
  25. *ppnc = NULL;
  26. // initialize COM
  27. hr = CoInitializeEx(NULL,
  28. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  29. if (SUCCEEDED(hr))
  30. {
  31. // Create the object implementing INetCfg.
  32. //
  33. INetCfg* pnc;
  34. hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER,
  35. IID_INetCfg, (void**)&pnc);
  36. if (SUCCEEDED(hr))
  37. {
  38. INetCfgLock * pncLock = NULL;
  39. if (fGetWriteLock)
  40. {
  41. // Get the locking interface
  42. hr = pnc->QueryInterface(IID_INetCfgLock,
  43. (LPVOID *)&pncLock);
  44. if (SUCCEEDED(hr))
  45. {
  46. // Attempt to lock the INetCfg for read/write
  47. static const ULONG c_cmsTimeout = 15000;
  48. static const TCHAR c_szSampleNetcfgApp[] =
  49. TEXT("Routing and Remote Access Manager (mprsnap.dll)");
  50. LPTSTR szLockedBy;
  51. hr = pncLock->AcquireWriteLock(c_cmsTimeout, c_szSampleNetcfgApp,
  52. &szLockedBy);
  53. if (S_FALSE == hr)
  54. {
  55. hr = NETCFG_E_NO_WRITE_LOCK;
  56. _tprintf(TEXT("Could not lock INetcfg, it is already locked by '%s'"), szLockedBy);
  57. }
  58. }
  59. }
  60. if (SUCCEEDED(hr))
  61. {
  62. // Initialize the INetCfg object.
  63. //
  64. hr = pnc->Initialize(NULL);
  65. if (SUCCEEDED(hr))
  66. {
  67. *ppnc = pnc;
  68. pnc->AddRef();
  69. }
  70. else
  71. {
  72. // initialize failed, if obtained lock, release it
  73. if (pncLock)
  74. {
  75. pncLock->ReleaseWriteLock();
  76. }
  77. }
  78. }
  79. ReleaseObj(pncLock);
  80. ReleaseObj(pnc);
  81. }
  82. if (FAILED(hr))
  83. {
  84. CoUninitialize();
  85. }
  86. }
  87. return hr;
  88. }
  89. //+---------------------------------------------------------------------------
  90. //
  91. // Function: HrReleaseINetCfg
  92. //
  93. // Purpose: Uninitialize INetCfg, release write lock (if present)
  94. // and uninitialize COM.
  95. //
  96. // Arguments:
  97. // fHasWriteLock [in] whether write lock needs to be released.
  98. // pnc [in] pointer to INetCfg object
  99. //
  100. // Returns: S_OK on success, otherwise an error code
  101. //
  102. // Author: kumarp 01-October-98
  103. //
  104. // Notes:
  105. //
  106. HRESULT HrReleaseINetCfg(BOOL fHasWriteLock, INetCfg* pnc)
  107. {
  108. HRESULT hr = S_OK;
  109. // uninitialize INetCfg
  110. hr = pnc->Uninitialize();
  111. // if write lock is present, unlock it
  112. if (SUCCEEDED(hr) && fHasWriteLock)
  113. {
  114. INetCfgLock* pncLock;
  115. // Get the locking interface
  116. hr = pnc->QueryInterface(IID_INetCfgLock,
  117. (LPVOID *)&pncLock);
  118. if (SUCCEEDED(hr))
  119. {
  120. hr = pncLock->ReleaseWriteLock();
  121. ReleaseObj(pncLock);
  122. }
  123. }
  124. ReleaseObj(pnc);
  125. CoUninitialize();
  126. return hr;
  127. }
  128. BOOL IsProtocolBoundToAdapter(INetCfg * pnc, INetCfgComponent* pncc, LPGUID pguid)
  129. {
  130. HRESULT hr;
  131. BOOL fBound = FALSE;
  132. BOOL fFound = FALSE;
  133. INetCfgClass* pncclass;
  134. // Get the Adapter Class
  135. //
  136. hr = pnc->QueryNetCfgClass(&GUID_DEVCLASS_NET, IID_INetCfgClass,
  137. reinterpret_cast<void**>(&pncclass));
  138. if (SUCCEEDED(hr))
  139. {
  140. IEnumNetCfgComponent* pencc = NULL;
  141. INetCfgComponent* pnccAdapter = NULL;
  142. ULONG celtFetched;
  143. // Search for the adapter in question
  144. //
  145. hr = pncclass->EnumComponents(&pencc);
  146. while (SUCCEEDED(hr) && (S_OK == (hr = pencc->Next(1, &pnccAdapter, &celtFetched))))
  147. {
  148. GUID guidAdapter;
  149. // Get the adapter's instance ID
  150. //
  151. hr = pnccAdapter->GetInstanceGuid(&guidAdapter);
  152. if (SUCCEEDED(hr))
  153. {
  154. // Is this the one we're looking for?
  155. //
  156. if (*pguid == guidAdapter)
  157. {
  158. INetCfgComponentBindings* pnccBind = NULL;
  159. // Get the Bindings interface and check if we're bound
  160. //
  161. hr = pncc->QueryInterface (IID_INetCfgComponentBindings,
  162. reinterpret_cast<VOID**>(&pnccBind));
  163. if (SUCCEEDED(hr))
  164. {
  165. // Is the protocol bound to this adapter?
  166. //
  167. hr = pnccBind->IsBoundTo (pnccAdapter);
  168. if (S_OK == hr)
  169. {
  170. fBound = TRUE;
  171. }
  172. pnccBind->Release();
  173. }
  174. // We found the adapter, no need to search further
  175. //
  176. fFound = TRUE;
  177. }
  178. }
  179. ReleaseObj(pnccAdapter);
  180. }
  181. ReleaseObj(pencc);
  182. ReleaseObj(pncclass);
  183. }
  184. return fBound;
  185. }
  186. BOOL FIsAppletalkBoundToAdapter(INetCfg * pnc, LPWSTR pszwInstanceGuid)
  187. {
  188. BOOL fBound = FALSE;
  189. GUID guidInstance;
  190. HRESULT hr;
  191. // change the instance guid string to a guid
  192. //
  193. hr = IIDFromString(const_cast<LPTSTR>(pszwInstanceGuid),
  194. static_cast<LPIID>(&guidInstance));
  195. if (SUCCEEDED(hr))
  196. {
  197. INetCfgClass* pncclass;
  198. // Find the Appletalk component
  199. //
  200. hr = pnc->QueryNetCfgClass(&GUID_DEVCLASS_NETTRANS, IID_INetCfgClass,
  201. reinterpret_cast<void**>(&pncclass));
  202. if (SUCCEEDED(hr))
  203. {
  204. INetCfgComponent* pnccAtlk = NULL;
  205. hr = pncclass->FindComponent(NETCFG_TRANS_CID_MS_APPLETALK, &pnccAtlk);
  206. // This call may succeed, but return S_FALSE if
  207. // Appletalk is not installed. Thus, we need to
  208. // check for S_OK.
  209. if (FHrOK(hr))
  210. {
  211. Assert(pnccAtlk);
  212. fBound = IsProtocolBoundToAdapter(pnc, pnccAtlk, &guidInstance);
  213. ReleaseObj(pnccAtlk);
  214. }
  215. ReleaseObj(pncclass);
  216. }
  217. else
  218. DisplayErrorMessage(NULL, hr);
  219. }
  220. return fBound;
  221. }