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.

1354 lines
32 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000 - 2001
  5. //
  6. // File: H N A P I . H
  7. //
  8. // Contents: OEM API
  9. //
  10. // Notes:
  11. //
  12. // Author: billi 21 Nov 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #define HNET_OEM_API_ENTER try {
  17. #define HNET_OEM_API_LEAVE } catch (...) { return DISP_E_EXCEPTION; }
  18. #include <eh.h>
  19. class HNet_Oem_SEH_Exception
  20. {
  21. private:
  22. unsigned int m_uSECode;
  23. public:
  24. HNet_Oem_SEH_Exception(unsigned int uSECode) : m_uSECode(uSECode) {}
  25. HNet_Oem_SEH_Exception() {}
  26. ~HNet_Oem_SEH_Exception() {}
  27. unsigned int getSeHNumber() { return m_uSECode; }
  28. };
  29. void __cdecl hnet_oem_trans_func( unsigned int uSECode, EXCEPTION_POINTERS* pExp );
  30. void EnableOEMExceptionHandling();
  31. void DisableOEMExceptionHandling();
  32. #ifndef IID_PPV_ARG
  33. #define IID_PPV_ARG(Type, Expr) \
  34. __uuidof(Type), reinterpret_cast<void**>(static_cast<Type **>((Expr)))
  35. #endif
  36. #ifndef ReleaseObj
  37. #define ReleaseObj(obj) (( obj ) ? (obj)->Release() : 0)
  38. #endif
  39. BOOLEAN IsSecureContext();
  40. BOOLEAN IsNotifyApproved();
  41. HRESULT InitializeOemApi( HINSTANCE hInstance );
  42. HRESULT ReleaseOemApi();
  43. HRESULT _ObtainIcsSettingsObj( IHNetIcsSettings** ppIcsSettings );
  44. // structs
  45. typedef struct tagICSPortMapping
  46. {
  47. OLECHAR *pszwName;
  48. UCHAR ucIPProtocol;
  49. USHORT usExternalPort;
  50. USHORT usInternalPort;
  51. DWORD dwOptions;
  52. OLECHAR *pszwTargetName;
  53. OLECHAR *pszwTargetIPAddress;
  54. VARIANT_BOOL bEnabled;
  55. }
  56. ICS_PORTMAPPING, *LPICS_PORTMAPPING;
  57. typedef struct tagICS_RESPONSE_RANGE
  58. {
  59. UCHAR ucIPProtocol;
  60. USHORT usStartPort;
  61. USHORT usEndPort;
  62. }
  63. ICS_RESPONSE_RANGE, *LPICS_RESPONSE_RANGE;
  64. typedef struct tagICS_APPLICATION_DEFINITION
  65. {
  66. VARIANT_BOOL bEnabled;
  67. OLECHAR *pszwName;
  68. UCHAR ucIPProtocol;
  69. USHORT usOutgoingPort;
  70. DWORD dwOptions;
  71. USHORT uscResponses;
  72. ICS_RESPONSE_RANGE lpResponse[1];
  73. }
  74. ICS_APPLICATION_DEFINITION, *LPICS_APPLICATION_DEFINITION;
  75. class ATL_NO_VTABLE CNetConnectionProps :
  76. public CComObjectRootEx<CComMultiThreadModel>,
  77. public IDispatchImpl<INetConnectionProps, &IID_INetConnectionProps, &LIBID_NETCONLib>
  78. {
  79. private:
  80. INetConnection* m_pNetConnection;
  81. public:
  82. BEGIN_COM_MAP(CNetConnectionProps)
  83. COM_INTERFACE_ENTRY(INetConnectionProps)
  84. COM_INTERFACE_ENTRY(IDispatch)
  85. END_COM_MAP()
  86. CNetConnectionProps()
  87. {
  88. m_pNetConnection = NULL;
  89. }
  90. ~CNetConnectionProps()
  91. {
  92. ReleaseObj (m_pNetConnection);
  93. }
  94. HRESULT Initialize (INetConnection * pNC)
  95. {
  96. _ASSERT (pNC);
  97. if (!pNC)
  98. return E_POINTER;
  99. ReleaseObj (m_pNetConnection);
  100. m_pNetConnection = pNC;
  101. m_pNetConnection->AddRef();
  102. return S_OK;
  103. }
  104. // INetConnectionProps
  105. STDMETHODIMP get_Guid (BSTR * pbstrGuid)
  106. {
  107. HNET_OEM_API_ENTER
  108. if (!pbstrGuid)
  109. return E_POINTER;
  110. *pbstrGuid = NULL;
  111. _ASSERT (m_pNetConnection);
  112. if (!m_pNetConnection)
  113. return E_UNEXPECTED;
  114. NETCON_PROPERTIES * pNCP = NULL;
  115. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  116. if (pNCP) {
  117. LPOLESTR lpo = NULL;
  118. hr = StringFromCLSID (pNCP->guidId, &lpo);
  119. if (lpo) {
  120. *pbstrGuid = SysAllocString (lpo);
  121. if (!*pbstrGuid)
  122. hr = E_OUTOFMEMORY;
  123. CoTaskMemFree (lpo);
  124. }
  125. NcFreeNetconProperties (pNCP);
  126. }
  127. return hr;
  128. HNET_OEM_API_LEAVE
  129. }
  130. STDMETHODIMP get_Name (BSTR * pbstrName)
  131. {
  132. HNET_OEM_API_ENTER
  133. if (!pbstrName)
  134. return E_POINTER;
  135. *pbstrName = NULL;
  136. _ASSERT (m_pNetConnection);
  137. if (!m_pNetConnection)
  138. return E_UNEXPECTED;
  139. NETCON_PROPERTIES * pNCP = NULL;
  140. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  141. if (pNCP) {
  142. *pbstrName = SysAllocString (pNCP->pszwName);
  143. if (!*pbstrName)
  144. hr = E_OUTOFMEMORY;
  145. NcFreeNetconProperties (pNCP);
  146. }
  147. return hr;
  148. HNET_OEM_API_LEAVE
  149. }
  150. STDMETHODIMP get_DeviceName(BSTR * pbstrDeviceName)
  151. {
  152. HNET_OEM_API_ENTER
  153. if (!pbstrDeviceName)
  154. return E_POINTER;
  155. *pbstrDeviceName = NULL;
  156. _ASSERT (m_pNetConnection);
  157. if (!m_pNetConnection)
  158. return E_UNEXPECTED;
  159. NETCON_PROPERTIES * pNCP = NULL;
  160. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  161. if (pNCP) {
  162. *pbstrDeviceName = SysAllocString (pNCP->pszwDeviceName);
  163. if (!*pbstrDeviceName)
  164. hr = E_OUTOFMEMORY;
  165. NcFreeNetconProperties (pNCP);
  166. }
  167. return hr;
  168. HNET_OEM_API_LEAVE
  169. }
  170. STDMETHODIMP get_Status (NETCON_STATUS * pStatus)
  171. {
  172. HNET_OEM_API_ENTER
  173. if (!pStatus)
  174. return E_POINTER;
  175. // *pStatus = NULL;
  176. _ASSERT (m_pNetConnection);
  177. if (!m_pNetConnection)
  178. return E_UNEXPECTED;
  179. NETCON_PROPERTIES * pNCP = NULL;
  180. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  181. if (pNCP) {
  182. *pStatus = pNCP->Status;
  183. NcFreeNetconProperties (pNCP);
  184. }
  185. return hr;
  186. HNET_OEM_API_LEAVE
  187. }
  188. STDMETHODIMP get_MediaType (NETCON_MEDIATYPE * pMediaType)
  189. {
  190. HNET_OEM_API_ENTER
  191. if (!pMediaType)
  192. return E_POINTER;
  193. *pMediaType = NCM_NONE;
  194. _ASSERT (m_pNetConnection);
  195. if (!m_pNetConnection)
  196. return E_UNEXPECTED;
  197. NETCON_PROPERTIES * pNCP = NULL;
  198. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  199. if (pNCP) {
  200. *pMediaType = pNCP->MediaType;
  201. NcFreeNetconProperties (pNCP);
  202. }
  203. return hr;
  204. HNET_OEM_API_LEAVE
  205. }
  206. STDMETHODIMP get_Characteristics (DWORD * pdwFlags)
  207. {
  208. HNET_OEM_API_ENTER
  209. if (!pdwFlags)
  210. return E_POINTER;
  211. *pdwFlags = NCCF_NONE;
  212. _ASSERT (m_pNetConnection);
  213. if (!m_pNetConnection)
  214. return E_UNEXPECTED;
  215. NETCON_PROPERTIES * pNCP = NULL;
  216. HRESULT hr = m_pNetConnection->GetProperties (&pNCP);
  217. if (pNCP) {
  218. *pdwFlags = pNCP->dwCharacter;
  219. NcFreeNetconProperties (pNCP);
  220. }
  221. return hr;
  222. HNET_OEM_API_LEAVE
  223. }
  224. };
  225. class ATL_NO_VTABLE CNetSharingConfiguration :
  226. public CComObjectRootEx<CComMultiThreadModel>,
  227. public IDispatchImpl<INetSharingConfiguration, &IID_INetSharingConfiguration, &LIBID_NETCONLib>
  228. {
  229. private:
  230. IHNetConnection* m_pHNetConnection;
  231. IHNetProtocolSettings* m_pSettings;
  232. CRITICAL_SECTION m_csSharingConfiguration;
  233. // private method called from 2 wrappers below (get_SharingEnabled, get_SharingEnabledType)
  234. STDMETHODIMP GetSharingEnabled (BOOLEAN* pbEnabled, SHARINGCONNECTIONTYPE* pType);
  235. // this was necessary because oleautomation allows only one retval type
  236. public:
  237. BEGIN_COM_MAP(CNetSharingConfiguration)
  238. COM_INTERFACE_ENTRY(INetSharingConfiguration)
  239. COM_INTERFACE_ENTRY(IDispatch)
  240. END_COM_MAP()
  241. CNetSharingConfiguration()
  242. {
  243. m_pHNetConnection = NULL;
  244. m_pSettings = NULL;
  245. InitializeCriticalSection(&m_csSharingConfiguration);
  246. }
  247. ~CNetSharingConfiguration()
  248. {
  249. DeleteCriticalSection(&m_csSharingConfiguration);
  250. }
  251. HRESULT
  252. Initialize(
  253. INetConnection *pNetConnection );
  254. HRESULT
  255. FinalRelease()
  256. {
  257. ReleaseObj(m_pHNetConnection);
  258. ReleaseObj(m_pSettings);
  259. return S_OK;
  260. }
  261. STDMETHODIMP get_SharingEnabled (VARIANT_BOOL* pbEnabled);
  262. STDMETHODIMP get_SharingConnectionType(SHARINGCONNECTIONTYPE* pType);
  263. STDMETHODIMP
  264. DisableSharing();
  265. STDMETHODIMP
  266. EnableSharing(
  267. SHARINGCONNECTIONTYPE Type );
  268. STDMETHODIMP
  269. get_InternetFirewallEnabled(
  270. VARIANT_BOOL *pbEnabled );
  271. STDMETHODIMP
  272. DisableInternetFirewall();
  273. STDMETHODIMP
  274. EnableInternetFirewall();
  275. // Return an IEnumSharingPortMapping interface used to enumerate all of
  276. // the contained INetSharingPortMapping objects.
  277. //
  278. STDMETHODIMP
  279. get_EnumPortMappings(
  280. SHARINGCONNECTION_ENUM_FLAGS Flags,
  281. INetSharingPortMappingCollection** ppColl);
  282. STDMETHODIMP
  283. AddPortMapping(
  284. OLECHAR* pszwName,
  285. UCHAR ucIPProtocol,
  286. USHORT usExternalPort,
  287. USHORT usInternalPort,
  288. DWORD dwOptions,
  289. OLECHAR* pszwTargetNameOrIPAddress,
  290. ICS_TARGETTYPE eTargetType,
  291. INetSharingPortMapping** ppMapping );
  292. STDMETHODIMP
  293. RemovePortMapping(
  294. INetSharingPortMapping* pMapping );
  295. };
  296. class ATL_NO_VTABLE CNetSharingManager :
  297. public CComObjectRootEx<CComMultiThreadModel>,
  298. public CComCoClass<CNetSharingManager, &CLSID_NetSharingManager>,
  299. public IDispatchImpl<INetSharingManager, &IID_INetSharingManager, &LIBID_NETCONLib>
  300. {
  301. private:
  302. IHNetIcsSettings* m_pIcsSettings;
  303. public:
  304. BEGIN_COM_MAP(CNetSharingManager)
  305. COM_INTERFACE_ENTRY(INetSharingManager)
  306. COM_INTERFACE_ENTRY(IDispatch)
  307. END_COM_MAP()
  308. DECLARE_REGISTRY_RESOURCEID(IDR_SHAREMGR)
  309. DECLARE_PROTECT_FINAL_CONSTRUCT()
  310. CNetSharingManager()
  311. {
  312. m_pIcsSettings = NULL;
  313. }
  314. HRESULT FinalConstruct()
  315. {
  316. return _ObtainIcsSettingsObj( &m_pIcsSettings );
  317. }
  318. HRESULT FinalRelease()
  319. {
  320. ReleaseObj( m_pIcsSettings );
  321. return S_OK;
  322. }
  323. STDMETHODIMP
  324. get_SharingInstalled(
  325. VARIANT_BOOL *pbInstalled );
  326. // Return an IEnumNetEveryConnection interface used to enumerate all of
  327. // the contained INetConnections
  328. //
  329. STDMETHODIMP
  330. get_EnumEveryConnection(
  331. INetSharingEveryConnectionCollection** ppColl);
  332. // Return an IEnumNetPublicConnection interface used to enumerate all of
  333. // the contained INetConnections configured as a public adapter
  334. //
  335. STDMETHODIMP
  336. get_EnumPublicConnections(
  337. SHARINGCONNECTION_ENUM_FLAGS Flags,
  338. INetSharingPublicConnectionCollection** ppColl);
  339. // Return an IEnumNetPrivateConnection interface used to enumerate all of
  340. // the contained INetConnections configured as a private adapter
  341. //
  342. STDMETHODIMP
  343. get_EnumPrivateConnections(
  344. SHARINGCONNECTION_ENUM_FLAGS Flags,
  345. INetSharingPrivateConnectionCollection** ppColl);
  346. STDMETHODIMP
  347. get_INetSharingConfigurationForINetConnection(
  348. INetConnection* pNetConnection,
  349. INetSharingConfiguration** ppNetSharingConfiguration
  350. );
  351. STDMETHODIMP
  352. get_NetConnectionProps(
  353. INetConnection * pNetConnection,
  354. INetConnectionProps **ppProps)
  355. {
  356. HNET_OEM_API_ENTER
  357. if (!ppProps)
  358. return E_POINTER;
  359. else
  360. *ppProps = NULL;
  361. if (!pNetConnection)
  362. return E_INVALIDARG;
  363. CComObject<CNetConnectionProps>* pNCP = NULL;
  364. HRESULT hr = CComObject<CNetConnectionProps>::CreateInstance(&pNCP);
  365. if (pNCP) {
  366. pNCP->AddRef();
  367. hr = pNCP->Initialize (pNetConnection);
  368. if (hr == S_OK)
  369. hr = pNCP->QueryInterface (
  370. __uuidof(INetConnectionProps), (void**)ppProps);
  371. pNCP->Release();
  372. }
  373. return hr;
  374. HNET_OEM_API_LEAVE
  375. }
  376. };
  377. template<
  378. class IMapping,
  379. class IProtocol
  380. >
  381. class TNetMapping :
  382. public CComObjectRootEx<CComMultiThreadModel>,
  383. public IMapping
  384. {
  385. private:
  386. IProtocol* m_pIProtocol;
  387. CRITICAL_SECTION m_csDefinition;
  388. protected:
  389. IProtocol* _IProtocol() { return m_pIProtocol; }
  390. IProtocol* _IProtocol( IProtocol* _pIProtocol )
  391. {
  392. if ( m_pIProtocol ) ReleaseObj(m_pIProtocol);
  393. m_pIProtocol = _pIProtocol;
  394. if ( m_pIProtocol ) m_pIProtocol->AddRef();
  395. return m_pIProtocol;
  396. }
  397. CRITICAL_SECTION* _CriticalSection()
  398. {
  399. return &m_csDefinition;
  400. }
  401. public:
  402. typedef TNetMapping<IMapping, IProtocol> _ThisClass;
  403. BEGIN_COM_MAP(_ThisClass)
  404. COM_INTERFACE_ENTRY(IMapping)
  405. END_COM_MAP()
  406. public:
  407. TNetMapping()
  408. {
  409. m_pIProtocol = NULL;
  410. InitializeCriticalSection(&m_csDefinition);
  411. }
  412. ~TNetMapping()
  413. {
  414. DeleteCriticalSection(&m_csDefinition);
  415. }
  416. HRESULT
  417. FinalRelease()
  418. {
  419. ReleaseObj( m_pIProtocol );
  420. return S_OK;
  421. }
  422. HRESULT
  423. Initialize(
  424. IProtocol* pItem)
  425. {
  426. EnterCriticalSection(&m_csDefinition);
  427. _IProtocol( pItem );
  428. LeaveCriticalSection(&m_csDefinition);
  429. return S_OK;
  430. }
  431. STDMETHOD(Disable)(void)
  432. {
  433. HRESULT hr;
  434. if ( !IsNotifyApproved() )
  435. {
  436. hr = E_ACCESSDENIED;
  437. }
  438. else if ( NULL == m_pIProtocol )
  439. {
  440. hr = E_UNEXPECTED;
  441. }
  442. else
  443. {
  444. hr = m_pIProtocol->SetEnabled( FALSE );
  445. }
  446. return hr;
  447. }
  448. STDMETHOD(Enable)(void)
  449. {
  450. HRESULT hr;
  451. if ( !IsNotifyApproved() )
  452. {
  453. hr = E_ACCESSDENIED;
  454. }
  455. else if ( NULL == m_pIProtocol )
  456. {
  457. hr = E_UNEXPECTED;
  458. }
  459. else
  460. {
  461. hr = m_pIProtocol->SetEnabled( TRUE );
  462. }
  463. return hr;
  464. }
  465. };
  466. class ATL_NO_VTABLE CNetSharingPortMapping :
  467. public IDispatchImpl<TNetMapping<INetSharingPortMapping, IHNetPortMappingBinding>, &IID_INetSharingPortMapping, &LIBID_NETCONLib>
  468. {
  469. public:
  470. typedef TNetMapping<INetSharingPortMapping, IHNetPortMappingBinding> _ThisOtherClass;
  471. BEGIN_COM_MAP(CNetSharingPortMapping)
  472. COM_INTERFACE_ENTRY(IDispatch)
  473. COM_INTERFACE_ENTRY_CHAIN(_ThisOtherClass)
  474. END_COM_MAP()
  475. STDMETHODIMP get_Properties (/*[out, retval]*/ INetSharingPortMappingProps ** ppNSPMP);
  476. STDMETHODIMP
  477. Delete();
  478. };
  479. template<
  480. class EnumInterface,
  481. class ItemInterface,
  482. class EnumWrapped,
  483. class ItemWrapped
  484. >
  485. class TNetApiEnum :
  486. public CComObjectRootEx<CComMultiThreadModel>,
  487. public EnumInterface,
  488. public IEnumVARIANT
  489. {
  490. protected:
  491. typedef TNetApiEnum<EnumInterface, ItemInterface, EnumWrapped, ItemWrapped> _ThisClass;
  492. friend class TNetApiEnum<EnumInterface, ItemInterface, EnumWrapped, ItemWrapped>;
  493. SHARINGCONNECTION_ENUM_FLAGS m_fEnumFlags;
  494. EnumWrapped* m_pEnum;
  495. CRITICAL_SECTION m_csEnum;
  496. public:
  497. TNetApiEnum ()
  498. {
  499. m_fEnumFlags = ICSSC_DEFAULT;
  500. m_pEnum = NULL;
  501. InitializeCriticalSection(&m_csEnum);
  502. }
  503. ~TNetApiEnum ()
  504. {
  505. DeleteCriticalSection(&m_csEnum);
  506. }
  507. BEGIN_COM_MAP(_ThisClass)
  508. COM_INTERFACE_ENTRY(EnumInterface)
  509. COM_INTERFACE_ENTRY(IEnumVARIANT)
  510. END_COM_MAP()
  511. DECLARE_PROTECT_FINAL_CONSTRUCT()
  512. HRESULT Initialize( EnumWrapped* pEnum, SHARINGCONNECTION_ENUM_FLAGS Flags )
  513. {
  514. EnterCriticalSection(&m_csEnum);
  515. ReleaseObj( m_pEnum );
  516. m_fEnumFlags = Flags;
  517. m_pEnum = pEnum;
  518. m_pEnum->AddRef();
  519. LeaveCriticalSection(&m_csEnum);
  520. return S_OK;
  521. }
  522. HRESULT FinalRelease ()
  523. {
  524. ReleaseObj( m_pEnum );
  525. return S_OK;
  526. }
  527. virtual HRESULT GetItemInterfaceFromItemWrapped( ItemWrapped* pItem, ItemInterface** ppIface )
  528. {
  529. HRESULT hr;
  530. if ( NULL == ppIface )
  531. {
  532. hr = E_POINTER;
  533. }
  534. else if ( NULL == pItem )
  535. {
  536. hr = E_INVALIDARG;
  537. }
  538. else
  539. {
  540. *ppIface = NULL;
  541. hr = E_NOTIMPL;
  542. }
  543. return hr;
  544. }
  545. private:
  546. STDMETHOD (Next) (
  547. ULONG celt,
  548. ItemInterface** rgelt,
  549. ULONG* pceltFetched)
  550. {
  551. HRESULT hr = S_OK;
  552. ULONG ulFetched = 0;
  553. ItemInterface **ppNet = rgelt;
  554. ItemWrapped **ItemArray;
  555. // Validate parameters.
  556. if ( !rgelt || ((1 < celt) && !pceltFetched) )
  557. {
  558. hr = E_POINTER;
  559. }
  560. else if ( 0 == celt )
  561. {
  562. hr = E_INVALIDARG;
  563. }
  564. if ( SUCCEEDED(hr) )
  565. {
  566. ZeroMemory(rgelt, sizeof(ItemInterface*) * celt);
  567. if ( pceltFetched ) *pceltFetched = 0;
  568. ulFetched = 0;
  569. if ( NULL == m_pEnum )
  570. {
  571. hr = S_FALSE;
  572. }
  573. else
  574. {
  575. ItemArray = new ItemWrapped * [ sizeof(ItemWrapped) * celt ];
  576. if ( ItemArray )
  577. {
  578. ZeroMemory( ItemArray, sizeof(ItemWrapped) * celt );
  579. }
  580. else
  581. {
  582. hr = E_OUTOFMEMORY;
  583. }
  584. }
  585. }
  586. if ( SUCCEEDED(hr) && m_pEnum )
  587. {
  588. ItemWrapped **ppItem;
  589. ULONG ulBatchFetched, i;
  590. hr = m_pEnum->Next( celt, ItemArray, &ulBatchFetched );
  591. ppItem = ItemArray;
  592. ppNet = rgelt;
  593. for( i=0, ulFetched=0; ( ( i < ulBatchFetched ) && ( S_OK == hr ) ); i++, ppItem++ )
  594. {
  595. hr = GetItemInterfaceFromItemWrapped( *ppItem, ppNet );
  596. if ( SUCCEEDED(hr) )
  597. {
  598. ppNet++;
  599. ulFetched++;
  600. }
  601. ReleaseObj( *ppItem );
  602. }
  603. delete [] ItemArray;
  604. }
  605. if ( ulFetched > 0 )
  606. {
  607. hr = ( ulFetched == celt ) ? S_OK : S_FALSE;
  608. if ( pceltFetched ) *pceltFetched = ulFetched;
  609. }
  610. return hr;
  611. }
  612. public:
  613. STDMETHOD (Next) (ULONG celt, VARIANT * rgVar, ULONG * pceltFetched)
  614. { // this Next calls the private Next to wrap up ItemInterfaces in VARIANTs
  615. HNET_OEM_API_ENTER
  616. if (!rgVar || ((1 < celt) && !pceltFetched))
  617. return E_POINTER;
  618. else if (0 == celt)
  619. return E_INVALIDARG;
  620. // clear variant array
  621. for (ULONG c=0; c<celt; c++)
  622. VariantInit (&rgVar[c]);
  623. HRESULT hr = S_OK;
  624. // alloc array of ItemInterface* and call private Next
  625. ItemInterface ** rgelt = (ItemInterface**)malloc (celt*sizeof(ItemInterface*));
  626. if (!rgelt)
  627. hr = E_OUTOFMEMORY;
  628. else {
  629. ULONG ulElements = 0;
  630. hr = Next (celt, rgelt, &ulElements);
  631. if (pceltFetched)
  632. *pceltFetched = ulElements;
  633. if (SUCCEEDED(hr)) {
  634. for (ULONG ul=0; ul<ulElements; ul++) {
  635. if (S_OK == (hr = rgelt[ul]->QueryInterface (__uuidof(IDispatch), (void**)&V_DISPATCH (&rgVar[ul]))))
  636. V_VT (&rgVar[ul]) = VT_DISPATCH;
  637. else
  638. if (S_OK == (hr = rgelt[ul]->QueryInterface (__uuidof(IUnknown), (void**)&V_UNKNOWN (&rgVar[ul]))))
  639. V_VT (&rgVar[ul]) = VT_UNKNOWN;
  640. else
  641. break;
  642. }
  643. for (ULONG ul=0; ul<ulElements; ul++)
  644. rgelt[ul]->Release();
  645. }
  646. free (rgelt);
  647. }
  648. return hr;
  649. HNET_OEM_API_LEAVE
  650. }
  651. STDMETHOD (Skip) (
  652. ULONG celt)
  653. {
  654. return ( (m_pEnum) ? m_pEnum->Skip(celt) : S_OK );
  655. }
  656. STDMETHOD (Reset) ()
  657. {
  658. return ( (m_pEnum) ? m_pEnum->Reset() : S_OK );
  659. }
  660. public:
  661. STDMETHOD (Clone) (
  662. EnumInterface** ppEnum)
  663. {
  664. HNET_OEM_API_ENTER
  665. HRESULT hr = S_OK;
  666. CComObject<_ThisClass>* pNewEnum;
  667. EnumWrapped* pClonedEnum;
  668. if ( NULL == ppEnum )
  669. {
  670. hr = E_POINTER;
  671. }
  672. else
  673. {
  674. // Attempt to clone the embedded enumeration.
  675. pClonedEnum = NULL;
  676. hr = m_pEnum->Clone(&pClonedEnum);
  677. }
  678. if ( SUCCEEDED(hr) )
  679. {
  680. // Create an initialized a new instance of ourselves
  681. hr = CComObject<_ThisClass>::CreateInstance(&pNewEnum);
  682. if ( SUCCEEDED(hr) )
  683. {
  684. pNewEnum->AddRef();
  685. hr = pNewEnum->Initialize( pClonedEnum, m_fEnumFlags );
  686. if ( SUCCEEDED(hr) )
  687. {
  688. hr = pNewEnum->QueryInterface( IID_PPV_ARG(EnumInterface, ppEnum) );
  689. }
  690. pNewEnum->Release();
  691. }
  692. // Release the cloned enum. New enum object will have
  693. // AddReffed it...
  694. pClonedEnum->Release();
  695. }
  696. return hr;
  697. HNET_OEM_API_LEAVE
  698. }
  699. public:
  700. STDMETHOD (Clone) (IEnumVARIANT ** ppEnum)
  701. {
  702. HNET_OEM_API_ENTER
  703. EnumInterface* pEnum = NULL;
  704. HRESULT hr = Clone (&pEnum);
  705. if (pEnum) {
  706. hr = pEnum->QueryInterface (__uuidof(IEnumVARIANT), (void**)ppEnum);
  707. pEnum->Release();
  708. }
  709. return hr;
  710. HNET_OEM_API_LEAVE
  711. }
  712. };
  713. template<>
  714. HRESULT TNetApiEnum <IEnumNetSharingEveryConnection,
  715. INetConnection,
  716. IEnumNetConnection,
  717. INetConnection>
  718. ::GetItemInterfaceFromItemWrapped(
  719. INetConnection* pItem,
  720. INetConnection** ppIface
  721. )
  722. {
  723. HRESULT hr;
  724. if ( NULL == ppIface )
  725. {
  726. hr = E_POINTER;
  727. }
  728. else if ( NULL == pItem )
  729. {
  730. hr = E_INVALIDARG;
  731. }
  732. else
  733. {
  734. hr = pItem->QueryInterface( IID_PPV_ARG( INetConnection, ppIface ) );
  735. }
  736. return hr;
  737. }
  738. class ATL_NO_VTABLE CSharingManagerEnumEveryConnection :
  739. public TNetApiEnum <IEnumNetSharingEveryConnection,
  740. INetConnection,
  741. IEnumNetConnection,
  742. INetConnection>
  743. {
  744. public:
  745. BEGIN_COM_MAP(CSharingManagerEnumEveryConnection)
  746. COM_INTERFACE_ENTRY(IEnumNetSharingEveryConnection)
  747. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  748. END_COM_MAP()
  749. };
  750. template<>
  751. HRESULT TNetApiEnum <IEnumNetSharingPublicConnection,
  752. INetConnection,
  753. IEnumHNetIcsPublicConnections,
  754. IHNetIcsPublicConnection>
  755. ::GetItemInterfaceFromItemWrapped(
  756. IHNetIcsPublicConnection* pItem,
  757. INetConnection** ppIface
  758. )
  759. {
  760. HRESULT hr;
  761. if ( NULL == ppIface )
  762. {
  763. hr = E_POINTER;
  764. }
  765. else if ( NULL == pItem )
  766. {
  767. hr = E_INVALIDARG;
  768. }
  769. else
  770. {
  771. IHNetConnection* pHNet;
  772. *ppIface = NULL;
  773. hr = pItem->QueryInterface( IID_PPV_ARG( IHNetConnection, &pHNet ) );
  774. if ( SUCCEEDED(hr) )
  775. {
  776. hr = pHNet->GetINetConnection( ppIface );
  777. ReleaseObj( pHNet );
  778. }
  779. }
  780. return hr;
  781. }
  782. class ATL_NO_VTABLE CSharingManagerEnumPublicConnection :
  783. public TNetApiEnum <IEnumNetSharingPublicConnection,
  784. INetConnection,
  785. IEnumHNetIcsPublicConnections,
  786. IHNetIcsPublicConnection>
  787. {
  788. public:
  789. BEGIN_COM_MAP(CSharingManagerEnumPublicConnection)
  790. COM_INTERFACE_ENTRY(IEnumNetSharingPublicConnection)
  791. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  792. END_COM_MAP()
  793. };
  794. template<>
  795. HRESULT TNetApiEnum <IEnumNetSharingPrivateConnection,
  796. INetConnection,
  797. IEnumHNetIcsPrivateConnections,
  798. IHNetIcsPrivateConnection>
  799. ::GetItemInterfaceFromItemWrapped(
  800. IHNetIcsPrivateConnection* pItem,
  801. INetConnection** ppIface )
  802. {
  803. HRESULT hr;
  804. if ( NULL == ppIface )
  805. {
  806. hr = E_POINTER;
  807. }
  808. else if ( NULL == pItem )
  809. {
  810. hr = E_INVALIDARG;
  811. }
  812. else
  813. {
  814. IHNetConnection* pHNet;
  815. *ppIface = NULL;
  816. hr = pItem->QueryInterface( IID_PPV_ARG( IHNetConnection, &pHNet ) );
  817. if ( SUCCEEDED(hr) )
  818. {
  819. hr = pHNet->GetINetConnection( ppIface );
  820. ReleaseObj( pHNet );
  821. }
  822. }
  823. return hr;
  824. }
  825. class ATL_NO_VTABLE CSharingManagerEnumPrivateConnection :
  826. public TNetApiEnum <IEnumNetSharingPrivateConnection,
  827. INetConnection,
  828. IEnumHNetIcsPrivateConnections,
  829. IHNetIcsPrivateConnection>
  830. {
  831. public:
  832. BEGIN_COM_MAP(CSharingManagerEnumPrivateConnection)
  833. COM_INTERFACE_ENTRY(IEnumNetSharingPrivateConnection)
  834. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  835. END_COM_MAP()
  836. };
  837. template<>
  838. HRESULT TNetApiEnum<IEnumNetSharingPortMapping,
  839. INetSharingPortMapping,
  840. IEnumHNetPortMappingBindings,
  841. IHNetPortMappingBinding>
  842. ::GetItemInterfaceFromItemWrapped(
  843. IHNetPortMappingBinding* pItem,
  844. INetSharingPortMapping** ppIface )
  845. {
  846. HRESULT hr;
  847. hr = ( NULL == ppIface ) ? E_POINTER : S_OK;
  848. if ( SUCCEEDED(hr) )
  849. {
  850. *ppIface = NULL;
  851. if ( NULL == pItem )
  852. {
  853. hr = E_INVALIDARG;
  854. }
  855. }
  856. if ( SUCCEEDED(hr) )
  857. {
  858. CComObject<CNetSharingPortMapping>* pMap;
  859. hr = CComObject<CNetSharingPortMapping>::CreateInstance(&pMap);
  860. if ( SUCCEEDED(hr) )
  861. {
  862. pMap->AddRef();
  863. hr = pMap->Initialize( pItem );
  864. if ( SUCCEEDED(hr) )
  865. {
  866. hr = pMap->QueryInterface( IID_PPV_ARG( INetSharingPortMapping, ppIface ) );
  867. }
  868. ReleaseObj(pMap);
  869. }
  870. }
  871. return hr;
  872. }
  873. class ATL_NO_VTABLE CSharingManagerEnumPortMapping :
  874. public TNetApiEnum<IEnumNetSharingPortMapping,
  875. INetSharingPortMapping,
  876. IEnumHNetPortMappingBindings,
  877. IHNetPortMappingBinding>
  878. {
  879. public:
  880. BEGIN_COM_MAP(CSharingManagerEnumPortMapping)
  881. COM_INTERFACE_ENTRY(IEnumNetSharingPortMapping)
  882. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  883. END_COM_MAP()
  884. };
  885. // collections
  886. template <class IEnumBase, class IEnumerator>
  887. class TNetCollection :
  888. public CComObjectRootEx<CComMultiThreadModel>,
  889. public IEnumBase
  890. {
  891. private:
  892. IEnumerator * m_pE;
  893. public:
  894. typedef TNetCollection<IEnumBase, IEnumerator> _ThisClass;
  895. BEGIN_COM_MAP(_ThisClass)
  896. COM_INTERFACE_ENTRY(IEnumBase)
  897. END_COM_MAP()
  898. public:
  899. TNetCollection()
  900. {
  901. m_pE = NULL;
  902. }
  903. ~TNetCollection()
  904. {
  905. ReleaseObj(m_pE);
  906. }
  907. HRESULT Initialize (IEnumerator * pE)
  908. {
  909. _ASSERT ( pE != NULL);
  910. _ASSERT(m_pE == NULL);
  911. m_pE = pE;
  912. m_pE->AddRef();
  913. return S_OK;
  914. }
  915. STDMETHOD(get__NewEnum)(IUnknown** ppVal)
  916. {
  917. HNET_OEM_API_ENTER
  918. if (!ppVal)
  919. return E_POINTER;
  920. if (!m_pE)
  921. return E_UNEXPECTED;
  922. return m_pE->QueryInterface (__uuidof(IUnknown), (void**)ppVal);
  923. HNET_OEM_API_LEAVE
  924. }
  925. STDMETHOD(get_Count)(long *pVal)
  926. {
  927. HNET_OEM_API_ENTER
  928. if (!pVal)
  929. return E_POINTER;
  930. if (!m_pE)
  931. return E_UNEXPECTED;
  932. CComPtr<IEnumerator> spE;
  933. HRESULT hr = m_pE->Clone (&spE);
  934. if (spE) {
  935. long lCount = 0;
  936. spE->Reset();
  937. while (1) {
  938. CComVariant cvar;
  939. HRESULT hr2 = spE->Next (1, &cvar, NULL);
  940. if (hr2 == S_OK)
  941. lCount++;
  942. else
  943. break;
  944. }
  945. *pVal = lCount;
  946. }
  947. return hr;
  948. HNET_OEM_API_LEAVE
  949. }
  950. };
  951. class ATL_NO_VTABLE CNetSharingEveryConnectionCollection :
  952. public IDispatchImpl<TNetCollection<INetSharingEveryConnectionCollection, IEnumNetSharingEveryConnection>, &IID_INetSharingEveryConnectionCollection, &LIBID_NETCONLib>
  953. {
  954. public:
  955. BEGIN_COM_MAP(CNetSharingEveryConnectionCollection)
  956. COM_INTERFACE_ENTRY(IDispatch)
  957. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  958. END_COM_MAP()
  959. };
  960. class ATL_NO_VTABLE CNetSharingPublicConnectionCollection :
  961. public IDispatchImpl<TNetCollection<INetSharingPublicConnectionCollection, IEnumNetSharingPublicConnection>, &IID_INetSharingPublicConnectionCollection, &LIBID_NETCONLib>
  962. {
  963. public:
  964. BEGIN_COM_MAP(CNetSharingPublicConnectionCollection)
  965. COM_INTERFACE_ENTRY(IDispatch)
  966. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  967. END_COM_MAP()
  968. };
  969. class ATL_NO_VTABLE CNetSharingPrivateConnectionCollection :
  970. public IDispatchImpl<TNetCollection<INetSharingPrivateConnectionCollection,IEnumNetSharingPrivateConnection>, &IID_INetSharingPrivateConnectionCollection, &LIBID_NETCONLib>
  971. {
  972. public:
  973. BEGIN_COM_MAP(CNetSharingPrivateConnectionCollection)
  974. COM_INTERFACE_ENTRY(IDispatch)
  975. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  976. END_COM_MAP()
  977. };
  978. class ATL_NO_VTABLE CNetSharingPortMappingCollection :
  979. public IDispatchImpl<TNetCollection<INetSharingPortMappingCollection, IEnumNetSharingPortMapping>, &IID_INetSharingPortMappingCollection, &LIBID_NETCONLib>
  980. {
  981. public:
  982. BEGIN_COM_MAP(CNetSharingPortMappingCollection)
  983. COM_INTERFACE_ENTRY(IDispatch)
  984. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  985. END_COM_MAP()
  986. };
  987. // props
  988. class ATL_NO_VTABLE CNetSharingPortMappingProps :
  989. public CComObjectRootEx<CComMultiThreadModel>,
  990. public IDispatchImpl<INetSharingPortMappingProps, &IID_INetSharingPortMappingProps, &LIBID_NETCONLib>
  991. {
  992. private:
  993. ICS_PORTMAPPING m_IPM; // not alloc'd
  994. public:
  995. BEGIN_COM_MAP(CNetSharingPortMappingProps)
  996. COM_INTERFACE_ENTRY(INetSharingPortMappingProps)
  997. COM_INTERFACE_ENTRY(IDispatch)
  998. END_COM_MAP()
  999. public:
  1000. CNetSharingPortMappingProps()
  1001. {
  1002. ZeroMemory (&m_IPM, sizeof(ICS_PORTMAPPING));
  1003. }
  1004. ~CNetSharingPortMappingProps()
  1005. {
  1006. FreeData (&m_IPM);
  1007. }
  1008. public: // CNetSharingPortMappingProps
  1009. ICS_PORTMAPPING * GetVolatileRawData (void)
  1010. {
  1011. return &m_IPM;
  1012. }
  1013. HRESULT SetRawData (ICS_PORTMAPPING * pIPM)
  1014. {
  1015. _ASSERT (pIPM);
  1016. if (!pIPM)
  1017. return E_POINTER;
  1018. ICS_PORTMAPPING IPM = {0};
  1019. HRESULT hr = DupData (pIPM, &IPM);
  1020. if (hr == S_OK) {
  1021. FreeData (&m_IPM);
  1022. m_IPM = IPM; // struct copy
  1023. }
  1024. return S_OK;
  1025. }
  1026. static OLECHAR * DupString (OLECHAR * in)
  1027. {
  1028. OLECHAR * po = NULL;
  1029. if (in) {
  1030. po = (OLECHAR*)malloc ((wcslen (in) + 1)*sizeof(OLECHAR));
  1031. if (po)
  1032. wcscpy(po, in);
  1033. } else {
  1034. // one of pszwTargetName or pszwTargetIPAddress may be blank! so...
  1035. po = (OLECHAR*)malloc (1*sizeof(OLECHAR));
  1036. if (po)
  1037. *po = 0; // ...alloc an emptry string
  1038. }
  1039. return po;
  1040. }
  1041. static HRESULT DupData (ICS_PORTMAPPING * in, ICS_PORTMAPPING * out)
  1042. {
  1043. if (!in) return E_POINTER;
  1044. out->ucIPProtocol = in->ucIPProtocol;
  1045. out->usExternalPort = in->usExternalPort;
  1046. out->usInternalPort = in->usInternalPort;
  1047. out->dwOptions = in->dwOptions;
  1048. out->bEnabled = in->bEnabled;
  1049. out->pszwName = DupString (in->pszwName);
  1050. out->pszwTargetName = DupString (in->pszwTargetName);
  1051. out->pszwTargetIPAddress = DupString (in->pszwTargetIPAddress);
  1052. if (!out->pszwName || !out->pszwTargetName || !out->pszwTargetIPAddress) {
  1053. FreeData (out);
  1054. return E_OUTOFMEMORY;
  1055. }
  1056. return S_OK;
  1057. }
  1058. static void FreeData (ICS_PORTMAPPING * pIPM)
  1059. {
  1060. if (pIPM) {
  1061. if (pIPM->pszwName) free (pIPM->pszwName);
  1062. if (pIPM->pszwTargetName) free (pIPM->pszwTargetName);
  1063. if (pIPM->pszwTargetIPAddress) free (pIPM->pszwTargetIPAddress);
  1064. }
  1065. }
  1066. public: // INetSharingPortMappingProps
  1067. STDMETHODIMP get_Name (/*[out, retval]*/ BSTR * pbstrName);
  1068. STDMETHODIMP get_IPProtocol (/*[out, retval]*/ UCHAR * pucIPProt);
  1069. STDMETHODIMP get_ExternalPort (/*[out, retval]*/ long * pusPort);
  1070. STDMETHODIMP get_InternalPort (/*[out, retval]*/ long * pusPort);
  1071. STDMETHODIMP get_Options (/*[out, retval]*/ long * pdwOptions);
  1072. STDMETHODIMP get_TargetName (/*[out, retval]*/ BSTR * pbstrTargetName);
  1073. STDMETHODIMP get_TargetIPAddress(/*[out, retval]*/ BSTR * pbstrTargetIPAddress);
  1074. STDMETHODIMP get_Enabled (/*[out, retval]*/ VARIANT_BOOL * pbool);
  1075. };