Source code of Windows XP (NT5)
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.

1351 lines
31 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. HRESULT hr = S_OK;
  621. // alloc array of ItemInterface* and call private Next
  622. ItemInterface ** rgelt = (ItemInterface**)malloc (celt*sizeof(ItemInterface*));
  623. if (!rgelt)
  624. hr = E_OUTOFMEMORY;
  625. else {
  626. hr = Next (celt, rgelt, pceltFetched);
  627. if (hr == S_OK) { // if error or S_FALSE, don't copy data
  628. ULONG ulElements;
  629. if (pceltFetched)
  630. ulElements = *pceltFetched;
  631. else
  632. ulElements = 1;
  633. for (ULONG ul=0; ul<ulElements; ul++) {
  634. if (S_OK == (hr = rgelt[ul]->QueryInterface (__uuidof(IDispatch), (void**)&V_DISPATCH (&rgVar[ul]))))
  635. V_VT (&rgVar[ul]) = VT_DISPATCH;
  636. else
  637. if (S_OK == (hr = rgelt[ul]->QueryInterface (__uuidof(IUnknown), (void**)&V_UNKNOWN (&rgVar[ul]))))
  638. V_VT (&rgVar[ul]) = VT_UNKNOWN;
  639. else
  640. break;
  641. }
  642. for (ULONG ul=0; ul<ulElements; ul++)
  643. rgelt[ul]->Release();
  644. }
  645. free (rgelt);
  646. }
  647. return hr;
  648. HNET_OEM_API_LEAVE
  649. }
  650. STDMETHOD (Skip) (
  651. ULONG celt)
  652. {
  653. return ( (m_pEnum) ? m_pEnum->Skip(celt) : S_OK );
  654. }
  655. STDMETHOD (Reset) ()
  656. {
  657. return ( (m_pEnum) ? m_pEnum->Reset() : S_OK );
  658. }
  659. public:
  660. STDMETHOD (Clone) (
  661. EnumInterface** ppEnum)
  662. {
  663. HNET_OEM_API_ENTER
  664. HRESULT hr = S_OK;
  665. CComObject<_ThisClass>* pNewEnum;
  666. EnumWrapped* pClonedEnum;
  667. if ( NULL == ppEnum )
  668. {
  669. hr = E_POINTER;
  670. }
  671. else
  672. {
  673. // Attempt to clone the embedded enumeration.
  674. pClonedEnum = NULL;
  675. hr = m_pEnum->Clone(&pClonedEnum);
  676. }
  677. if ( SUCCEEDED(hr) )
  678. {
  679. // Create an initialized a new instance of ourselves
  680. hr = CComObject<_ThisClass>::CreateInstance(&pNewEnum);
  681. if ( SUCCEEDED(hr) )
  682. {
  683. pNewEnum->AddRef();
  684. hr = pNewEnum->Initialize( pClonedEnum, m_fEnumFlags );
  685. if ( SUCCEEDED(hr) )
  686. {
  687. hr = pNewEnum->QueryInterface( IID_PPV_ARG(EnumInterface, ppEnum) );
  688. }
  689. pNewEnum->Release();
  690. }
  691. // Release the cloned enum. New enum object will have
  692. // AddReffed it...
  693. pClonedEnum->Release();
  694. }
  695. return hr;
  696. HNET_OEM_API_LEAVE
  697. }
  698. public:
  699. STDMETHOD (Clone) (IEnumVARIANT ** ppEnum)
  700. {
  701. HNET_OEM_API_ENTER
  702. EnumInterface* pEnum = NULL;
  703. HRESULT hr = Clone (&pEnum);
  704. if (pEnum) {
  705. hr = pEnum->QueryInterface (__uuidof(IEnumVARIANT), (void**)ppEnum);
  706. pEnum->Release();
  707. }
  708. return hr;
  709. HNET_OEM_API_LEAVE
  710. }
  711. };
  712. template<>
  713. HRESULT TNetApiEnum <IEnumNetSharingEveryConnection,
  714. INetConnection,
  715. IEnumNetConnection,
  716. INetConnection>
  717. ::GetItemInterfaceFromItemWrapped(
  718. INetConnection* pItem,
  719. INetConnection** ppIface
  720. )
  721. {
  722. HRESULT hr;
  723. if ( NULL == ppIface )
  724. {
  725. hr = E_POINTER;
  726. }
  727. else if ( NULL == pItem )
  728. {
  729. hr = E_INVALIDARG;
  730. }
  731. else
  732. {
  733. hr = pItem->QueryInterface( IID_PPV_ARG( INetConnection, ppIface ) );
  734. }
  735. return hr;
  736. }
  737. class ATL_NO_VTABLE CSharingManagerEnumEveryConnection :
  738. public TNetApiEnum <IEnumNetSharingEveryConnection,
  739. INetConnection,
  740. IEnumNetConnection,
  741. INetConnection>
  742. {
  743. public:
  744. BEGIN_COM_MAP(CSharingManagerEnumEveryConnection)
  745. COM_INTERFACE_ENTRY(IEnumNetSharingEveryConnection)
  746. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  747. END_COM_MAP()
  748. };
  749. template<>
  750. HRESULT TNetApiEnum <IEnumNetSharingPublicConnection,
  751. INetConnection,
  752. IEnumHNetIcsPublicConnections,
  753. IHNetIcsPublicConnection>
  754. ::GetItemInterfaceFromItemWrapped(
  755. IHNetIcsPublicConnection* pItem,
  756. INetConnection** ppIface
  757. )
  758. {
  759. HRESULT hr;
  760. if ( NULL == ppIface )
  761. {
  762. hr = E_POINTER;
  763. }
  764. else if ( NULL == pItem )
  765. {
  766. hr = E_INVALIDARG;
  767. }
  768. else
  769. {
  770. IHNetConnection* pHNet;
  771. *ppIface = NULL;
  772. hr = pItem->QueryInterface( IID_PPV_ARG( IHNetConnection, &pHNet ) );
  773. if ( SUCCEEDED(hr) )
  774. {
  775. hr = pHNet->GetINetConnection( ppIface );
  776. ReleaseObj( pHNet );
  777. }
  778. }
  779. return hr;
  780. }
  781. class ATL_NO_VTABLE CSharingManagerEnumPublicConnection :
  782. public TNetApiEnum <IEnumNetSharingPublicConnection,
  783. INetConnection,
  784. IEnumHNetIcsPublicConnections,
  785. IHNetIcsPublicConnection>
  786. {
  787. public:
  788. BEGIN_COM_MAP(CSharingManagerEnumPublicConnection)
  789. COM_INTERFACE_ENTRY(IEnumNetSharingPublicConnection)
  790. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  791. END_COM_MAP()
  792. };
  793. template<>
  794. HRESULT TNetApiEnum <IEnumNetSharingPrivateConnection,
  795. INetConnection,
  796. IEnumHNetIcsPrivateConnections,
  797. IHNetIcsPrivateConnection>
  798. ::GetItemInterfaceFromItemWrapped(
  799. IHNetIcsPrivateConnection* pItem,
  800. INetConnection** ppIface )
  801. {
  802. HRESULT hr;
  803. if ( NULL == ppIface )
  804. {
  805. hr = E_POINTER;
  806. }
  807. else if ( NULL == pItem )
  808. {
  809. hr = E_INVALIDARG;
  810. }
  811. else
  812. {
  813. IHNetConnection* pHNet;
  814. *ppIface = NULL;
  815. hr = pItem->QueryInterface( IID_PPV_ARG( IHNetConnection, &pHNet ) );
  816. if ( SUCCEEDED(hr) )
  817. {
  818. hr = pHNet->GetINetConnection( ppIface );
  819. ReleaseObj( pHNet );
  820. }
  821. }
  822. return hr;
  823. }
  824. class ATL_NO_VTABLE CSharingManagerEnumPrivateConnection :
  825. public TNetApiEnum <IEnumNetSharingPrivateConnection,
  826. INetConnection,
  827. IEnumHNetIcsPrivateConnections,
  828. IHNetIcsPrivateConnection>
  829. {
  830. public:
  831. BEGIN_COM_MAP(CSharingManagerEnumPrivateConnection)
  832. COM_INTERFACE_ENTRY(IEnumNetSharingPrivateConnection)
  833. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  834. END_COM_MAP()
  835. };
  836. template<>
  837. HRESULT TNetApiEnum<IEnumNetSharingPortMapping,
  838. INetSharingPortMapping,
  839. IEnumHNetPortMappingBindings,
  840. IHNetPortMappingBinding>
  841. ::GetItemInterfaceFromItemWrapped(
  842. IHNetPortMappingBinding* pItem,
  843. INetSharingPortMapping** ppIface )
  844. {
  845. HRESULT hr;
  846. hr = ( NULL == ppIface ) ? E_POINTER : S_OK;
  847. if ( SUCCEEDED(hr) )
  848. {
  849. *ppIface = NULL;
  850. if ( NULL == pItem )
  851. {
  852. hr = E_INVALIDARG;
  853. }
  854. }
  855. if ( SUCCEEDED(hr) )
  856. {
  857. CComObject<CNetSharingPortMapping>* pMap;
  858. hr = CComObject<CNetSharingPortMapping>::CreateInstance(&pMap);
  859. if ( SUCCEEDED(hr) )
  860. {
  861. pMap->AddRef();
  862. hr = pMap->Initialize( pItem );
  863. if ( SUCCEEDED(hr) )
  864. {
  865. hr = pMap->QueryInterface( IID_PPV_ARG( INetSharingPortMapping, ppIface ) );
  866. }
  867. ReleaseObj(pMap);
  868. }
  869. }
  870. return hr;
  871. }
  872. class ATL_NO_VTABLE CSharingManagerEnumPortMapping :
  873. public TNetApiEnum<IEnumNetSharingPortMapping,
  874. INetSharingPortMapping,
  875. IEnumHNetPortMappingBindings,
  876. IHNetPortMappingBinding>
  877. {
  878. public:
  879. BEGIN_COM_MAP(CSharingManagerEnumPortMapping)
  880. COM_INTERFACE_ENTRY(IEnumNetSharingPortMapping)
  881. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  882. END_COM_MAP()
  883. };
  884. // collections
  885. template <class IEnumBase, class IEnumerator>
  886. class TNetCollection :
  887. public CComObjectRootEx<CComMultiThreadModel>,
  888. public IEnumBase
  889. {
  890. private:
  891. IEnumerator * m_pE;
  892. public:
  893. typedef TNetCollection<IEnumBase, IEnumerator> _ThisClass;
  894. BEGIN_COM_MAP(_ThisClass)
  895. COM_INTERFACE_ENTRY(IEnumBase)
  896. END_COM_MAP()
  897. public:
  898. TNetCollection()
  899. {
  900. m_pE = NULL;
  901. }
  902. ~TNetCollection()
  903. {
  904. ReleaseObj(m_pE);
  905. }
  906. HRESULT Initialize (IEnumerator * pE)
  907. {
  908. _ASSERT ( pE != NULL);
  909. _ASSERT(m_pE == NULL);
  910. m_pE = pE;
  911. m_pE->AddRef();
  912. return S_OK;
  913. }
  914. STDMETHOD(get__NewEnum)(IUnknown** ppVal)
  915. {
  916. HNET_OEM_API_ENTER
  917. if (!ppVal)
  918. return E_POINTER;
  919. if (!m_pE)
  920. return E_UNEXPECTED;
  921. return m_pE->QueryInterface (__uuidof(IUnknown), (void**)ppVal);
  922. HNET_OEM_API_LEAVE
  923. }
  924. STDMETHOD(get_Count)(long *pVal)
  925. {
  926. HNET_OEM_API_ENTER
  927. if (!pVal)
  928. return E_POINTER;
  929. if (!m_pE)
  930. return E_UNEXPECTED;
  931. CComPtr<IEnumerator> spE;
  932. HRESULT hr = m_pE->Clone (&spE);
  933. if (spE) {
  934. long lCount = 0;
  935. spE->Reset();
  936. while (1) {
  937. CComVariant cvar;
  938. HRESULT hr2 = spE->Next (1, &cvar, NULL);
  939. if (hr2 == S_OK)
  940. lCount++;
  941. else
  942. break;
  943. }
  944. *pVal = lCount;
  945. }
  946. return hr;
  947. HNET_OEM_API_LEAVE
  948. }
  949. };
  950. class ATL_NO_VTABLE CNetSharingEveryConnectionCollection :
  951. public IDispatchImpl<TNetCollection<INetSharingEveryConnectionCollection, IEnumNetSharingEveryConnection>, &IID_INetSharingEveryConnectionCollection, &LIBID_NETCONLib>
  952. {
  953. public:
  954. BEGIN_COM_MAP(CNetSharingEveryConnectionCollection)
  955. COM_INTERFACE_ENTRY(IDispatch)
  956. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  957. END_COM_MAP()
  958. };
  959. class ATL_NO_VTABLE CNetSharingPublicConnectionCollection :
  960. public IDispatchImpl<TNetCollection<INetSharingPublicConnectionCollection, IEnumNetSharingPublicConnection>, &IID_INetSharingPublicConnectionCollection, &LIBID_NETCONLib>
  961. {
  962. public:
  963. BEGIN_COM_MAP(CNetSharingPublicConnectionCollection)
  964. COM_INTERFACE_ENTRY(IDispatch)
  965. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  966. END_COM_MAP()
  967. };
  968. class ATL_NO_VTABLE CNetSharingPrivateConnectionCollection :
  969. public IDispatchImpl<TNetCollection<INetSharingPrivateConnectionCollection,IEnumNetSharingPrivateConnection>, &IID_INetSharingPrivateConnectionCollection, &LIBID_NETCONLib>
  970. {
  971. public:
  972. BEGIN_COM_MAP(CNetSharingPrivateConnectionCollection)
  973. COM_INTERFACE_ENTRY(IDispatch)
  974. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  975. END_COM_MAP()
  976. };
  977. class ATL_NO_VTABLE CNetSharingPortMappingCollection :
  978. public IDispatchImpl<TNetCollection<INetSharingPortMappingCollection, IEnumNetSharingPortMapping>, &IID_INetSharingPortMappingCollection, &LIBID_NETCONLib>
  979. {
  980. public:
  981. BEGIN_COM_MAP(CNetSharingPortMappingCollection)
  982. COM_INTERFACE_ENTRY(IDispatch)
  983. COM_INTERFACE_ENTRY_CHAIN(_ThisClass)
  984. END_COM_MAP()
  985. };
  986. // props
  987. class ATL_NO_VTABLE CNetSharingPortMappingProps :
  988. public CComObjectRootEx<CComMultiThreadModel>,
  989. public IDispatchImpl<INetSharingPortMappingProps, &IID_INetSharingPortMappingProps, &LIBID_NETCONLib>
  990. {
  991. private:
  992. ICS_PORTMAPPING m_IPM; // not alloc'd
  993. public:
  994. BEGIN_COM_MAP(CNetSharingPortMappingProps)
  995. COM_INTERFACE_ENTRY(INetSharingPortMappingProps)
  996. COM_INTERFACE_ENTRY(IDispatch)
  997. END_COM_MAP()
  998. public:
  999. CNetSharingPortMappingProps()
  1000. {
  1001. ZeroMemory (&m_IPM, sizeof(ICS_PORTMAPPING));
  1002. }
  1003. ~CNetSharingPortMappingProps()
  1004. {
  1005. FreeData (&m_IPM);
  1006. }
  1007. public: // CNetSharingPortMappingProps
  1008. ICS_PORTMAPPING * GetVolatileRawData (void)
  1009. {
  1010. return &m_IPM;
  1011. }
  1012. HRESULT SetRawData (ICS_PORTMAPPING * pIPM)
  1013. {
  1014. _ASSERT (pIPM);
  1015. if (!pIPM)
  1016. return E_POINTER;
  1017. ICS_PORTMAPPING IPM = {0};
  1018. HRESULT hr = DupData (pIPM, &IPM);
  1019. if (hr == S_OK) {
  1020. FreeData (&m_IPM);
  1021. m_IPM = IPM; // struct copy
  1022. }
  1023. return S_OK;
  1024. }
  1025. static OLECHAR * DupString (OLECHAR * in)
  1026. {
  1027. OLECHAR * po = NULL;
  1028. if (in) {
  1029. po = (OLECHAR*)malloc ((wcslen (in) + 1)*sizeof(OLECHAR));
  1030. if (po)
  1031. wcscpy(po, in);
  1032. } else {
  1033. // one of pszwTargetName or pszwTargetIPAddress may be blank! so...
  1034. po = (OLECHAR*)malloc (1*sizeof(OLECHAR));
  1035. if (po)
  1036. *po = 0; // ...alloc an emptry string
  1037. }
  1038. return po;
  1039. }
  1040. static HRESULT DupData (ICS_PORTMAPPING * in, ICS_PORTMAPPING * out)
  1041. {
  1042. if (!in) return E_POINTER;
  1043. out->ucIPProtocol = in->ucIPProtocol;
  1044. out->usExternalPort = in->usExternalPort;
  1045. out->usInternalPort = in->usInternalPort;
  1046. out->dwOptions = in->dwOptions;
  1047. out->bEnabled = in->bEnabled;
  1048. out->pszwName = DupString (in->pszwName);
  1049. out->pszwTargetName = DupString (in->pszwTargetName);
  1050. out->pszwTargetIPAddress = DupString (in->pszwTargetIPAddress);
  1051. if (!out->pszwName || !out->pszwTargetName || !out->pszwTargetIPAddress) {
  1052. FreeData (out);
  1053. return E_OUTOFMEMORY;
  1054. }
  1055. return S_OK;
  1056. }
  1057. static void FreeData (ICS_PORTMAPPING * pIPM)
  1058. {
  1059. if (pIPM) {
  1060. if (pIPM->pszwName) free (pIPM->pszwName);
  1061. if (pIPM->pszwTargetName) free (pIPM->pszwTargetName);
  1062. if (pIPM->pszwTargetIPAddress) free (pIPM->pszwTargetIPAddress);
  1063. }
  1064. }
  1065. public: // INetSharingPortMappingProps
  1066. STDMETHODIMP get_Name (/*[out, retval]*/ BSTR * pbstrName);
  1067. STDMETHODIMP get_IPProtocol (/*[out, retval]*/ UCHAR * pucIPProt);
  1068. STDMETHODIMP get_ExternalPort (/*[out, retval]*/ long * pusPort);
  1069. STDMETHODIMP get_InternalPort (/*[out, retval]*/ long * pusPort);
  1070. STDMETHODIMP get_Options (/*[out, retval]*/ long * pdwOptions);
  1071. STDMETHODIMP get_TargetName (/*[out, retval]*/ BSTR * pbstrTargetName);
  1072. STDMETHODIMP get_TargetIPAddress(/*[out, retval]*/ BSTR * pbstrTargetIPAddress);
  1073. STDMETHODIMP get_Enabled (/*[out, retval]*/ VARIANT_BOOL * pbool);
  1074. };