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.

5924 lines
157 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: MTNode.cpp
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 9/17/1996 RaviR Created
  15. //____________________________________________________________________________
  16. //
  17. #include "stdafx.h"
  18. #include "nodemgr.h"
  19. #include "comdbg.h"
  20. #include "regutil.h"
  21. #include "bitmap.h"
  22. #include "dummysi.h"
  23. #include "tasks.h"
  24. #include "policy.h"
  25. #include "bookmark.h"
  26. #include "nodepath.h"
  27. #include "siprop.h"
  28. #include "util.h"
  29. #include "addsnpin.h"
  30. #include "about.h"
  31. #include "nodemgrdebug.h"
  32. extern const CLSID CLSID_FolderSnapin;
  33. extern const CLSID CLSID_OCXSnapin;
  34. extern const CLSID CLSID_HTMLSnapin;
  35. #ifdef _DEBUG
  36. #undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39. // {118B559C-6D8C-11d0-B503-00C04FD9080A}
  40. const GUID IID_PersistData =
  41. { 0x118b559c, 0x6d8c, 0x11d0, { 0xb5, 0x3, 0x0, 0xc0, 0x4f, 0xd9, 0x8, 0xa } };
  42. //############################################################################
  43. //############################################################################
  44. //
  45. // Implementation of class CStorage
  46. //
  47. //############################################################################
  48. //############################################################################
  49. /*+-------------------------------------------------------------------------*
  50. * class CStorage
  51. *
  52. *
  53. * PURPOSE: Wrapper for IStorage. Provides several utility functions.
  54. *
  55. *+-------------------------------------------------------------------------*/
  56. class CStorage
  57. {
  58. IStoragePtr m_spStorage;
  59. public:
  60. CStorage() {}
  61. CStorage(IStorage *pStorage)
  62. {
  63. m_spStorage = pStorage;
  64. }
  65. CStorage & operator = (const CStorage &rhs)
  66. {
  67. m_spStorage = rhs.m_spStorage;
  68. return *this;
  69. }
  70. void Attach(IStorage *pStorage)
  71. {
  72. m_spStorage = pStorage;
  73. }
  74. IStorage *Get()
  75. {
  76. return m_spStorage;
  77. }
  78. // create this storage below the specified storage
  79. SC ScCreate(CStorage &storageParent, const wchar_t* name, DWORD grfMode, const wchar_t* instanceName)
  80. {
  81. SC sc;
  82. sc = CreateDebugStorage(storageParent.Get(), name, grfMode, instanceName, &m_spStorage);
  83. return sc;
  84. }
  85. SC ScMoveElementTo(const wchar_t *name, CStorage &storageDest, const wchar_t *newName, DWORD grfFlags)
  86. {
  87. SC sc;
  88. if(!Get() || ! storageDest.Get())
  89. goto PointerError;
  90. sc = m_spStorage->MoveElementTo(name, storageDest.Get(), newName, grfFlags);
  91. // error STG_E_FILENOTFOUND must be treated differently, since it is expected
  92. // to occur and means the end of move operation (loop) in ScConvertLegacyNode.
  93. // Do not trace in this case.
  94. if(sc == SC(STG_E_FILENOTFOUND))
  95. goto Cleanup;
  96. if(sc)
  97. goto Error;
  98. Cleanup:
  99. return sc;
  100. PointerError:
  101. sc = E_POINTER;
  102. Error:
  103. TraceError(TEXT("CStorage::ScMoveElementTo"), sc);
  104. goto Cleanup;
  105. }
  106. };
  107. //############################################################################
  108. //############################################################################
  109. //
  110. // Implementation of class CStream
  111. //
  112. //############################################################################
  113. //############################################################################
  114. /*+-------------------------------------------------------------------------*
  115. * class CStream
  116. *
  117. *
  118. * PURPOSE: Wrapper for IStream. Provides several utility functions.
  119. *
  120. *+-------------------------------------------------------------------------*/
  121. class CStream
  122. {
  123. IStreamPtr m_spStream;
  124. typedef IStream *PSTREAM;
  125. public:
  126. CStream() {}
  127. CStream(IStream *pStream)
  128. {
  129. m_spStream = pStream;
  130. }
  131. CStream & operator = (const CStream &rhs)
  132. {
  133. m_spStream = rhs.m_spStream;
  134. return *this;
  135. }
  136. void Attach(IStream *pStream)
  137. {
  138. m_spStream = pStream;
  139. }
  140. IStream *Get()
  141. {
  142. return m_spStream;
  143. }
  144. operator IStream&()
  145. {
  146. return *m_spStream;
  147. }
  148. // create this stream below the specified storage
  149. SC ScCreate(CStorage& storageParent, const wchar_t* name, DWORD grfMode, const wchar_t* instanceName)
  150. {
  151. SC sc;
  152. sc = CreateDebugStream(storageParent.Get(), name, grfMode, instanceName, &m_spStream);
  153. return sc;
  154. }
  155. /*+-------------------------------------------------------------------------*
  156. *
  157. * ScRead
  158. *
  159. * PURPOSE: Reads the specified object from the stream.
  160. *
  161. * PARAMETERS:
  162. * void * pv : The location of the object.
  163. * size_t size : The size of the object.
  164. *
  165. * RETURNS:
  166. * SC
  167. *
  168. *+-------------------------------------------------------------------------*/
  169. SC ScRead(void *pv, size_t size, bool bIgnoreErrors = false)
  170. {
  171. DECLARE_SC(sc, TEXT("CStream::ScRead"));
  172. // parameter check
  173. sc = ScCheckPointers(pv);
  174. if (sc)
  175. return sc;
  176. // internal pointer check
  177. sc = ScCheckPointers(m_spStream, E_POINTER);
  178. if (sc)
  179. return sc;
  180. // read the data
  181. ULONG bytesRead = 0;
  182. sc = m_spStream->Read(pv, size, &bytesRead);
  183. // if we need to ignore errors, just return.
  184. if(bIgnoreErrors)
  185. return sc.Clear(), sc;
  186. if (sc)
  187. return sc;
  188. // since this function does not return the number of bytes read,
  189. // failure to read as may as requested should be treated as error
  190. if (sc == SC(S_FALSE) || bytesRead != size)
  191. return sc = E_FAIL;
  192. return sc;
  193. }
  194. /*+-------------------------------------------------------------------------*
  195. *
  196. * ScWrite
  197. *
  198. * PURPOSE: Writes the specified object to the stream
  199. *
  200. * PARAMETERS:
  201. * const void :
  202. * size_t size :
  203. *
  204. * RETURNS:
  205. * SC
  206. *
  207. *+-------------------------------------------------------------------------*/
  208. SC ScWrite(const void *pv, size_t size)
  209. {
  210. DECLARE_SC(sc, TEXT("CStream::ScWrite"));
  211. // parameter check
  212. sc = ScCheckPointers(pv);
  213. if (sc)
  214. return sc;
  215. // internal pointer check
  216. sc = ScCheckPointers(m_spStream, E_POINTER);
  217. if (sc)
  218. return sc;
  219. // write the data
  220. ULONG bytesWritten = 0;
  221. sc = m_spStream->Write(pv, size, &bytesWritten);
  222. if (sc)
  223. return sc;
  224. // since this function does not return the number of bytes written,
  225. // failure to write as may as requested should be treated as error
  226. if (bytesWritten != size)
  227. return sc = E_FAIL;
  228. return sc;
  229. }
  230. };
  231. /////////////////////////////////////////////////////////////////////////////
  232. // Forward declaration of helper functions defined below
  233. SC ScLoadBitmap (CStream &stream, HBITMAP* pBitmap);
  234. void PersistBitmap (CPersistor &persistor, LPCTSTR name, HBITMAP& hBitmap);
  235. static inline SC ScWriteEmptyNode(CStream &stream)
  236. {
  237. SC sc;
  238. int nt = 0;
  239. sc = stream.ScWrite(&nt, sizeof(nt));
  240. if(sc)
  241. goto Error;
  242. Cleanup:
  243. return sc;
  244. Error:
  245. TraceError(TEXT("ScWriteEmptyNode"), sc);
  246. goto Cleanup;
  247. }
  248. static inline CLIPFORMAT GetPreLoadFormat (void)
  249. {
  250. static CLIPFORMAT s_cfPreLoads = 0;
  251. if (s_cfPreLoads == 0) {
  252. USES_CONVERSION;
  253. s_cfPreLoads = (CLIPFORMAT) RegisterClipboardFormat (W2T(CCF_SNAPIN_PRELOADS));
  254. }
  255. return s_cfPreLoads;
  256. }
  257. //############################################################################
  258. //############################################################################
  259. //
  260. // Implementation of class CMTNode
  261. //
  262. //############################################################################
  263. //############################################################################
  264. DEBUG_DECLARE_INSTANCE_COUNTER(CMTNode);
  265. // Static member
  266. MTNODEID CMTNode::m_NextID = ROOTNODEID;
  267. CMTNode::CMTNode()
  268. : m_ID(GetNextID()), m_pNext(NULL), m_pChild(NULL), m_pParent(NULL),
  269. m_bIsDirty(true), m_cRef(1), m_usFlags(0), m_bLoaded(false),
  270. m_bookmark(NULL)
  271. {
  272. DEBUG_INCREMENT_INSTANCE_COUNTER(CMTNode);
  273. Reset();
  274. m_nImage = eStockImage_Folder;
  275. m_nOpenImage = eStockImage_OpenFolder;
  276. m_nState = 0;
  277. }
  278. void CMTNode::Reset()
  279. {
  280. m_idOwner = TVOWNED_MAGICWORD;
  281. m_lUserParam = 0;
  282. m_pPrimaryComponentData = NULL;
  283. m_bInit = false;
  284. m_bExtensionsExpanded = false;
  285. m_usExpandFlags = 0;
  286. ResetExpandedAtLeastOnce();
  287. }
  288. CMTNode::~CMTNode()
  289. {
  290. DEBUG_DECREMENT_INSTANCE_COUNTER(CMTNode);
  291. DECLARE_SC(sc, TEXT("CMTNode::~CMTNode"));
  292. if (IsPropertyPageDisplayed() == TRUE)
  293. MMCIsMTNodeValid(this, TRUE);
  294. ASSERT(m_pNext == NULL);
  295. ASSERT(m_pParent == NULL);
  296. ASSERT(m_cRef == 0);
  297. CScopeTree *pScopeTree = CScopeTree::GetScopeTree();
  298. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  299. if (!sc)
  300. {
  301. sc = pScopeTree->ScUnadviseMTNode(this);
  302. }
  303. if (m_pChild != NULL)
  304. {
  305. // Don't recurse the siblings of the child.
  306. CMTNode* pMTNodeCurr = m_pChild;
  307. while (pMTNodeCurr)
  308. {
  309. m_pChild = pMTNodeCurr->Next();
  310. pMTNodeCurr->AttachNext(NULL);
  311. pMTNodeCurr->AttachParent(NULL);
  312. pMTNodeCurr->Release();
  313. pMTNodeCurr = m_pChild;
  314. }
  315. m_pChild = NULL;
  316. }
  317. // DON'T CHANGE THE ORDER OF THESE NULL ASSIGNMENTS!!!!!!!!!
  318. m_spTreeStream = NULL;
  319. m_spViewStorage = NULL;
  320. m_spCDStorage = NULL;
  321. m_spNodeStorage = NULL;
  322. m_spPersistData = NULL;
  323. if (m_pParent != NULL)
  324. {
  325. if (m_pParent->m_pChild == this)
  326. {
  327. m_pParent->m_pChild = NULL;
  328. if (GetStaticParent() == this)
  329. m_pParent->SetDirty();
  330. }
  331. }
  332. }
  333. // Was MMCN_REMOVE_CHILDREN sent to the snapin owning this node or its parent
  334. bool CMTNode::AreChildrenBeingRemoved ()
  335. {
  336. if (_IsFlagSet(FLAG_REMOVING_CHILDREN))
  337. return true;
  338. if (Parent())
  339. return Parent()->AreChildrenBeingRemoved ();
  340. return false;
  341. }
  342. CMTNode* CMTNode::FromScopeItem (HSCOPEITEM item)
  343. {
  344. CMTNode* pMTNode = reinterpret_cast<CMTNode*>(item);
  345. try
  346. {
  347. pMTNode = dynamic_cast<CMTNode*>(pMTNode);
  348. }
  349. catch (...)
  350. {
  351. pMTNode = NULL;
  352. }
  353. return (pMTNode);
  354. }
  355. /*+-------------------------------------------------------------------------*
  356. * class CMMCSnapIn
  357. *
  358. *
  359. * PURPOSE: The COM 0bject that exposes the SnapIn interface.
  360. *
  361. *+-------------------------------------------------------------------------*/
  362. class CMMCSnapIn :
  363. public CMMCIDispatchImpl<SnapIn>, // the View interface
  364. public CTiedComObject<CMTSnapInNode>
  365. {
  366. typedef CMTSnapInNode CMyTiedObject;
  367. typedef std::auto_ptr<CSnapinAbout> SnapinAboutPtr;
  368. public:
  369. BEGIN_MMC_COM_MAP(CMMCSnapIn)
  370. END_MMC_COM_MAP()
  371. public:
  372. MMC_METHOD1(get_Name, PBSTR /*pbstrName*/);
  373. STDMETHOD(get_Vendor)( PBSTR pbstrVendor );
  374. STDMETHOD(get_Version)( PBSTR pbstrVersion );
  375. MMC_METHOD1(get_Extensions, PPEXTENSIONS /*ppExtensions*/);
  376. MMC_METHOD1(get_SnapinCLSID,PBSTR /*pbstrSnapinCLSID*/);
  377. MMC_METHOD1(get_Properties, PPPROPERTIES /*ppProperties*/);
  378. MMC_METHOD1(EnableAllExtensions, BOOL /*bEnable*/);
  379. // not an interface method,
  380. // just a convenient way to reach for tied object's method
  381. MMC_METHOD1(GetSnapinClsid, CLSID& /*clsid*/);
  382. CMTSnapInNode *GetMTSnapInNode();
  383. private:
  384. ::SC ScGetSnapinAbout(CSnapinAbout*& pAbout);
  385. private:
  386. SnapinAboutPtr m_spSnapinAbout;
  387. };
  388. /*+-------------------------------------------------------------------------*
  389. * class CExtension
  390. *
  391. *
  392. * PURPOSE: The COM 0bject that exposes the SnapIn interface.
  393. *
  394. * This extension is not tied to any object. An extension snapin instance
  395. * can be uniquely identified by combination of its class-id & its primary
  396. * snapin's class-id. So this object just stores this data.
  397. * See addsnpin.h for more comments.
  398. *
  399. *+-------------------------------------------------------------------------*/
  400. class CExtension :
  401. public CMMCIDispatchImpl<Extension>
  402. {
  403. typedef std::auto_ptr<CSnapinAbout> SnapinAboutPtr;
  404. public:
  405. BEGIN_MMC_COM_MAP(CExtension)
  406. END_MMC_COM_MAP()
  407. public:
  408. STDMETHODIMP get_Name( PBSTR pbstrName);
  409. STDMETHODIMP get_Vendor( PBSTR pbstrVendor);
  410. STDMETHODIMP get_Version( PBSTR pbstrVersion);
  411. STDMETHODIMP get_Extensions( PPEXTENSIONS ppExtensions);
  412. STDMETHODIMP get_SnapinCLSID( PBSTR pbstrSnapinCLSID);
  413. STDMETHODIMP EnableAllExtensions(BOOL bEnable);
  414. STDMETHODIMP Enable(BOOL bEnable = TRUE);
  415. CExtension() : m_clsidAbout(GUID_NULL) {}
  416. void Init(const CLSID& clsidExtendingSnapin, const CLSID& clsidThisExtension, const CLSID& clsidAbout)
  417. {
  418. m_clsidExtendingSnapin = clsidExtendingSnapin;
  419. m_clsidThisExtension = clsidThisExtension;
  420. m_clsidAbout = clsidAbout;
  421. }
  422. LPCOLESTR GetVersion()
  423. {
  424. CSnapinAbout *pSnapinAbout = GetSnapinAbout();
  425. if (! pSnapinAbout)
  426. return NULL;
  427. return pSnapinAbout->GetVersion();
  428. }
  429. LPCOLESTR GetVendor()
  430. {
  431. CSnapinAbout *pSnapinAbout = GetSnapinAbout();
  432. if (! pSnapinAbout)
  433. return NULL;
  434. return pSnapinAbout->GetCompanyName();
  435. }
  436. private:
  437. CSnapinAbout* GetSnapinAbout()
  438. {
  439. // If about object is already created just return it.
  440. if (m_spExtensionAbout.get())
  441. return m_spExtensionAbout.get();
  442. if (m_clsidAbout == GUID_NULL)
  443. return NULL;
  444. // Else create & initialize the about object.
  445. m_spExtensionAbout = SnapinAboutPtr (new CSnapinAbout);
  446. if (! m_spExtensionAbout.get())
  447. return NULL;
  448. if (m_spExtensionAbout->GetSnapinInformation(m_clsidAbout))
  449. return m_spExtensionAbout.get();
  450. return NULL;
  451. }
  452. private:
  453. CLSID m_clsidThisExtension;
  454. CLSID m_clsidExtendingSnapin;
  455. CLSID m_clsidAbout;
  456. SnapinAboutPtr m_spExtensionAbout;
  457. };
  458. //############################################################################
  459. //############################################################################
  460. //
  461. // Implementation of class CExtensions
  462. //
  463. //############################################################################
  464. //############################################################################
  465. /*+-------------------------------------------------------------------------*
  466. * class CExtensions
  467. *
  468. *
  469. * PURPOSE: Implements the Extensions automation interface.
  470. *
  471. * The Scget_Extensions uses this class as a template parameter to the typedef
  472. * below. The typedef is an array of Extension objects, that needs atleast below
  473. * empty class declared. Scget_Extensions adds the extensions to the array.
  474. *
  475. * typedef CComObject< CMMCArrayEnum<Extensions, Extension> > CMMCExtensions;
  476. *
  477. *+-------------------------------------------------------------------------*/
  478. class CExtensions :
  479. public CMMCIDispatchImpl<Extensions>,
  480. public CTiedObject // enumerators are tied to it
  481. {
  482. protected:
  483. typedef void CMyTiedObject;
  484. };
  485. // Helper functions used by both CMMCSnapIn as well as CExtension.
  486. SC Scget_Extensions(const CLSID& clsidPrimarySnapin, PPEXTENSIONS ppExtensions);
  487. SC ScEnableAllExtensions (const CLSID& clsidPrimarySnapin, BOOL bEnable);
  488. //+-------------------------------------------------------------------
  489. //
  490. // Member: Scget_Extensions
  491. //
  492. // Synopsis: Helper function, given class-id of primary creates &
  493. // returns the extensions collection for this snapin.
  494. //
  495. // Arguments: [clsidPrimarySnapin] -
  496. // [ppExtensions] - out param, extensions collection.
  497. //
  498. // Returns: SC
  499. //
  500. // Note: Collection does not include dynamic extensions.
  501. //
  502. //--------------------------------------------------------------------
  503. SC Scget_Extensions(const CLSID& clsidPrimarySnapin, PPEXTENSIONS ppExtensions)
  504. {
  505. DECLARE_SC(sc, TEXT("Scget_Extensions"));
  506. sc = ScCheckPointers(ppExtensions);
  507. if (sc)
  508. return sc;
  509. *ppExtensions = NULL;
  510. // Create the extensions collection (which also implements the enumerator).
  511. typedef CComObject< CMMCArrayEnum<Extensions, Extension> > CMMCExtensions;
  512. CMMCExtensions *pMMCExtensions = NULL;
  513. sc = CMMCExtensions::CreateInstance(&pMMCExtensions);
  514. if (sc)
  515. return sc;
  516. sc = ScCheckPointers(pMMCExtensions, E_UNEXPECTED);
  517. if (sc)
  518. return sc;
  519. typedef CComPtr<Extension> CMMCExtensionPtr;
  520. typedef std::vector<CMMCExtensionPtr> ExtensionSnapins;
  521. ExtensionSnapins extensions;
  522. // Now get the extensions for this collection from this snapin.
  523. CExtensionsCache extnsCache;
  524. sc = MMCGetExtensionsForSnapIn(clsidPrimarySnapin, extnsCache);
  525. if (sc)
  526. return sc;
  527. // Create Extension object for each non-dynamic extension.
  528. CExtensionsCacheIterator it(extnsCache);
  529. for (; it.IsEnd() == FALSE; it.Advance())
  530. {
  531. // Collection does not include dynamic extensions.
  532. if (CExtSI::EXT_TYPE_DYNAMIC & it.GetValue())
  533. continue;
  534. typedef CComObject<CExtension> CMMCExtensionSnap;
  535. CMMCExtensionSnap *pExtension = NULL;
  536. sc = CMMCExtensionSnap::CreateInstance(&pExtension);
  537. if (sc)
  538. return sc;
  539. sc = ScCheckPointers(pExtension, E_UNEXPECTED);
  540. if (sc)
  541. return sc;
  542. CLSID clsidAbout;
  543. sc = ScGetAboutFromSnapinCLSID(it.GetKey(), clsidAbout);
  544. if (sc)
  545. sc.TraceAndClear();
  546. // Make the Extension aware of its primary snapin & about object.
  547. pExtension->Init(clsidPrimarySnapin, it.GetKey(), clsidAbout);
  548. extensions.push_back(pExtension);
  549. }
  550. // Fill this data into the extensions collection.
  551. pMMCExtensions->Init(extensions.begin(), extensions.end());
  552. sc = pMMCExtensions->QueryInterface(ppExtensions);
  553. if (sc)
  554. return sc;
  555. return (sc);
  556. }
  557. //+-------------------------------------------------------------------
  558. //
  559. // Member: ScEnableAllExtensions
  560. //
  561. // Synopsis: Helper function, given class-id of primary enables
  562. // all extensions or un-checks the enable all so that
  563. // individual extension can be disabled.
  564. //
  565. // Arguments: [clsidPrimarySnapin] -
  566. // [bEnable] - enable or disable.
  567. //
  568. // Returns: SC
  569. //
  570. // Note: Collection does not include dynamic extensions.
  571. //
  572. //--------------------------------------------------------------------
  573. SC ScEnableAllExtensions (const CLSID& clsidPrimarySnapin, BOOL bEnable)
  574. {
  575. DECLARE_SC(sc, _T("ScEnableAllExtensions"));
  576. // Create snapin manager.
  577. CScopeTree *pScopeTree = CScopeTree::GetScopeTree();
  578. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  579. if (sc)
  580. return sc.ToHr();
  581. CSnapinManager snapinMgr(pScopeTree->GetRoot());
  582. // Ask the snapinMgr to enable/disable its extensions.
  583. sc = snapinMgr.ScEnableAllExtensions(clsidPrimarySnapin, bEnable);
  584. if (sc)
  585. return sc.ToHr();
  586. // Update the scope tree with changes made by snapin manager.
  587. sc = pScopeTree->ScAddOrRemoveSnapIns(snapinMgr.GetDeletedNodesList(),
  588. snapinMgr.GetNewNodes());
  589. if (sc)
  590. return sc.ToHr();
  591. return (sc);
  592. }
  593. //+-------------------------------------------------------------------
  594. //
  595. // Member: CExtension::get_Name
  596. //
  597. // Synopsis: Return the name of this extension.
  598. //
  599. // Arguments:
  600. //
  601. // Returns: SC
  602. //
  603. //--------------------------------------------------------------------
  604. STDMETHODIMP CExtension::get_Name (PBSTR pbstrName)
  605. {
  606. DECLARE_SC(sc, _T("CExtension::get_Name"));
  607. sc = ScCheckPointers(pbstrName);
  608. if (sc)
  609. return sc.ToHr();
  610. *pbstrName = NULL;
  611. tstring tszSnapinName;
  612. bool bRet = GetSnapinNameFromCLSID(m_clsidThisExtension, tszSnapinName);
  613. if (!bRet)
  614. return (sc = E_FAIL).ToHr();
  615. USES_CONVERSION;
  616. *pbstrName = SysAllocString(T2COLE(tszSnapinName.data()));
  617. if ( (! *pbstrName) && (tszSnapinName.length() > 0) )
  618. return (sc = E_OUTOFMEMORY).ToHr();
  619. return sc.ToHr();
  620. }
  621. //+-------------------------------------------------------------------
  622. //
  623. // Member: CExtension::get_Vendor
  624. //
  625. // Synopsis: Get the vendor information for this extension if it exists.
  626. //
  627. // Arguments: [pbstrVendor] - out param, ptr to vendor info.
  628. //
  629. // Returns: HRESULT
  630. //
  631. //--------------------------------------------------------------------
  632. STDMETHODIMP CExtension::get_Vendor (PBSTR pbstrVendor)
  633. {
  634. DECLARE_SC(sc, _T("CExtension::get_Vendor"));
  635. sc = ScCheckPointers(pbstrVendor);
  636. if (sc)
  637. return sc.ToHr();
  638. LPCOLESTR lpszVendor = GetVendor();
  639. *pbstrVendor = SysAllocString(lpszVendor);
  640. if ((lpszVendor) && (! *pbstrVendor))
  641. return (sc = E_OUTOFMEMORY).ToHr();
  642. return (sc.ToHr());
  643. }
  644. //+-------------------------------------------------------------------
  645. //
  646. // Member: CExtension::get_Version
  647. //
  648. // Synopsis: Get the version info for this extension if it exists.
  649. //
  650. // Arguments: [pbstrVersion] - out param, ptr to version info.
  651. //
  652. // Returns: HRESULT
  653. //
  654. //--------------------------------------------------------------------
  655. STDMETHODIMP CExtension::get_Version (PBSTR pbstrVersion)
  656. {
  657. DECLARE_SC(sc, _T("CExtension::get_Version"));
  658. sc = ScCheckPointers(pbstrVersion);
  659. if (sc)
  660. return sc.ToHr();
  661. LPCOLESTR lpszVersion = GetVersion();
  662. *pbstrVersion = SysAllocString(lpszVersion);
  663. if ((lpszVersion) && (! *pbstrVersion))
  664. return (sc = E_OUTOFMEMORY).ToHr();
  665. return (sc.ToHr());
  666. }
  667. //+-------------------------------------------------------------------
  668. //
  669. // Member: CExtension::get_SnapinCLSID
  670. //
  671. // Synopsis: Get the extension snapin class-id.
  672. //
  673. // Arguments: [pbstrSnapinCLSID] - out param, snapin class-id.
  674. //
  675. // Returns: HRESULT
  676. //
  677. //--------------------------------------------------------------------
  678. STDMETHODIMP CExtension::get_SnapinCLSID (PBSTR pbstrSnapinCLSID)
  679. {
  680. DECLARE_SC(sc, _T("CExtension::get_SnapinCLSID"));
  681. sc = ScCheckPointers(pbstrSnapinCLSID);
  682. if (sc)
  683. return sc.ToHr();
  684. CCoTaskMemPtr<OLECHAR> szSnapinClsid;
  685. sc = StringFromCLSID(m_clsidThisExtension, &szSnapinClsid);
  686. if (sc)
  687. return sc.ToHr();
  688. *pbstrSnapinCLSID = SysAllocString(szSnapinClsid);
  689. if (! *pbstrSnapinCLSID)
  690. sc = E_OUTOFMEMORY;
  691. return (sc.ToHr());
  692. }
  693. //+-------------------------------------------------------------------
  694. //
  695. // Member: CExtension::ScEnable
  696. //
  697. // Synopsis: Enable or disable this extension
  698. //
  699. // Arguments: [bEnable] - enable or disable.
  700. //
  701. // Returns: SC
  702. //
  703. //--------------------------------------------------------------------
  704. STDMETHODIMP CExtension::Enable (BOOL bEnable /*= TRUE*/)
  705. {
  706. DECLARE_SC(sc, _T("CExtension::ScEnable"));
  707. /*
  708. * 1. Create snapin manager.
  709. * 2. Ask snapin mgr to disable this snapin.
  710. */
  711. // Create snapin manager.
  712. CScopeTree *pScopeTree = CScopeTree::GetScopeTree();
  713. sc = ScCheckPointers(pScopeTree, E_UNEXPECTED);
  714. if (sc)
  715. return sc.ToHr();
  716. CSnapinManager snapinMgr(pScopeTree->GetRoot());
  717. // Ask the snapinMgr to disable this extension.
  718. sc = snapinMgr.ScEnableExtension(m_clsidExtendingSnapin, m_clsidThisExtension, bEnable);
  719. if (sc)
  720. return sc.ToHr();
  721. // Update the scope tree with changes made by snapin manager.
  722. sc = pScopeTree->ScAddOrRemoveSnapIns(snapinMgr.GetDeletedNodesList(),
  723. snapinMgr.GetNewNodes());
  724. if (sc)
  725. return sc.ToHr();
  726. return sc.ToHr();
  727. }
  728. //+-------------------------------------------------------------------
  729. //
  730. // Member: CExtension::Scget_Extensions
  731. //
  732. // Synopsis: Get the extensions collection for this snapin.
  733. //
  734. // Arguments: [ppExtensions] - out ptr to extensions collection.
  735. //
  736. // Returns: SC
  737. //
  738. //--------------------------------------------------------------------
  739. HRESULT CExtension::get_Extensions( PPEXTENSIONS ppExtensions)
  740. {
  741. DECLARE_SC(sc, _T("CExtension::get_Extensions"));
  742. sc = ScCheckPointers(ppExtensions);
  743. if (sc)
  744. return sc.ToHr();
  745. *ppExtensions = NULL;
  746. sc = ::Scget_Extensions(m_clsidThisExtension, ppExtensions);
  747. if (sc)
  748. return sc.ToHr();
  749. return (sc.ToHr());
  750. }
  751. //+-------------------------------------------------------------------
  752. //
  753. // Member: CExtension::EnableAllExtensions
  754. //
  755. // Synopsis: Enable/Disable all the extensions of this snapin.
  756. //
  757. // Arguments:
  758. //
  759. // Returns: HRESULT
  760. //
  761. //--------------------------------------------------------------------
  762. STDMETHODIMP CExtension::EnableAllExtensions(BOOL bEnable)
  763. {
  764. DECLARE_SC(sc, TEXT("CExtension::EnableAllExtensions"));
  765. sc = ::ScEnableAllExtensions(m_clsidThisExtension, bEnable);
  766. if (sc)
  767. return sc.ToHr();
  768. return sc.ToHr();
  769. }
  770. //+-------------------------------------------------------------------
  771. //
  772. // Member: CMTSnapInNode::ScGetCMTSnapinNode
  773. //
  774. // Synopsis: Static function, given PSNAPIN (SnapIn interface)
  775. // return the CMTSnapInNode of that snapin.
  776. //
  777. // Arguments: [pSnapIn] - Snapin interface.
  778. //
  779. // Returns: SC
  780. //
  781. //--------------------------------------------------------------------
  782. SC CMTSnapInNode::ScGetCMTSnapinNode(PSNAPIN pSnapIn, CMTSnapInNode **ppMTSnapInNode)
  783. {
  784. DECLARE_SC(sc, _T("CMTSnapInNode::GetCMTSnapinNode"));
  785. sc = ScCheckPointers(pSnapIn, ppMTSnapInNode);
  786. if (sc)
  787. return sc;
  788. *ppMTSnapInNode = NULL;
  789. CMMCSnapIn *pMMCSnapIn = dynamic_cast<CMMCSnapIn*>(pSnapIn);
  790. if (!pMMCSnapIn)
  791. return (sc = E_UNEXPECTED);
  792. *ppMTSnapInNode = pMMCSnapIn->GetMTSnapInNode();
  793. return (sc);
  794. }
  795. //+-------------------------------------------------------------------
  796. //
  797. // Member: CMTSnapInNode::Scget_Name
  798. //
  799. // Synopsis: Return the name of this snapin.
  800. //
  801. // Arguments:
  802. //
  803. // Returns: SC
  804. //
  805. //--------------------------------------------------------------------
  806. SC CMTSnapInNode::Scget_Name (PBSTR pbstrName)
  807. {
  808. DECLARE_SC(sc, _T("CMTSnapInNode::Scget_Name"));
  809. sc = ScCheckPointers(pbstrName);
  810. if (sc)
  811. return sc;
  812. *pbstrName = NULL;
  813. CSnapIn *pSnapin = GetPrimarySnapIn();
  814. sc = ScCheckPointers(pSnapin, E_UNEXPECTED);
  815. if (sc)
  816. return sc;
  817. WTL::CString strSnapInName;
  818. sc = pSnapin->ScGetSnapInName(strSnapInName);
  819. if (sc)
  820. return sc;
  821. USES_CONVERSION;
  822. *pbstrName = strSnapInName.AllocSysString();
  823. if ( (! *pbstrName) && (strSnapInName.GetLength() > 0) )
  824. return (sc = E_OUTOFMEMORY);
  825. return (sc);
  826. }
  827. //+-------------------------------------------------------------------
  828. //
  829. // Member: CMTSnapInNode::Scget_Extensions
  830. //
  831. // Synopsis: Get the extensions collection for this snapin.
  832. //
  833. // Arguments: [ppExtensions] - out ptr to extensions collection.
  834. //
  835. // Returns: SC
  836. //
  837. //--------------------------------------------------------------------
  838. SC CMTSnapInNode::Scget_Extensions( PPEXTENSIONS ppExtensions)
  839. {
  840. DECLARE_SC(sc, _T("CMTSnapInNode::Scget_Extensions"));
  841. sc = ScCheckPointers(ppExtensions);
  842. if (sc)
  843. return sc;
  844. *ppExtensions = NULL;
  845. CSnapIn *pSnapin = GetPrimarySnapIn();
  846. sc = ScCheckPointers(pSnapin, E_UNEXPECTED);
  847. if (sc)
  848. return sc;
  849. sc = ::Scget_Extensions(pSnapin->GetSnapInCLSID(), ppExtensions);
  850. if (sc)
  851. return sc;
  852. return (sc);
  853. }
  854. //+-------------------------------------------------------------------
  855. //
  856. // Member: CMTSnapInNode::ScGetSnapinClsid
  857. //
  858. // Synopsis: Gets the CLSID of snapin
  859. //
  860. // Arguments: CLSID& clsid [out] - class id of snapin.
  861. //
  862. // Returns: SC
  863. //
  864. //--------------------------------------------------------------------
  865. SC CMTSnapInNode::ScGetSnapinClsid(CLSID& clsid)
  866. {
  867. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScGetAboutClsid"));
  868. // init out param
  869. clsid = GUID_NULL;
  870. CSnapIn *pSnapin = GetPrimarySnapIn();
  871. sc = ScCheckPointers(pSnapin, E_UNEXPECTED);
  872. if (sc)
  873. return sc;
  874. clsid = pSnapin->GetSnapInCLSID();
  875. return sc;
  876. }
  877. //+-------------------------------------------------------------------
  878. //
  879. // Member: CMTSnapInNode::Scget_SnapinCLSID
  880. //
  881. // Synopsis: Get the CLSID for this snapin.
  882. //
  883. // Arguments: [pbstrSnapinCLSID] - out ptr to CLSID.
  884. //
  885. // Returns: SC
  886. //
  887. //--------------------------------------------------------------------
  888. SC CMTSnapInNode::Scget_SnapinCLSID( PBSTR pbstrSnapinCLSID)
  889. {
  890. DECLARE_SC(sc, TEXT("CMTSnapInNode::Scget_SnapinCLSID"));
  891. sc = ScCheckPointers(pbstrSnapinCLSID);
  892. if (sc)
  893. return sc;
  894. CSnapIn *pSnapin = GetPrimarySnapIn();
  895. sc = ScCheckPointers(pSnapin, E_UNEXPECTED);
  896. if (sc)
  897. return sc;
  898. CCoTaskMemPtr<OLECHAR> szSnapinClsid;
  899. sc = StringFromCLSID(pSnapin->GetSnapInCLSID(), &szSnapinClsid);
  900. if (sc)
  901. return sc.ToHr();
  902. *pbstrSnapinCLSID = SysAllocString(szSnapinClsid);
  903. if (! *pbstrSnapinCLSID)
  904. sc = E_OUTOFMEMORY;
  905. return sc;
  906. }
  907. //+-------------------------------------------------------------------
  908. //
  909. // Member: CMTSnapInNode::ScEnableAllExtensions
  910. //
  911. // Synopsis: Enable or not enable all extensions of this snapin.
  912. //
  913. // Arguments:
  914. //
  915. // Returns: SC
  916. //
  917. //--------------------------------------------------------------------
  918. SC CMTSnapInNode::ScEnableAllExtensions (BOOL bEnable)
  919. {
  920. DECLARE_SC(sc, _T("CMTSnapInNode::ScEnableAllExtensions"));
  921. CSnapIn *pSnapin = GetPrimarySnapIn();
  922. sc = ScCheckPointers(pSnapin, E_UNEXPECTED);
  923. if (sc)
  924. return sc;
  925. sc = ::ScEnableAllExtensions(pSnapin->GetSnapInCLSID(), bEnable);
  926. if (sc)
  927. return sc;
  928. return (sc);
  929. }
  930. /*+-------------------------------------------------------------------------*
  931. * CMTSnapInNode::Scget_Properties
  932. *
  933. * Returns a pointer to the snap-in's Properties object
  934. *--------------------------------------------------------------------------*/
  935. SC CMTSnapInNode::Scget_Properties( PPPROPERTIES ppProperties)
  936. {
  937. DECLARE_SC (sc, _T("CMTSnapInNode::Scget_Properties"));
  938. /*
  939. * validate parameters
  940. */
  941. sc = ScCheckPointers (ppProperties);
  942. if (sc)
  943. return (sc);
  944. *ppProperties = m_spProps;
  945. /*
  946. * If the snap-in doesn't support ISnapinProperties, don't return
  947. * a Properties interface. This is not an error, but rather a valid
  948. * unsuccessful return, so we return E_NOINTERFACE directly instead
  949. * of assigning to sc first.
  950. */
  951. if (m_spProps == NULL)
  952. return (E_NOINTERFACE);
  953. /*
  954. * put a ref on for the client
  955. */
  956. (*ppProperties)->AddRef();
  957. return (sc);
  958. }
  959. /*+-------------------------------------------------------------------------*
  960. *
  961. * CMTSnapInNode::ScGetSnapIn
  962. *
  963. * PURPOSE: Returns a pointer to the SnapIn object.
  964. *
  965. * PARAMETERS:
  966. * PPSNAPIN ppSnapIn :
  967. *
  968. * RETURNS:
  969. * SC
  970. *
  971. *+-------------------------------------------------------------------------*/
  972. SC
  973. CMTSnapInNode::ScGetSnapIn(PPSNAPIN ppSnapIn)
  974. {
  975. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScGetSnapIn"));
  976. sc = ScCheckPointers(ppSnapIn);
  977. if(sc)
  978. return sc;
  979. // initialize out parameter
  980. *ppSnapIn = NULL;
  981. // create a CMMCView if needed.
  982. sc = CTiedComObjectCreator<CMMCSnapIn>::ScCreateAndConnect(*this, m_spSnapIn);
  983. if(sc)
  984. return sc;
  985. if(m_spSnapIn == NULL)
  986. {
  987. sc = E_UNEXPECTED;
  988. return sc;
  989. }
  990. // addref the pointer for the client.
  991. m_spSnapIn->AddRef();
  992. *ppSnapIn = m_spSnapIn;
  993. return sc;
  994. }
  995. HRESULT CMTNode::OpenStorageForNode()
  996. {
  997. if (m_spNodeStorage != NULL)
  998. return S_OK;
  999. ASSERT(m_spPersistData != NULL);
  1000. if (m_spPersistData == NULL)
  1001. return E_POINTER;
  1002. // Get the storage for all of the nodes
  1003. IStorage* const pAllNodes = m_spPersistData->GetNodeStorage();
  1004. ASSERT(pAllNodes != NULL);
  1005. if (pAllNodes == NULL)
  1006. return E_POINTER;
  1007. // Create the outer storage for this node
  1008. WCHAR name[MAX_PATH];
  1009. HRESULT hr = OpenDebugStorage(pAllNodes, GetStorageName(name),
  1010. STGM_READWRITE | STGM_SHARE_EXCLUSIVE, L"\\node\\#", &m_spNodeStorage);
  1011. return hr == S_OK ? S_OK : E_FAIL;
  1012. }
  1013. HRESULT CMTNode::OpenStorageForView()
  1014. {
  1015. if (m_spViewStorage != NULL)
  1016. return S_OK;
  1017. // Get the storage for all of the nodes
  1018. IStorage* const pNodeStorage = GetNodeStorage();
  1019. ASSERT(pNodeStorage != NULL);
  1020. if (pNodeStorage == NULL)
  1021. return E_FAIL;
  1022. // Create the outer storage for this node
  1023. WCHAR name[MAX_PATH];
  1024. HRESULT hr = OpenDebugStorage(pNodeStorage, L"view",
  1025. STGM_READWRITE | STGM_SHARE_EXCLUSIVE, L"\\node\\#\\view",
  1026. &m_spViewStorage);
  1027. return hr == S_OK ? S_OK : E_FAIL;
  1028. }
  1029. HRESULT CMTNode::OpenStorageForCD()
  1030. {
  1031. if (m_spCDStorage != NULL)
  1032. return S_OK;
  1033. // Get the storage for all of the nodes
  1034. IStorage* const pNodeStorage = GetNodeStorage();
  1035. ASSERT(pNodeStorage != NULL);
  1036. if (pNodeStorage == NULL)
  1037. return E_FAIL;
  1038. // Create the outer storage for this node
  1039. WCHAR name[MAX_PATH];
  1040. HRESULT hr = OpenDebugStorage(pNodeStorage, L"data",
  1041. STGM_READWRITE | STGM_SHARE_EXCLUSIVE, L"\\node\\#\\data",
  1042. &m_spCDStorage);
  1043. return hr == S_OK ? S_OK : E_FAIL;
  1044. }
  1045. HRESULT CMTNode::OpenTreeStream()
  1046. {
  1047. if (m_spTreeStream != NULL)
  1048. {
  1049. const LARGE_INTEGER loc = {0,0};
  1050. ULARGE_INTEGER newLoc;
  1051. HRESULT hr = m_spTreeStream->Seek(loc, STREAM_SEEK_SET, &newLoc);
  1052. ASSERT(SUCCEEDED(hr));
  1053. if (FAILED(hr))
  1054. return E_FAIL;
  1055. return S_OK;
  1056. }
  1057. HRESULT hr = OpenStorageForNode();
  1058. ASSERT(SUCCEEDED(hr));
  1059. if (FAILED(hr))
  1060. return E_FAIL;
  1061. hr = OpenStorageForView();
  1062. ASSERT(SUCCEEDED(hr));
  1063. if (FAILED(hr))
  1064. return E_FAIL;
  1065. hr = OpenStorageForCD();
  1066. ASSERT(SUCCEEDED(hr));
  1067. if (FAILED(hr))
  1068. return E_FAIL;
  1069. IStorage* const pTreeNodes = GetNodeStorage();
  1070. ASSERT(pTreeNodes != NULL);
  1071. if (pTreeNodes == NULL)
  1072. return E_POINTER;
  1073. hr = OpenDebugStream(pTreeNodes, L"tree",
  1074. STGM_READWRITE | STGM_SHARE_EXCLUSIVE, L"\\node\\#\\tree", &m_spTreeStream);
  1075. ASSERT(SUCCEEDED(hr) && m_spTreeStream != NULL);
  1076. return SUCCEEDED(hr) ? S_OK : E_FAIL;
  1077. }
  1078. /*+-------------------------------------------------------------------------*
  1079. *
  1080. * CMTSnapInNode::NextStaticNode
  1081. *
  1082. * PURPOSE:
  1083. *
  1084. * PARAMETERS:
  1085. *
  1086. * RETURNS: NULL if not found, else the next CMTSnapInNode.
  1087. * inline
  1088. *
  1089. * NOTE: This performance is poor! Improve by indexing all CMTSnapInNodes
  1090. * separately.
  1091. *+-------------------------------------------------------------------------*/
  1092. CMTNode*
  1093. CMTNode::NextStaticNode()
  1094. {
  1095. CMTNode *pNext = this;
  1096. while (pNext)
  1097. {
  1098. if (pNext->IsStaticNode())
  1099. return pNext;
  1100. pNext = pNext->Next();
  1101. }
  1102. return NULL;
  1103. }
  1104. HRESULT CMTNode::IsDirty()
  1105. {
  1106. if (GetDirty())
  1107. {
  1108. TraceDirtyFlag(TEXT("CMTNode"), true);
  1109. return S_OK;
  1110. }
  1111. HRESULT hr;
  1112. CMTNode* const pChild = m_pChild->NextStaticNode();
  1113. if (pChild)
  1114. {
  1115. hr = pChild->IsDirty();
  1116. ASSERT(SUCCEEDED(hr));
  1117. if (FAILED(hr))
  1118. return hr;
  1119. if (hr != S_FALSE)
  1120. {
  1121. TraceDirtyFlag(TEXT("CMTNode"), true);
  1122. return hr;
  1123. }
  1124. }
  1125. CMTNode* const pNext = m_pNext->NextStaticNode();
  1126. if (pNext)
  1127. {
  1128. hr = pNext->IsDirty();
  1129. ASSERT(SUCCEEDED(hr));
  1130. if (FAILED(hr))
  1131. return hr;
  1132. if (hr != S_FALSE)
  1133. {
  1134. TraceDirtyFlag(TEXT("CMTNode"), true);
  1135. return hr;
  1136. }
  1137. }
  1138. TraceDirtyFlag(TEXT("CMTNode"), false);
  1139. return S_FALSE;
  1140. }
  1141. /*+-------------------------------------------------------------------------*
  1142. *
  1143. * CMTNode::InitNew
  1144. *
  1145. * PURPOSE:
  1146. *
  1147. * PARAMETERS:
  1148. * PersistData* d :
  1149. *
  1150. * RETURNS:
  1151. * HRESULT
  1152. *
  1153. *+-------------------------------------------------------------------------*/
  1154. HRESULT
  1155. CMTNode::InitNew(PersistData* d)
  1156. {
  1157. SC sc;
  1158. CStream treeStream;
  1159. if ( (m_spPersistData != NULL) || (d==NULL) || !IsStaticNode())
  1160. goto FailedError;
  1161. m_spPersistData = d;
  1162. if (m_spPersistData == NULL)
  1163. goto ArgumentError;
  1164. sc = InitNew();
  1165. if(sc)
  1166. goto Error;
  1167. // Get the stream for persistence of the tree
  1168. treeStream.Attach( m_spPersistData->GetTreeStream());
  1169. // recurse thru children
  1170. {
  1171. CMTNode* const pChild = m_pChild->NextStaticNode();
  1172. if (pChild)
  1173. {
  1174. sc = pChild->InitNew(d);
  1175. if(sc)
  1176. goto Error;
  1177. }
  1178. else
  1179. {
  1180. sc = ScWriteEmptyNode(treeStream);
  1181. if(sc)
  1182. goto Error;
  1183. }
  1184. }
  1185. // chain to next node.
  1186. {
  1187. CMTNode* const pNext = m_pNext->NextStaticNode();
  1188. if (pNext)
  1189. {
  1190. sc = pNext->InitNew(d);
  1191. if(sc)
  1192. goto Error;
  1193. }
  1194. else
  1195. {
  1196. sc = ScWriteEmptyNode(treeStream);
  1197. if(sc)
  1198. goto Error;
  1199. }
  1200. }
  1201. Cleanup:
  1202. return HrFromSc(sc);
  1203. FailedError:
  1204. sc = E_FAIL;
  1205. goto Error;
  1206. ArgumentError:
  1207. sc = E_INVALIDARG;
  1208. Error:
  1209. TraceError(TEXT("CMTNode::InitNew"), sc);
  1210. goto Cleanup;
  1211. }
  1212. /*+-------------------------------------------------------------------------*
  1213. *
  1214. * CMTNode::Persist
  1215. *
  1216. * PURPOSE: Persists the CMTNode to the specified persistor.
  1217. *
  1218. * PARAMETERS:
  1219. * CPersistor& persistor :
  1220. *
  1221. * RETURNS:
  1222. * void
  1223. *
  1224. *+-------------------------------------------------------------------------*/
  1225. void CMTNode::Persist(CPersistor& persistor)
  1226. {
  1227. MTNODEID id = GetID(); // persist the node id
  1228. persistor.PersistAttribute(XML_ATTR_MT_NODE_ID, id);
  1229. SetID(id);
  1230. // Save the children
  1231. CPersistor persistorSubNode(persistor, XML_TAG_SCOPE_TREE_NODES);
  1232. if (persistor.IsStoring())
  1233. {
  1234. CMTNode* pChild = m_pChild->NextStaticNode();
  1235. while (pChild)
  1236. {
  1237. persistorSubNode.Persist(*pChild);
  1238. // get next node
  1239. pChild = pChild->Next();
  1240. // advance if it is not a static node
  1241. pChild = (pChild ? pChild->NextStaticNode() : NULL);
  1242. }
  1243. ClearDirty();
  1244. }
  1245. else
  1246. {
  1247. XMLListCollectionBase::Persist(persistorSubNode);
  1248. }
  1249. UINT nImage = m_nImage;
  1250. if (nImage > eStockImage_Max) // if SnapIn changed icon dynamically, then
  1251. nImage = eStockImage_Folder; // this value will be bogus next time:
  1252. // replace w/ 0 (closed folder)
  1253. persistor.PersistAttribute(XML_ATTR_MT_NODE_IMAGE, nImage);
  1254. persistor.PersistString(XML_ATTR_MT_NODE_NAME, m_strName);
  1255. }
  1256. /*+-------------------------------------------------------------------------*
  1257. *
  1258. * CMTNode::OnNewElement
  1259. *
  1260. * PURPOSE: called for each new child node found in XML doc
  1261. *
  1262. * PARAMETERS:
  1263. * CPersistor& persistor :
  1264. *
  1265. * RETURNS:
  1266. * void
  1267. *
  1268. *+-------------------------------------------------------------------------*/
  1269. void CMTNode::OnNewElement(CPersistor& persistor)
  1270. {
  1271. DECLARE_SC(sc, TEXT("CMTNode::OnNewElement"));
  1272. // load the child
  1273. CMTNode* pChild;
  1274. // attach to the list
  1275. PersistNewNode(persistor, &pChild);
  1276. if (pChild)
  1277. {
  1278. pChild->SetParent(this);
  1279. CMTNode** ppLast = &m_pChild;
  1280. while (*ppLast) ppLast = &(*ppLast)->m_pNext;
  1281. *ppLast = pChild;
  1282. }
  1283. }
  1284. /*+-------------------------------------------------------------------------*
  1285. *
  1286. * CMTNode::ScLoad
  1287. *
  1288. * PURPOSE: Loads the MTNode from the specified stream.
  1289. * COMPATIBILITY issues: MMC1.0 through MMC1.2 used special built-in
  1290. * node types to represent Folder, Web Link, and ActiveX control nodes.
  1291. * MMC2.0 and higher use snap-ins instead. The only special node is
  1292. * Console Root, which is still saved and loaded as a Folder node with
  1293. * ID = 1.
  1294. *
  1295. *
  1296. * RETURNS:
  1297. * SC
  1298. *
  1299. *+-------------------------------------------------------------------------*/
  1300. SC CMTNode::ScLoad(PersistData* d, CMTNode** ppNode)
  1301. {
  1302. DECLARE_SC(sc, TEXT("CMTNode::ScLoad"));
  1303. CMTSnapInNode* pmtSnapInNode = NULL;
  1304. CStream treeStream;
  1305. // check parameters
  1306. sc = ScCheckPointers(d, ppNode);
  1307. if(sc)
  1308. return sc;
  1309. *ppNode = NULL;
  1310. // Read the type of node from the stream.
  1311. treeStream.Attach(d->GetTreeStream());
  1312. int nt;
  1313. sc = treeStream.ScRead(&nt, sizeof(nt));
  1314. if(sc)
  1315. return sc;
  1316. if (!nt)
  1317. return sc;
  1318. if (!(nt == NODE_CODE_SNAPIN || nt == NODE_CODE_FOLDER ||
  1319. nt == NODE_CODE_HTML || nt == NODE_CODE_OCX))
  1320. return (sc = E_FAIL); // invalid node type.
  1321. // Read the storage key
  1322. MTNODEID id;
  1323. sc = treeStream.ScRead(&id, sizeof(id));
  1324. if(sc)
  1325. return sc;
  1326. // Create a node of the appropriate type. Everything, including Console Root
  1327. // uses CMTSnapInNode.
  1328. if( nt == NODE_CODE_FOLDER || nt == NODE_CODE_SNAPIN || nt == NODE_CODE_HTML || nt == NODE_CODE_OCX )
  1329. {
  1330. pmtSnapInNode = new CMTSnapInNode (NULL);
  1331. ASSERT(pmtSnapInNode != NULL);
  1332. if (pmtSnapInNode == NULL)
  1333. return E_POINTER;
  1334. *ppNode = pmtSnapInNode;
  1335. }
  1336. else
  1337. return (sc = E_UNEXPECTED); // should never happen
  1338. (*ppNode)->m_bLoaded = true;
  1339. ASSERT((*ppNode)->m_spPersistData == NULL);
  1340. ASSERT(d != NULL);
  1341. (*ppNode)->m_spPersistData = d;
  1342. ASSERT((*ppNode)->m_spPersistData != NULL);
  1343. if ((*ppNode)->m_spPersistData == NULL)
  1344. return E_INVALIDARG;
  1345. (*ppNode)->SetID(id);
  1346. if (id >= m_NextID)
  1347. m_NextID = id+1;
  1348. // Open the stream for the nodes data
  1349. sc = (*ppNode)->OpenTreeStream();
  1350. if (sc)
  1351. {
  1352. (*ppNode)->Release();
  1353. *ppNode = NULL;
  1354. return sc;
  1355. }
  1356. // Load the node
  1357. // If old style node, then convert to snap-in type node
  1358. switch (nt)
  1359. {
  1360. case NODE_CODE_SNAPIN:
  1361. sc = (*ppNode)->ScLoad();
  1362. break;
  1363. // All folder nodes, INCLUDING old-style console root nodes, are upgraded to snap-ins.
  1364. case NODE_CODE_FOLDER:
  1365. if(pmtSnapInNode == NULL)
  1366. return (sc = E_UNEXPECTED);
  1367. sc = pmtSnapInNode->ScConvertLegacyNode(CLSID_FolderSnapin);
  1368. break;
  1369. case NODE_CODE_HTML:
  1370. sc = pmtSnapInNode->ScConvertLegacyNode(CLSID_HTMLSnapin);
  1371. break;
  1372. case NODE_CODE_OCX:
  1373. sc = pmtSnapInNode->ScConvertLegacyNode(CLSID_OCXSnapin);
  1374. break;
  1375. default:
  1376. ASSERT(0 && "Invalid node type");
  1377. sc = E_FAIL;
  1378. }
  1379. if (sc)
  1380. {
  1381. (*ppNode)->Release();
  1382. *ppNode = NULL;
  1383. return sc;
  1384. }
  1385. // load the children
  1386. CMTNode* pChild;
  1387. sc = ScLoad(d, &pChild);
  1388. if (sc)
  1389. {
  1390. (*ppNode)->Release();
  1391. *ppNode = NULL;
  1392. return sc;
  1393. }
  1394. if (pChild)
  1395. pChild->SetParent(*ppNode);
  1396. (*ppNode)->m_pChild = pChild;
  1397. // Load siblings
  1398. CMTNode* pNext;
  1399. sc = ScLoad(d, &(*ppNode)->m_pNext);
  1400. if (sc)
  1401. {
  1402. (*ppNode)->Release();
  1403. *ppNode = NULL;
  1404. return sc;
  1405. }
  1406. (*ppNode)->SetDirty(false);
  1407. return sc;
  1408. }
  1409. /*+-------------------------------------------------------------------------*
  1410. *
  1411. * CMTNode::PersistNewNode
  1412. *
  1413. * PURPOSE: Loads the MTNode from the persistor.
  1414. *
  1415. *+-------------------------------------------------------------------------*/
  1416. void CMTNode::PersistNewNode(CPersistor &persistor, CMTNode** ppNode)
  1417. {
  1418. DECLARE_SC(sc, TEXT("CMTNode::PersistNewNode"));
  1419. CMTSnapInNode* pmtSnapInNode = NULL;
  1420. const int CONSOLE_ROOT_ID = 1;
  1421. // check parameters
  1422. sc = ScCheckPointers(ppNode);
  1423. if (sc)
  1424. sc.Throw();
  1425. *ppNode = NULL;
  1426. // Create a node of the snapin type. Everything uses CMTSnapInNode.
  1427. pmtSnapInNode = new CMTSnapInNode(NULL);
  1428. sc = ScCheckPointers(pmtSnapInNode,E_OUTOFMEMORY);
  1429. if (sc)
  1430. sc.Throw();
  1431. *ppNode = pmtSnapInNode;
  1432. (*ppNode)->m_bLoaded = true;
  1433. ASSERT((*ppNode)->m_spPersistData == NULL);
  1434. try
  1435. {
  1436. persistor.Persist(**ppNode);
  1437. }
  1438. catch(...)
  1439. {
  1440. // ensure cleanup here
  1441. (*ppNode)->Release();
  1442. *ppNode = NULL;
  1443. throw;
  1444. }
  1445. // update index for new nodes
  1446. MTNODEID id = (*ppNode)->GetID();
  1447. if (id >= m_NextID)
  1448. m_NextID = id+1;
  1449. (*ppNode)->SetDirty(false);
  1450. }
  1451. HRESULT CMTNode::DestroyElements()
  1452. {
  1453. if (!IsStaticNode())
  1454. return S_OK;
  1455. HRESULT hr;
  1456. if (m_pChild != NULL)
  1457. {
  1458. hr = m_pChild->DestroyElements();
  1459. ASSERT(SUCCEEDED(hr));
  1460. if (FAILED(hr))
  1461. return hr;
  1462. }
  1463. return DoDestroyElements();
  1464. }
  1465. HRESULT CMTNode::DoDestroyElements()
  1466. {
  1467. if (m_spPersistData == NULL)
  1468. return S_OK;
  1469. IStorage* const pNodeStorage = m_spPersistData->GetNodeStorage();
  1470. ASSERT(pNodeStorage != NULL);
  1471. if (pNodeStorage == NULL)
  1472. return S_OK;
  1473. WCHAR name[MAX_PATH];
  1474. HRESULT hr = pNodeStorage->DestroyElement(GetStorageName(name));
  1475. SetDirty();
  1476. CMTNode* const psParent = m_pParent != NULL ? m_pParent->GetStaticParent() : NULL;
  1477. if (psParent != NULL)
  1478. psParent->SetDirty();
  1479. return S_OK;
  1480. }
  1481. void CMTNode::SetParent(CMTNode* pParent)
  1482. {
  1483. m_pParent = pParent;
  1484. if (m_pNext)
  1485. m_pNext->SetParent(pParent);
  1486. }
  1487. HRESULT CMTNode::CloseView(int idView)
  1488. {
  1489. if (!IsStaticNode())
  1490. return S_OK;
  1491. HRESULT hr;
  1492. CMTNode* const pChild = m_pChild->NextStaticNode();
  1493. if (pChild)
  1494. {
  1495. hr = pChild->CloseView(idView);
  1496. ASSERT(SUCCEEDED(hr));
  1497. if (FAILED(hr))
  1498. return E_FAIL;
  1499. }
  1500. CMTNode* const pNext = m_pNext->NextStaticNode();
  1501. if (pNext)
  1502. {
  1503. hr = pNext->CloseView(idView);
  1504. ASSERT(SUCCEEDED(hr));
  1505. if (FAILED(hr))
  1506. return E_FAIL;
  1507. }
  1508. return S_OK;
  1509. }
  1510. HRESULT CMTNode::DeleteView(int idView)
  1511. {
  1512. if (!IsStaticNode())
  1513. return S_OK;
  1514. HRESULT hr;
  1515. CMTNode* const pChild = m_pChild->NextStaticNode();
  1516. if (pChild)
  1517. {
  1518. hr = pChild->DeleteView(idView);
  1519. ASSERT(SUCCEEDED(hr));
  1520. if (FAILED(hr))
  1521. return E_FAIL;
  1522. }
  1523. CMTNode* const pNext = m_pNext->NextStaticNode();
  1524. if (pNext)
  1525. {
  1526. hr = pNext->DeleteView(idView);
  1527. ASSERT(SUCCEEDED(hr));
  1528. if (FAILED(hr))
  1529. return E_FAIL;
  1530. }
  1531. return S_OK;
  1532. }
  1533. //+-------------------------------------------------------------------
  1534. //
  1535. // Member: GetBookmark
  1536. //
  1537. // Synopsis: Get bookmark for this MTNode.
  1538. //
  1539. // Arguments: None.
  1540. //
  1541. // Returns: auto pointer to CBookmark.
  1542. //
  1543. // History: 04-23-1999 AnandhaG Created
  1544. //
  1545. //--------------------------------------------------------------------
  1546. CBookmark* CMTNode::GetBookmark()
  1547. {
  1548. DECLARE_SC(sc, TEXT("CMTNode::GetBookmark"));
  1549. // If the bookmark is not created, create one.
  1550. if (NULL == m_bookmark.get())
  1551. {
  1552. m_bookmark = std::auto_ptr<CBookmarkEx>(new CBookmarkEx);
  1553. if (NULL == m_bookmark.get())
  1554. return NULL;
  1555. m_bookmark->Reset();
  1556. SC sc = m_bookmark->ScInitialize(this, GetStaticParent(), false /*bFastRetrievalOnly*/);
  1557. if(sc)
  1558. sc.TraceAndClear(); // change
  1559. }
  1560. return m_bookmark.get();
  1561. }
  1562. void
  1563. CMTNode::SetCachedDisplayName(LPCTSTR pszName)
  1564. {
  1565. if (m_strName.str() != pszName)
  1566. {
  1567. m_strName = pszName;
  1568. SetDirty();
  1569. if (Parent())
  1570. Parent()->OnChildrenChanged();
  1571. }
  1572. }
  1573. UINT
  1574. CMTNode::GetState(void)
  1575. {
  1576. UINT nState = 0;
  1577. if (WasExpandedAtLeastOnce())
  1578. {
  1579. nState |= MMC_SCOPE_ITEM_STATE_EXPANDEDONCE;
  1580. }
  1581. return nState;
  1582. }
  1583. /*+-------------------------------------------------------------------------*
  1584. *
  1585. * CMTNode::ScLoad
  1586. *
  1587. * PURPOSE: Loads the node from the tree stream
  1588. *
  1589. * RETURNS:
  1590. * SC
  1591. *
  1592. *+-------------------------------------------------------------------------*/
  1593. SC
  1594. CMTNode::ScLoad()
  1595. {
  1596. ASSERT (IsStaticNode());
  1597. SC sc;
  1598. CStream stream;
  1599. stream.Attach(GetTreeStream());
  1600. HRESULT hr;
  1601. IStringTablePrivate* pStringTable = CScopeTree::GetStringTable();
  1602. ASSERT (pStringTable != NULL);
  1603. /*
  1604. * read the "versioned stream" marker
  1605. */
  1606. StreamVersionIndicator nVersionMarker;
  1607. sc = stream.ScRead(&nVersionMarker, sizeof(nVersionMarker));
  1608. if(sc)
  1609. goto Error;
  1610. /*
  1611. * Determine the stream version number. If this is a versioned
  1612. * stream, the version is the next DWORD in the stream, otherwise
  1613. * it must be it's a version 1 stream
  1614. */
  1615. StreamVersionIndicator nVersion;
  1616. if (nVersionMarker == VersionedStreamMarker)
  1617. {
  1618. sc = stream.ScRead(&nVersion, sizeof(nVersion));
  1619. if(sc)
  1620. goto Error;
  1621. }
  1622. else
  1623. nVersion = Stream_V0100;
  1624. switch (nVersion)
  1625. {
  1626. /*
  1627. * MMC 1.0 stream
  1628. */
  1629. case Stream_V0100:
  1630. {
  1631. /*
  1632. * Version 1 streams didn't have a version marker; they began with
  1633. * the image index as the first DWORD. The first DWORD has
  1634. * already been read (version marker), so we can recycle that
  1635. * value for the image index.
  1636. */
  1637. m_nImage = nVersionMarker;
  1638. /*
  1639. * Continue reading with the display name (length then characters)
  1640. */
  1641. unsigned int stringLength = 0;
  1642. sc = stream.ScRead(&stringLength, sizeof(stringLength));
  1643. if(sc)
  1644. goto Error;
  1645. if (stringLength)
  1646. {
  1647. wchar_t* str = reinterpret_cast<wchar_t*>(alloca((stringLength+1)*2));
  1648. ASSERT(str != NULL);
  1649. if (str == NULL)
  1650. return E_POINTER;
  1651. sc = stream.ScRead(str, stringLength*2);
  1652. if(sc)
  1653. goto Error;
  1654. str[stringLength] = 0;
  1655. USES_CONVERSION;
  1656. m_strName = W2T (str);
  1657. }
  1658. break;
  1659. }
  1660. /*
  1661. * MMC 1.1 stream
  1662. */
  1663. case Stream_V0110:
  1664. {
  1665. /*
  1666. * read the image index
  1667. */
  1668. sc = stream.ScRead(&m_nImage, sizeof(m_nImage));
  1669. if(sc)
  1670. goto Error;
  1671. /*
  1672. * read the name (stream insertion operators will throw
  1673. * _com_error's, so we need an exception block here)
  1674. */
  1675. try
  1676. {
  1677. IStream *pStream = stream.Get();
  1678. if(!pStream)
  1679. goto PointerError;
  1680. *pStream >> m_strName;
  1681. }
  1682. catch (_com_error& err)
  1683. {
  1684. hr = err.Error();
  1685. ASSERT (false && "Caught _com_error");
  1686. return (hr);
  1687. }
  1688. break;
  1689. }
  1690. default:
  1691. #ifdef DBG
  1692. TCHAR szTraceMsg[80];
  1693. wsprintf (szTraceMsg, _T("Unexpected stream version 0x08x\n"), nVersion);
  1694. TRACE (szTraceMsg);
  1695. ASSERT (FALSE);
  1696. #endif
  1697. return (E_FAIL);
  1698. break;
  1699. }
  1700. Cleanup:
  1701. return sc;
  1702. PointerError:
  1703. sc = E_POINTER;
  1704. Error:
  1705. TraceError(TEXT("CMTNode::Load"), sc);
  1706. goto Cleanup;
  1707. }
  1708. HRESULT CMTNode::Init(void)
  1709. {
  1710. DECLARE_SC(sc, TEXT("CMTNode::Init"));
  1711. if (m_bInit == TRUE)
  1712. return S_FALSE;
  1713. ASSERT(WasExpandedAtLeastOnce() == FALSE);
  1714. if (!m_pPrimaryComponentData)
  1715. return E_FAIL;
  1716. CMTSnapInNode* pMTSnapIn = GetStaticParent();
  1717. HMTNODE hMTNode = CMTNode::ToHandle(pMTSnapIn);
  1718. if (!m_pPrimaryComponentData->IsInitialized())
  1719. {
  1720. sc = m_pPrimaryComponentData->Init(hMTNode);
  1721. if(sc)
  1722. return sc.ToHr();
  1723. sc = pMTSnapIn->ScInitIComponentData(m_pPrimaryComponentData);
  1724. if (sc)
  1725. return sc.ToHr();
  1726. }
  1727. // Init the extensions
  1728. m_bInit = TRUE;
  1729. BOOL fProblem = FALSE;
  1730. // Get node's node-type
  1731. GUID guidNodeType;
  1732. sc = GetNodeType(&guidNodeType);
  1733. if (sc)
  1734. return sc.ToHr();
  1735. CExtensionsIterator it;
  1736. // TODO: try to use the easier form of it.ScInitialize()
  1737. sc = it.ScInitialize(m_pPrimaryComponentData->GetSnapIn(), guidNodeType, g_szNameSpace,
  1738. m_arrayDynExtCLSID.GetData(), m_arrayDynExtCLSID.GetSize());
  1739. if(sc)
  1740. return sc.ToHr();
  1741. else
  1742. {
  1743. CComponentData* pCCD = NULL;
  1744. for (; it.IsEnd() == FALSE; it.Advance())
  1745. {
  1746. pCCD = pMTSnapIn->GetComponentData(it.GetCLSID());
  1747. if (pCCD == NULL)
  1748. {
  1749. CSnapInPtr spSnapIn;
  1750. // If a dynamic extension, we have to get the snap-in ourselves
  1751. // otherwise the iterator has it
  1752. if (it.IsDynamic())
  1753. {
  1754. CSnapInsCache* const pCache = theApp.GetSnapInsCache();
  1755. ASSERT(pCache != NULL);
  1756. SC sc = pCache->ScGetSnapIn(it.GetCLSID(), &spSnapIn);
  1757. ASSERT(!sc.IsError());
  1758. // On failure, continue with other extensions
  1759. if (sc)
  1760. continue;
  1761. }
  1762. else
  1763. {
  1764. spSnapIn = it.GetSnapIn();
  1765. }
  1766. ASSERT(spSnapIn != NULL);
  1767. pCCD = new CComponentData(spSnapIn);
  1768. pMTSnapIn->AddComponentDataToArray(pCCD);
  1769. }
  1770. ASSERT(pCCD != NULL);
  1771. if (pCCD != NULL && pCCD->IsInitialized() == FALSE)
  1772. {
  1773. sc = pCCD->Init(hMTNode);
  1774. if ( !sc.IsError() )
  1775. sc = pMTSnapIn->ScInitIComponentData(pCCD);
  1776. if ( sc )
  1777. {
  1778. sc.TraceAndClear();
  1779. fProblem = TRUE;
  1780. }
  1781. }
  1782. }
  1783. pMTSnapIn->CompressComponentDataArray();
  1784. }
  1785. if (fProblem == TRUE)
  1786. {
  1787. Dbg(DEB_TRACE, _T("Failed to load some extensions"));
  1788. }
  1789. return S_OK;
  1790. }
  1791. HRESULT CMTNode::Expand(void)
  1792. {
  1793. DECLARE_SC(sc, TEXT("CMTNode::Expand"));
  1794. CComponentData* pCCD = m_pPrimaryComponentData;
  1795. if (WasExpandedAtLeastOnce() == FALSE)
  1796. Init();
  1797. SetExpandedAtLeastOnce();
  1798. ASSERT(pCCD != NULL);
  1799. if (pCCD == NULL)
  1800. return E_FAIL;
  1801. // Get the data object for the cookie from the owner snap-in
  1802. IDataObjectPtr spDataObject;
  1803. HRESULT hr = pCCD->QueryDataObject(GetUserParam(), CCT_SCOPE, &spDataObject);
  1804. CHECK_HRESULT(hr);
  1805. if (FAILED(hr))
  1806. return hr;
  1807. // hr = pCCD->Notify (spDataObject, MMCN_EXPAND, TRUE,
  1808. // reinterpret_cast<LPARAM>(this));
  1809. hr = Expand (pCCD, spDataObject, TRUE);
  1810. CHECK_HRESULT(hr);
  1811. if (FAILED(hr))
  1812. return hr;
  1813. // Mark the folder for the master tree item as expanded
  1814. CMTSnapInNode* pSIMTNode = GetStaticParent();
  1815. //
  1816. // Deal with extension snap-ins
  1817. //
  1818. m_bExtensionsExpanded = TRUE;
  1819. // Get node's node-type
  1820. GUID guidNodeType;
  1821. hr = GetNodeType(&guidNodeType);
  1822. if (FAILED(hr))
  1823. return hr;
  1824. CExtensionsIterator it;
  1825. // TODO: try to use the easier form of it.ScInitialize()
  1826. sc = it.ScInitialize(GetPrimarySnapIn(), guidNodeType, g_szNameSpace,
  1827. m_arrayDynExtCLSID.GetData(), m_arrayDynExtCLSID.GetSize());
  1828. if (sc)
  1829. return S_FALSE; // The snapin is not loaded on the m/c.
  1830. if (it.IsEnd()) // No extensions.
  1831. return S_OK;
  1832. BOOL fProblem = FALSE;
  1833. for (; it.IsEnd() == FALSE; it.Advance())
  1834. {
  1835. CComponentData* pCCD = pSIMTNode->GetComponentData(it.GetCLSID());
  1836. if (pCCD == NULL)
  1837. continue;
  1838. // hr = pCCD->Notify (spDataObject, MMCN_EXPAND, TRUE,
  1839. // reinterpret_cast<LPARAM>(this));
  1840. hr = Expand (pCCD, spDataObject, TRUE);
  1841. CHECK_HRESULT(hr);
  1842. // continue even if an error occurs with extension snapins
  1843. if (FAILED(hr))
  1844. fProblem = TRUE;
  1845. }
  1846. return (fProblem == TRUE) ? S_FALSE : S_OK;
  1847. }
  1848. CNode* CMTNode::GetNode(CViewData* pViewData, BOOL fRootNode)
  1849. {
  1850. CMTSnapInNode* pMTSnapInNode = GetStaticParent();
  1851. if (pMTSnapInNode == NULL)
  1852. return (NULL);
  1853. if (fRootNode)
  1854. {
  1855. /*
  1856. * create a static parent node for this non-static
  1857. * root node (it will be deleted in the CNode dtor)
  1858. */
  1859. CNode* pNodeTemp = pMTSnapInNode->GetNode(pViewData, FALSE);
  1860. if (pNodeTemp == NULL)
  1861. return NULL;
  1862. }
  1863. CNode* pNode = new CNode(this, pViewData, fRootNode);
  1864. if (pNode != NULL)
  1865. {
  1866. CComponent* pCC = pMTSnapInNode->GetComponent(pViewData->GetViewID(),
  1867. GetPrimaryComponentID(), GetPrimarySnapIn());
  1868. if (pCC==NULL)
  1869. {
  1870. delete pNode;
  1871. return NULL;
  1872. }
  1873. else
  1874. pNode->SetPrimaryComponent(pCC);
  1875. }
  1876. return pNode;
  1877. }
  1878. HRESULT CMTNode::AddExtension(LPCLSID lpclsid)
  1879. {
  1880. DECLARE_SC(sc, TEXT("CMTNode::AddExtension"));
  1881. sc = ScCheckPointers(lpclsid);
  1882. if (sc)
  1883. return sc.ToHr();
  1884. CMTSnapInNode* pMTSnapIn = GetStaticParent();
  1885. CSnapInsCache* const pCache = theApp.GetSnapInsCache();
  1886. sc = ScCheckPointers(pMTSnapIn, pCache, E_UNEXPECTED);
  1887. if (sc)
  1888. return sc.ToHr();
  1889. do // not a loop
  1890. {
  1891. // Get node's node-type
  1892. GUID guidNodeType;
  1893. sc = GetNodeType(&guidNodeType);
  1894. if (sc)
  1895. return sc.ToHr();
  1896. // Must be a namespace extension
  1897. if (!ExtendsNodeNameSpace(guidNodeType, lpclsid))
  1898. return (sc = E_INVALIDARG).ToHr();
  1899. // Check if extension is already enabled
  1900. CExtensionsIterator it;
  1901. // TODO: try to use the easier form of it.ScInitialize()
  1902. sc = it.ScInitialize(GetPrimarySnapIn(), guidNodeType, g_szNameSpace,
  1903. m_arrayDynExtCLSID.GetData(), m_arrayDynExtCLSID.GetSize());
  1904. for (; it.IsEnd() == FALSE; it.Advance())
  1905. {
  1906. if (IsEqualCLSID(*lpclsid, it.GetCLSID()))
  1907. return (sc = S_FALSE).ToHr();
  1908. }
  1909. // Add extension to dynamic list
  1910. m_arrayDynExtCLSID.Add(*lpclsid);
  1911. // No errors returned if node is not initialized in MMC1.2.
  1912. if (!m_bInit)
  1913. break;
  1914. HMTNODE hMTNode = CMTNode::ToHandle(pMTSnapIn);
  1915. CSnapInPtr spSI;
  1916. CComponentData* pCCD = pMTSnapIn->GetComponentData(*lpclsid);
  1917. if (pCCD == NULL)
  1918. {
  1919. sc = pCache->ScGetSnapIn(*lpclsid, &spSI);
  1920. if (sc)
  1921. return sc.ToHr();
  1922. pCCD = new CComponentData(spSI);
  1923. sc = ScCheckPointers(pCCD, E_OUTOFMEMORY);
  1924. if (sc)
  1925. return sc.ToHr();
  1926. pMTSnapIn->AddComponentDataToArray(pCCD);
  1927. }
  1928. sc = ScCheckPointers(pCCD, E_UNEXPECTED);
  1929. if (sc)
  1930. return sc.ToHr();
  1931. if (pCCD->IsInitialized() == FALSE)
  1932. {
  1933. sc = pCCD->Init(hMTNode);
  1934. if (sc)
  1935. {
  1936. // Init failed.
  1937. pMTSnapIn->CompressComponentDataArray();
  1938. return sc.ToHr();
  1939. }
  1940. else
  1941. {
  1942. // Above Init is successful.
  1943. sc = pMTSnapIn->ScInitIComponentData(pCCD);
  1944. sc.TraceAndClear(); // to maintain compatibility
  1945. }
  1946. }
  1947. // Create and initialize a CComponent for all initialized nodes
  1948. CNodeList& nodes = pMTSnapIn->GetNodeList();
  1949. POSITION pos = nodes.GetHeadPosition();
  1950. CNode* pNode = NULL;
  1951. while (pos)
  1952. {
  1953. pNode = nodes.GetNext(pos);
  1954. CSnapInNode* pSINode = dynamic_cast<CSnapInNode*>(pNode);
  1955. sc = ScCheckPointers(pSINode, E_UNEXPECTED);
  1956. if (sc)
  1957. {
  1958. sc.TraceAndClear();
  1959. continue;
  1960. }
  1961. // Create component if hasn't been done yet
  1962. CComponent* pCC = pSINode->GetComponent(pCCD->GetComponentID());
  1963. if (pCC == NULL)
  1964. {
  1965. // Create and initialize one
  1966. pCC = new CComponent(pCCD->GetSnapIn());
  1967. sc = ScCheckPointers(pCC, E_OUTOFMEMORY);
  1968. if (sc)
  1969. return sc.ToHr();
  1970. pCC->SetComponentID(pCCD->GetComponentID());
  1971. pSINode->AddComponentToArray(pCC);
  1972. sc = pCC->Init(pCCD->GetIComponentData(), hMTNode, CNode::ToHandle(pNode),
  1973. pCCD->GetComponentID(), pNode->GetViewID());
  1974. sc.Trace_(); // Just trace for MMC1.2 compatibility.
  1975. }
  1976. }
  1977. // if extensions are already expanded, expand the new one now
  1978. if (AreExtensionsExpanded())
  1979. {
  1980. // Get the data object for the cookie from the owner snap-in
  1981. IDataObjectPtr spDataObject;
  1982. sc = GetPrimaryComponentData()->QueryDataObject(GetUserParam(), CCT_SCOPE, &spDataObject);
  1983. if (sc)
  1984. return sc.ToHr();
  1985. // hr = pCCD->Notify (spDataObject, MMCN_EXPAND, TRUE,
  1986. // reinterpret_cast<LPARAM>(this));
  1987. sc = Expand (pCCD, spDataObject, TRUE);
  1988. if (sc)
  1989. sc.Trace_(); // Just trace for MMC1.2 compatibility.
  1990. }
  1991. }
  1992. while(0);
  1993. return sc.ToHr();
  1994. }
  1995. HRESULT CMTNode::IsExpandable()
  1996. {
  1997. DECLARE_SC(sc, TEXT("CMTNode::IsExpandable"));
  1998. // if already expanded, we know if there are children
  1999. if (WasExpandedAtLeastOnce())
  2000. return (Child() != NULL) ? S_OK : S_FALSE;
  2001. // Even if not expanded there might be static children
  2002. if (Child() != NULL)
  2003. return S_OK;
  2004. // if primary snap-in can add children, return TRUE
  2005. // Note: When primary declares no children, it is also declaring
  2006. // there will be no dynamic namespace extensions
  2007. if (!(m_usExpandFlags & FLAG_NO_CHILDREN_FROM_PRIMARY))
  2008. return S_OK;
  2009. // Check enabled static extensions if haven't already
  2010. if (!(m_usExpandFlags & FLAG_NAMESPACE_EXTNS_CHECKED))
  2011. {
  2012. m_usExpandFlags |= FLAG_NAMESPACE_EXTNS_CHECKED;
  2013. do
  2014. {
  2015. // Do quick check for no extensions first
  2016. if (GetPrimarySnapIn()->GetExtensionSnapIn() == NULL)
  2017. {
  2018. m_usExpandFlags |= FLAG_NO_NAMESPACE_EXTNS;
  2019. break;
  2020. }
  2021. // Use iterator to find statically enabled namespace extens
  2022. GUID guidNodeType;
  2023. HRESULT hr = GetNodeType(&guidNodeType);
  2024. ASSERT(SUCCEEDED(hr));
  2025. if (FAILED(hr))
  2026. break;
  2027. CExtensionsIterator it;
  2028. // TODO: try to use the easier form of it.ScInitialize()
  2029. sc = it.ScInitialize(GetPrimarySnapIn(), guidNodeType, g_szNameSpace, NULL, 0);
  2030. // if no extensions found, set the flag
  2031. if (sc.IsError() || it.IsEnd())
  2032. m_usExpandFlags |= FLAG_NO_NAMESPACE_EXTNS;
  2033. }
  2034. while (FALSE);
  2035. }
  2036. // if no namespace extensions, there will be no children
  2037. if (m_usExpandFlags & FLAG_NO_NAMESPACE_EXTNS)
  2038. return S_FALSE;
  2039. return S_OK;
  2040. }
  2041. HRESULT CMTNode::Expand (
  2042. CComponentData* pComponentData,
  2043. IDataObject* pDataObject,
  2044. BOOL bExpanding)
  2045. {
  2046. HRESULT hr = E_FAIL;
  2047. bool fSendExpand = true;
  2048. if (CScopeTree::_IsSynchronousExpansionRequired())
  2049. {
  2050. MMC_EXPANDSYNC_STRUCT ess;
  2051. ess.bHandled = FALSE;
  2052. ess.bExpanding = bExpanding;
  2053. ess.hItem = reinterpret_cast<HSCOPEITEM>(this);
  2054. hr = pComponentData->Notify (pDataObject, MMCN_EXPANDSYNC, 0,
  2055. reinterpret_cast<LPARAM>(&ess));
  2056. fSendExpand = !ess.bHandled;
  2057. }
  2058. if (fSendExpand)
  2059. {
  2060. hr = pComponentData->Notify (pDataObject, MMCN_EXPAND, bExpanding,
  2061. reinterpret_cast<LPARAM>(this));
  2062. }
  2063. return (hr);
  2064. }
  2065. SC CMTNode::ScQueryDispatch(DATA_OBJECT_TYPES type,
  2066. PPDISPATCH ppScopeNodeObject)
  2067. {
  2068. DECLARE_SC(sc, _T("CMTNode::QueryDispatch"));
  2069. sc = ScCheckPointers(ppScopeNodeObject);
  2070. if (sc)
  2071. return sc;
  2072. *ppScopeNodeObject = NULL;
  2073. CMTSnapInNode* pMTSINode = GetStaticParent();
  2074. sc = ScCheckPointers(pMTSINode, E_UNEXPECTED);
  2075. if (sc)
  2076. return sc;
  2077. CComponentData* pCCD = pMTSINode->GetComponentData(GetPrimarySnapInCLSID());
  2078. sc = ScCheckPointers(pCCD, E_UNEXPECTED);
  2079. if (sc)
  2080. return sc;
  2081. sc = pCCD->ScQueryDispatch(GetUserParam(), type, ppScopeNodeObject);
  2082. return sc;
  2083. }
  2084. /*+-------------------------------------------------------------------------*
  2085. * CMTNode::SetDisplayName
  2086. *
  2087. *
  2088. *--------------------------------------------------------------------------*/
  2089. void CMTNode::SetDisplayName (LPCTSTR pszName)
  2090. {
  2091. // This function should never be called as it does nothing. Display names
  2092. DECLARE_SC(sc, TEXT("CMTNode::SetDisplayName"));
  2093. if (pszName != (LPCTSTR) MMC_TEXTCALLBACK)
  2094. {
  2095. sc = E_INVALIDARG;
  2096. TraceError(TEXT("The string should be MMC_TEXTCALLBACK"), sc);
  2097. sc.Clear();
  2098. }
  2099. }
  2100. /*+-------------------------------------------------------------------------*
  2101. *
  2102. * CMTNode::GetDisplayName
  2103. *
  2104. * PURPOSE: Returns the display name of the node.
  2105. *
  2106. * RETURNS:
  2107. * LPCTSTR
  2108. *
  2109. *+-------------------------------------------------------------------------*/
  2110. tstring
  2111. CMTNode::GetDisplayName()
  2112. {
  2113. CComponentData* pCCD = GetPrimaryComponentData();
  2114. if (pCCD)
  2115. {
  2116. SCOPEDATAITEM ScopeDataItem;
  2117. ZeroMemory(&ScopeDataItem, sizeof(ScopeDataItem));
  2118. ScopeDataItem.mask = SDI_STR;
  2119. ScopeDataItem.lParam = GetUserParam();
  2120. HRESULT hr = pCCD->GetDisplayInfo(&ScopeDataItem);
  2121. CHECK_HRESULT(hr);
  2122. /*
  2123. * if we succeeded, cache the name returned to us for
  2124. * persistence
  2125. */
  2126. if (SUCCEEDED(hr))
  2127. {
  2128. USES_CONVERSION;
  2129. if (ScopeDataItem.displayname)
  2130. SetCachedDisplayName(OLE2T(ScopeDataItem.displayname));
  2131. else
  2132. SetCachedDisplayName(_T(""));
  2133. }
  2134. }
  2135. return GetCachedDisplayName();
  2136. }
  2137. /***************************************************************************\
  2138. *
  2139. * METHOD: CMTNode::ScGetPropertyFromINodeProperties
  2140. *
  2141. * PURPOSE: gets SnapIn property thru INodeProperties interface
  2142. *
  2143. * PARAMETERS:
  2144. * LPDATAOBJECT pDataObject [in] - data object
  2145. * BSTR bstrPropertyName [in] - property name
  2146. * PBSTR pbstrPropertyValue [out] - property value
  2147. *
  2148. * RETURNS:
  2149. * SC - result code
  2150. *
  2151. \***************************************************************************/
  2152. SC CMTNode::ScGetPropertyFromINodeProperties(LPDATAOBJECT pDataObject, BSTR bstrPropertyName, PBSTR pbstrPropertyValue)
  2153. {
  2154. DECLARE_SC(sc, TEXT("CMTNode::ScGetPropertyFromINodeProperties"));
  2155. SC sc_no_trace; // for 'valid' error - not to be traced
  2156. // parameter check
  2157. sc = ScCheckPointers(pDataObject, bstrPropertyName, pbstrPropertyValue);
  2158. if(sc)
  2159. return sc;
  2160. // get the CComponentData
  2161. CComponentData *pComponentData = GetPrimaryComponentData();
  2162. sc = ScCheckPointers(pComponentData, E_UNEXPECTED);
  2163. if(sc)
  2164. return sc;
  2165. // QI for INodeProperties from IComponentData
  2166. INodePropertiesPtr spNodeProperties = pComponentData->GetIComponentData();
  2167. // at this point we should have a valid interface if it is supported
  2168. sc_no_trace = ScCheckPointers(spNodeProperties, E_NOINTERFACE);
  2169. if(sc_no_trace)
  2170. return sc_no_trace;
  2171. // get the property
  2172. sc_no_trace = spNodeProperties->GetProperty(pDataObject, bstrPropertyName, pbstrPropertyValue);
  2173. return sc_no_trace;
  2174. }
  2175. //############################################################################
  2176. //############################################################################
  2177. //
  2178. // Implementation of class CComponentData
  2179. //
  2180. //############################################################################
  2181. //############################################################################
  2182. //____________________________________________________________________________
  2183. //
  2184. // Class: CComponentData Inlines
  2185. //____________________________________________________________________________
  2186. //
  2187. DEBUG_DECLARE_INSTANCE_COUNTER(CComponentData);
  2188. CComponentData::CComponentData(CSnapIn * pSnapIn)
  2189. : m_spSnapIn(pSnapIn), m_ComponentID(-1), m_bIComponentDataInitialized(false)
  2190. {
  2191. TRACE_CONSTRUCTOR(CComponentData);
  2192. DEBUG_INCREMENT_INSTANCE_COUNTER(CComponentData);
  2193. ASSERT(m_spSnapIn != NULL);
  2194. }
  2195. CComponentData::~CComponentData()
  2196. {
  2197. TRACE_DESTRUCTOR(CComponentData);
  2198. DEBUG_DECREMENT_INSTANCE_COUNTER(CComponentData);
  2199. if (m_spIComponentData != NULL)
  2200. m_spIComponentData->Destroy();
  2201. }
  2202. HRESULT CComponentData::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  2203. {
  2204. ASSERT(m_spIComponentData != NULL);
  2205. if (m_spIComponentData == NULL)
  2206. return E_FAIL;
  2207. HRESULT hr = S_OK;
  2208. __try
  2209. {
  2210. hr = m_spIComponentData->Notify(lpDataObject, event, arg, param);
  2211. }
  2212. __except (EXCEPTION_EXECUTE_HANDLER)
  2213. {
  2214. hr = E_FAIL;
  2215. if (m_spSnapIn)
  2216. TraceSnapinException(m_spSnapIn->GetSnapInCLSID(), TEXT("IComponentData::Notify"), event);
  2217. }
  2218. return hr;
  2219. }
  2220. SC CComponentData::ScQueryDispatch(MMC_COOKIE cookie,
  2221. DATA_OBJECT_TYPES type,
  2222. PPDISPATCH ppScopeNodeObject)
  2223. {
  2224. DECLARE_SC(sc, _T("CComponentData::ScQueryDispatch"));
  2225. sc = ScCheckPointers(m_spIComponentData, E_UNEXPECTED);
  2226. if (sc)
  2227. return sc;
  2228. IComponentData2Ptr spCompData2 = m_spIComponentData;
  2229. sc = ScCheckPointers(spCompData2.GetInterfacePtr(), E_NOINTERFACE);
  2230. if (sc)
  2231. return sc;
  2232. ASSERT(type != CCT_RESULT); // Cant Ask Disp for resultpane objects.
  2233. sc = spCompData2->QueryDispatch(cookie, type, ppScopeNodeObject);
  2234. return sc;
  2235. }
  2236. /*+-------------------------------------------------------------------------*
  2237. *
  2238. * CreateSnapIn
  2239. *
  2240. * PURPOSE: Create a name space snapin (standalone or extension).
  2241. *
  2242. * PARAMETERS:
  2243. * clsid - class id of the snapin to be created.
  2244. * ppICD - IComponentData ptr of created snapin.
  2245. * fCreateDummyOnFailure - Create dummy snapin if Create snapin fails.
  2246. *
  2247. * RETURNS:
  2248. * HRESULT
  2249. *
  2250. *+-------------------------------------------------------------------------*/
  2251. HRESULT CreateSnapIn (const CLSID& clsid, IComponentData** ppICD,
  2252. bool fCreateDummyOnFailure /* =true */)
  2253. {
  2254. DECLARE_SC(sc, TEXT("CreateSnapIn"));
  2255. EDummyCreateReason eReason = eSnapCreateFailed;
  2256. IComponentDataPtr spICD;
  2257. sc = ScCheckPointers(ppICD);
  2258. if(sc)
  2259. return sc.ToHr();
  2260. // initialize the out parameter
  2261. *ppICD = NULL;
  2262. CPolicy policy;
  2263. sc = policy.ScInit();
  2264. if (sc)
  2265. {
  2266. eReason = eSnapPolicyFailed;
  2267. }
  2268. else if (policy.IsPermittedSnapIn(clsid))
  2269. {
  2270. /*
  2271. * Bug 258270: creating the snap-in might result in MSI running to
  2272. * install it. The MSI status window is modeless, but may spawn a
  2273. * modal dialog. If we don't manually disable MMC's main window,
  2274. * the user might start clicking around in the scope tree while that
  2275. * modal dialog is up, leading to reentrancy and all of the resulting
  2276. * calamity that one would expect.
  2277. */
  2278. bool fReenableMMC = false;
  2279. CScopeTree* pScopeTree = CScopeTree::GetScopeTree();
  2280. HWND hwndMain = (pScopeTree) ? pScopeTree->GetMainWindow() : NULL;
  2281. if (IsWindow (hwndMain))
  2282. {
  2283. fReenableMMC = IsWindowEnabled (hwndMain);
  2284. if (fReenableMMC)
  2285. EnableWindow (hwndMain, false);
  2286. }
  2287. //create the snapin
  2288. sc = spICD.CreateInstance(clsid, NULL,MMC_CLSCTX_INPROC);
  2289. if(!sc.IsError() && (spICD==NULL))
  2290. sc = E_NOINTERFACE;
  2291. /*
  2292. * re-enable the main window if we disabled it
  2293. */
  2294. if (fReenableMMC)
  2295. EnableWindow (hwndMain, true);
  2296. if (sc)
  2297. {
  2298. ReportSnapinInitFailure(clsid);
  2299. // Create a dummy snapin with snapin
  2300. // creation failed message.
  2301. eReason = eSnapCreateFailed;
  2302. }
  2303. else // creation succeeded. return
  2304. {
  2305. *ppICD = spICD.Detach();
  2306. return sc.ToHr();
  2307. }
  2308. }
  2309. else
  2310. {
  2311. // Display a message that policies does not
  2312. // allow this snapin to be created.
  2313. DisplayPolicyErrorMessage(clsid, FALSE);
  2314. // Create a dummy snapin with policy
  2315. // restriction message.
  2316. sc = E_FAIL;
  2317. eReason = eSnapPolicyFailed;
  2318. }
  2319. // If we've reached here, an error occurred
  2320. // create dummy snap-in that only displays error message
  2321. if (fCreateDummyOnFailure)
  2322. {
  2323. sc = ScCreateDummySnapin (&spICD, eReason, clsid);
  2324. if(sc)
  2325. return sc.ToHr();
  2326. sc = ScCheckPointers(spICD, E_UNEXPECTED);
  2327. if(sc)
  2328. return sc.ToHr();
  2329. *ppICD = spICD.Detach();
  2330. }
  2331. return sc.ToHr();
  2332. }
  2333. CExtSI* AddExtension(CSnapIn* pSnapIn, CLSID& rclsid, CSnapInsCache* pCache)
  2334. {
  2335. ASSERT(pSnapIn != NULL);
  2336. // See if extension is already present
  2337. CExtSI* pExt = pSnapIn->FindExtension(rclsid);
  2338. // if not, create one
  2339. if (pExt == NULL)
  2340. {
  2341. // Create cache entry for extension snapin
  2342. if (pCache == NULL)
  2343. pCache = theApp.GetSnapInsCache();
  2344. ASSERT(pCache != NULL);
  2345. CSnapInPtr spExtSnapIn;
  2346. SC sc = pCache->ScGetSnapIn(rclsid, &spExtSnapIn);
  2347. ASSERT(!sc.IsError() && spExtSnapIn != NULL);
  2348. // Attach extension to snap-in
  2349. if (!sc.IsError())
  2350. pExt = pSnapIn->AddExtension(spExtSnapIn);
  2351. }
  2352. else
  2353. {
  2354. // Clear deletion flag
  2355. pExt->MarkDeleted(FALSE);
  2356. }
  2357. return pExt;
  2358. }
  2359. HRESULT LoadRequiredExtensions (
  2360. CSnapIn* pSnapIn,
  2361. IComponentData* pICD,
  2362. CSnapInsCache* pCache /*=NULL*/)
  2363. {
  2364. SC sc;
  2365. ASSERT(pSnapIn != NULL);
  2366. // if already loaded, just return
  2367. if (pSnapIn->RequiredExtensionsLoaded())
  2368. goto Cleanup;
  2369. do
  2370. {
  2371. // Set extensions loaded, so we don't try again
  2372. pSnapIn->SetRequiredExtensionsLoaded();
  2373. // if snapin was enabling all extensions
  2374. // clear the flags before asking again
  2375. if (pSnapIn->DoesSnapInEnableAll())
  2376. {
  2377. pSnapIn->SetSnapInEnablesAll(FALSE);
  2378. pSnapIn->SetAllExtensionsEnabled(FALSE);
  2379. }
  2380. // Mark all required extensions for deletion
  2381. CExtSI* pExt = pSnapIn->GetExtensionSnapIn();
  2382. while (pExt != NULL)
  2383. {
  2384. if (pExt->IsRequired())
  2385. pExt->MarkDeleted(TRUE);
  2386. pExt = pExt->Next();
  2387. }
  2388. // Check for interface
  2389. IRequiredExtensionsPtr spReqExtn = pICD;
  2390. // if snap-in wants all extensions enabled
  2391. if (spReqExtn != NULL && spReqExtn->EnableAllExtensions() == S_OK)
  2392. {
  2393. // Set the "enable all" flags
  2394. pSnapIn->SetSnapInEnablesAll(TRUE);
  2395. pSnapIn->SetAllExtensionsEnabled(TRUE);
  2396. }
  2397. // if either user or snap-in wants all extensions
  2398. if (pSnapIn->AreAllExtensionsEnabled())
  2399. {
  2400. // Get list of all extensions
  2401. CExtensionsCache ExtCache;
  2402. sc = MMCGetExtensionsForSnapIn(pSnapIn->GetSnapInCLSID(), ExtCache);
  2403. if (sc)
  2404. goto Cleanup;
  2405. // Add each extension to snap-in's extension list
  2406. CExtensionsCacheIterator ExtIter(ExtCache);
  2407. for (; ExtIter.IsEnd() == FALSE; ExtIter.Advance())
  2408. {
  2409. // Only add extensions that can be statically enabled
  2410. if ((ExtIter.GetValue() & CExtSI::EXT_TYPE_STATIC) == 0)
  2411. continue;
  2412. GUID clsid = ExtIter.GetKey();
  2413. CExtSI* pExt = AddExtension(pSnapIn, clsid, pCache);
  2414. // Mark required if enabled by the snap-in
  2415. if (pExt != NULL && pSnapIn->DoesSnapInEnableAll())
  2416. pExt->SetRequired();
  2417. }
  2418. }
  2419. CPolicy policy;
  2420. sc = policy.ScInit();
  2421. if (sc)
  2422. goto Error;
  2423. // if snap-in supports the interface and didn't enable all
  2424. // ask for specific required extensions
  2425. // Note: this is done even if the user has enabled all because
  2426. // we need to know which ones the snap-in requires
  2427. if (spReqExtn != NULL && !pSnapIn->DoesSnapInEnableAll())
  2428. {
  2429. CLSID clsid;
  2430. sc = spReqExtn->GetFirstExtension(&clsid);
  2431. // Do while snap-in provides extension CLSIDs
  2432. while (HrFromSc(sc) == S_OK)
  2433. {
  2434. // See if the extension is restricted by policy.
  2435. // If so display a message.
  2436. if (! policy.IsPermittedSnapIn(clsid))
  2437. DisplayPolicyErrorMessage(clsid, TRUE);
  2438. // Add as required extension
  2439. CExtSI* pExt = AddExtension(pSnapIn, clsid, pCache);
  2440. if (pExt != NULL)
  2441. pExt->SetRequired();
  2442. sc = spReqExtn->GetNextExtension(&clsid);
  2443. }
  2444. }
  2445. // Delete extensions that are no longer required
  2446. // Note: Because required extensions are updated when snap-in is first loaded
  2447. // we don't have to worry about adding/deleting any nodes now.
  2448. pSnapIn->PurgeExtensions();
  2449. } while (FALSE);
  2450. Cleanup:
  2451. return HrFromSc(sc);
  2452. Error:
  2453. TraceError(TEXT("LoadRequiredExtensions"), sc);
  2454. goto Cleanup;
  2455. }
  2456. HRESULT CComponentData::Init(HMTNODE hMTNode)
  2457. {
  2458. ASSERT(hMTNode != 0);
  2459. if (IsInitialized() == TRUE)
  2460. return S_OK;
  2461. ASSERT(m_spSnapIn != NULL);
  2462. HRESULT hr = S_OK;
  2463. do
  2464. {
  2465. if (m_spIComponentData == NULL)
  2466. {
  2467. if (m_spSnapIn == NULL)
  2468. {
  2469. hr = E_POINTER;
  2470. break;
  2471. }
  2472. IUnknownPtr spUnknown;
  2473. hr = CreateSnapIn(m_spSnapIn->GetSnapInCLSID(), &m_spIComponentData);
  2474. ASSERT(SUCCEEDED(hr));
  2475. ASSERT(m_spIComponentData != NULL);
  2476. if (FAILED(hr))
  2477. break;
  2478. if(m_spIComponentData == NULL)
  2479. {
  2480. hr = E_FAIL;
  2481. break;
  2482. }
  2483. }
  2484. hr = m_spIFramePrivate.CreateInstance(CLSID_NodeInit,
  2485. #if _MSC_VER >= 1100
  2486. NULL,
  2487. #endif
  2488. MMC_CLSCTX_INPROC);
  2489. CHECK_HRESULT(hr);
  2490. BREAK_ON_FAIL(hr);
  2491. Debug_SetNodeInitSnapinName(m_spSnapIn, m_spIFramePrivate.GetInterfacePtr());
  2492. // Init frame.
  2493. ASSERT(m_ComponentID != -1);
  2494. ASSERT(m_spIFramePrivate != NULL);
  2495. ASSERT(m_spSnapIn != NULL);
  2496. if ((m_spIFramePrivate == NULL) || (m_spSnapIn == NULL))
  2497. {
  2498. hr = E_UNEXPECTED;
  2499. CHECK_HRESULT(hr);
  2500. break;
  2501. }
  2502. m_spIFramePrivate->SetComponentID(m_ComponentID);
  2503. m_spIFramePrivate->CreateScopeImageList(m_spSnapIn->GetSnapInCLSID());
  2504. m_spIFramePrivate->SetNode(hMTNode, NULL);
  2505. // Load extensions requested by snap-in and proceed regardless of outcome
  2506. LoadRequiredExtensions(m_spSnapIn, m_spIComponentData);
  2507. hr = m_spIComponentData->Initialize(m_spIFramePrivate);
  2508. CHECK_HRESULT(hr);
  2509. BREAK_ON_FAIL(hr);
  2510. } while (0);
  2511. if (FAILED(hr))
  2512. {
  2513. m_spIComponentData = NULL;
  2514. m_spIFramePrivate = NULL;
  2515. }
  2516. return hr;
  2517. }
  2518. //############################################################################
  2519. //############################################################################
  2520. //
  2521. // Implementation of class CMTSnapInNode
  2522. //
  2523. //############################################################################
  2524. //############################################################################
  2525. DEBUG_DECLARE_INSTANCE_COUNTER(CMTSnapInNode);
  2526. CMTSnapInNode::CMTSnapInNode(Properties* pProps)
  2527. : m_spProps (pProps),
  2528. m_fCallbackForDisplayName(false)
  2529. {
  2530. DEBUG_INCREMENT_INSTANCE_COUNTER(CMTSnapInNode);
  2531. // Open and Closed images
  2532. SetImage(eStockImage_Folder);
  2533. SetOpenImage(eStockImage_OpenFolder);
  2534. m_ePreloadState = ePreload_Unknown;
  2535. m_bHasBitmaps = FALSE;
  2536. m_resultImage = CMTNode::GetImage();
  2537. /*
  2538. * attach this node to it's properties collection
  2539. */
  2540. if (m_spProps != NULL)
  2541. {
  2542. CSnapinProperties* pSIProps = CSnapinProperties::FromInterface (m_spProps);
  2543. if (pSIProps != NULL)
  2544. pSIProps->ScSetSnapInNode (this);
  2545. }
  2546. }
  2547. CMTSnapInNode::~CMTSnapInNode() throw()
  2548. {
  2549. DEBUG_DECREMENT_INSTANCE_COUNTER(CMTSnapInNode);
  2550. for (int i=0; i < m_ComponentDataArray.size(); i++)
  2551. delete m_ComponentDataArray[i];
  2552. // DON'T CHANGE THIS ORDER!!!!!
  2553. m_ComponentStorage.Clear();
  2554. /*
  2555. * detach this node from it's properties collection
  2556. */
  2557. if (m_spProps != NULL)
  2558. {
  2559. CSnapinProperties* pSIProps = CSnapinProperties::FromInterface (m_spProps);
  2560. if (pSIProps != NULL)
  2561. pSIProps->ScSetSnapInNode (NULL);
  2562. }
  2563. /*
  2564. * clean up the image lists (they aren't self-cleaning!)
  2565. */
  2566. m_imlSmall.Destroy();
  2567. m_imlLarge.Destroy();
  2568. }
  2569. HRESULT CMTSnapInNode::Init(void)
  2570. {
  2571. DECLARE_SC (sc, _T("CMTSnapInNode::Init"));
  2572. if (IsInitialized() == TRUE)
  2573. return S_FALSE;
  2574. HRESULT hr = CMTNode::Init();
  2575. if (FAILED(hr))
  2576. return hr;
  2577. /*
  2578. * initialize the snap-in with its properties interface
  2579. */
  2580. sc = ScInitProperties ();
  2581. if (sc)
  2582. return (sc.ToHr());
  2583. if (IsPreloadRequired())
  2584. {
  2585. CComponentData* pCCD = GetPrimaryComponentData();
  2586. ASSERT(pCCD != NULL);
  2587. IDataObjectPtr spDataObject;
  2588. hr = pCCD->QueryDataObject(GetUserParam(), CCT_SCOPE, &spDataObject);
  2589. ASSERT(SUCCEEDED(hr));
  2590. if (FAILED(hr))
  2591. return hr;
  2592. HSCOPEITEM hsi = reinterpret_cast<HSCOPEITEM>(this);
  2593. pCCD->Notify(spDataObject, MMCN_PRELOAD, hsi, 0);
  2594. }
  2595. return S_OK;
  2596. }
  2597. /*+-------------------------------------------------------------------------*
  2598. * CMTSnapInNode::ScInitProperties
  2599. *
  2600. * Initializes the snap-in with its properties interface, if it supports
  2601. * ISnapinProperties.
  2602. *--------------------------------------------------------------------------*/
  2603. SC CMTSnapInNode::ScInitProperties ()
  2604. {
  2605. DECLARE_SC (sc, _T("CMTSnapInNode::ScInitProperties"));
  2606. /*
  2607. * get the snap-in's IComponentData
  2608. */
  2609. CComponentData* pCCD = GetPrimaryComponentData();
  2610. if (pCCD == NULL)
  2611. return (sc = E_UNEXPECTED);
  2612. IComponentDataPtr spComponentData = pCCD->GetIComponentData();
  2613. if (spComponentData == NULL)
  2614. return (sc = E_UNEXPECTED);
  2615. /*
  2616. * If the snap-in supports ISnapinProperties, give it its Properties
  2617. * interface.
  2618. */
  2619. ISnapinPropertiesPtr spISP = spComponentData;
  2620. if (spISP != NULL)
  2621. {
  2622. /*
  2623. * If we didn't persist properties for this snap-in we won't have
  2624. * a CSnapinProperties object yet; create one now.
  2625. */
  2626. CSnapinProperties* pSIProps = NULL;
  2627. sc = ScCreateSnapinProperties (&pSIProps);
  2628. if (sc)
  2629. return (sc);
  2630. if (pSIProps == NULL)
  2631. return (sc = E_UNEXPECTED);
  2632. /*
  2633. * Initialize the snap-in with the initial properties.
  2634. */
  2635. sc = pSIProps->ScInitialize (spISP, pSIProps, this);
  2636. if (sc)
  2637. return (sc);
  2638. }
  2639. return (sc);
  2640. }
  2641. /*+-------------------------------------------------------------------------*
  2642. * CMTSnapInNode::ScCreateSnapinProperties
  2643. *
  2644. * Creates the CSnapinProperties object for this node. It is safe to call
  2645. * this method multiple times; subsequent invocations will short out.
  2646. *--------------------------------------------------------------------------*/
  2647. SC CMTSnapInNode::ScCreateSnapinProperties (
  2648. CSnapinProperties** ppSIProps) /* O:pointer to the CSnapinProperties object (optional) */
  2649. {
  2650. DECLARE_SC (sc, _T("CMTSnapInNode::ScCreateSnapinProperties"));
  2651. /*
  2652. * create a CSnapinProperties if we don't already have one
  2653. */
  2654. if (m_spProps == NULL)
  2655. {
  2656. /*
  2657. * create the properties object
  2658. */
  2659. CComObject<CSnapinProperties>* pSIProps;
  2660. sc = CComObject<CSnapinProperties>::CreateInstance (&pSIProps);
  2661. if (sc)
  2662. return (sc);
  2663. if (pSIProps == NULL)
  2664. return (sc = E_UNEXPECTED);
  2665. /*
  2666. * keep a reference to the object
  2667. */
  2668. m_spProps = pSIProps;
  2669. }
  2670. /*
  2671. * return a pointer to the implementing object, if desired
  2672. */
  2673. if (ppSIProps != NULL)
  2674. *ppSIProps = CSnapinProperties::FromInterface (m_spProps);
  2675. return (sc);
  2676. }
  2677. /*+-------------------------------------------------------------------------*
  2678. *
  2679. * CMTSnapInNode::SetDisplayName
  2680. *
  2681. * PURPOSE: Sets the display name of the node.
  2682. *
  2683. * PARAMETERS:
  2684. * LPCTSTR pszName :
  2685. *
  2686. * RETURNS:
  2687. * void
  2688. *
  2689. *+-------------------------------------------------------------------------*/
  2690. void
  2691. CMTSnapInNode::SetDisplayName(LPCTSTR pszName)
  2692. {
  2693. bool fDisplayCallback = (pszName == (LPCTSTR)MMC_TEXTCALLBACK);
  2694. /*
  2695. * if our callback setting has changed, we're dirty
  2696. */
  2697. if (m_fCallbackForDisplayName != fDisplayCallback)
  2698. {
  2699. m_fCallbackForDisplayName = fDisplayCallback;
  2700. SetDirty();
  2701. }
  2702. /*
  2703. * if we're not now callback, cache the name (if we're callback,
  2704. * the name will be cached the next time GetDisplayName is called)
  2705. */
  2706. if (!m_fCallbackForDisplayName)
  2707. SetCachedDisplayName(pszName);
  2708. }
  2709. /*+-------------------------------------------------------------------------*
  2710. *
  2711. * CMTSnapInNode::GetDisplayName
  2712. *
  2713. * PURPOSE: Returns the display name of the node.
  2714. *
  2715. * RETURNS:
  2716. * LPCTSTR
  2717. *
  2718. *+-------------------------------------------------------------------------*/
  2719. tstring
  2720. CMTSnapInNode::GetDisplayName()
  2721. {
  2722. if (m_fCallbackForDisplayName)
  2723. return (CMTNode::GetDisplayName());
  2724. return GetCachedDisplayName();
  2725. }
  2726. HRESULT CMTSnapInNode::IsExpandable()
  2727. {
  2728. // if haven't intiailized the snap-in we have to assume that
  2729. // there could be children
  2730. if (!IsInitialized())
  2731. return S_OK;
  2732. return CMTNode::IsExpandable();
  2733. }
  2734. void CMTSnapInNode::CompressComponentDataArray()
  2735. {
  2736. int nSize = m_ComponentDataArray.size();
  2737. int nSkipped = 0;
  2738. for (int i=0; i<nSize; ++i)
  2739. {
  2740. ASSERT(m_ComponentDataArray[i] != NULL);
  2741. if (m_ComponentDataArray[i]->IsInitialized() == FALSE)
  2742. {
  2743. // if component failed to intialize, delete it
  2744. // and skip over it
  2745. delete m_ComponentDataArray[i];
  2746. ++nSkipped;
  2747. }
  2748. else
  2749. {
  2750. // if components have been skiped, move the good component to the
  2751. // first vacant slot and adjust the component's ID
  2752. if (nSkipped)
  2753. {
  2754. m_ComponentDataArray[i-nSkipped] = m_ComponentDataArray[i];
  2755. m_ComponentDataArray[i-nSkipped]->ResetComponentID(i-nSkipped);
  2756. }
  2757. }
  2758. }
  2759. // reduce array size by number skipped
  2760. if (nSkipped)
  2761. m_ComponentDataArray.resize(nSize - nSkipped);
  2762. }
  2763. void CMTSnapInNode::AddNode(CNode * pNode)
  2764. {
  2765. #ifdef DBG
  2766. {
  2767. POSITION pos = m_NodeList.Find(pNode);
  2768. ASSERT(pos == NULL);
  2769. }
  2770. #endif
  2771. if (!FindNode(pNode->GetViewID()))
  2772. m_NodeList.AddHead(pNode);
  2773. }
  2774. void CMTSnapInNode::RemoveNode(CNode * pNode)
  2775. {
  2776. POSITION pos = m_NodeList.Find(pNode);
  2777. if (pos != NULL)
  2778. m_NodeList.RemoveAt(pos);
  2779. }
  2780. CSnapInNode* CMTSnapInNode::FindNode(int nViewID)
  2781. {
  2782. POSITION pos = m_NodeList.GetHeadPosition();
  2783. while (pos)
  2784. {
  2785. CSnapInNode* pSINode =
  2786. dynamic_cast<CSnapInNode*>(m_NodeList.GetNext(pos));
  2787. ASSERT(pSINode != NULL);
  2788. if (pSINode->GetViewID() == nViewID)
  2789. {
  2790. return pSINode;
  2791. }
  2792. }
  2793. return NULL;
  2794. }
  2795. UINT CMTSnapInNode::GetResultImage(CNode* pNode, IImageListPrivate* pResultImageList)
  2796. {
  2797. if (pResultImageList == NULL)
  2798. return GetImage();
  2799. if ((m_bHasBitmaps == FALSE) && (m_resultImage != MMC_IMAGECALLBACK))
  2800. return GetImage();
  2801. int ret = 0;
  2802. IFramePrivate* pFramePrivate = dynamic_cast<IFramePrivate*>(pResultImageList);
  2803. COMPONENTID id = 0;
  2804. pFramePrivate->GetComponentID (&id);
  2805. COMPONENTID tempID = (COMPONENTID)-GetID(); // use Ravi's negative of ID scheme
  2806. pFramePrivate->SetComponentID (tempID);
  2807. if (m_bHasBitmaps)
  2808. {
  2809. const int nResultImageIndex = 0;
  2810. /*
  2811. * if we haven't added this node's images to the result image list,
  2812. * add it now
  2813. */
  2814. if (FAILED (pResultImageList->MapRsltImage (tempID, nResultImageIndex, &ret)))
  2815. {
  2816. /*
  2817. * Extract icons from the imagelist dynamically for device independence.
  2818. * (There ought to be a way to copy images from one imagelist to
  2819. * another, but there's not. ImageList_Copy looks like it should
  2820. * work, but it only supports copying images within the same image
  2821. * list.)
  2822. */
  2823. HRESULT hr;
  2824. CSmartIcon icon;
  2825. /*
  2826. * Set our icon from the small imagelist. ImageListSetIcon
  2827. * will also set the large icon by stretching the small, but
  2828. * we'll fix that below.
  2829. */
  2830. icon.Attach (m_imlSmall.GetIcon (0));
  2831. hr = pResultImageList->ImageListSetIcon (
  2832. reinterpret_cast<PLONG_PTR>((HICON)icon),
  2833. nResultImageIndex);
  2834. if (hr == S_OK)
  2835. {
  2836. /*
  2837. * Replace the large icon that ImageListSetIcon generated
  2838. * by stretching the small icon above, with the large icon
  2839. * that was created with the correct dimensions.
  2840. */
  2841. icon.Attach (m_imlLarge.GetIcon (0));
  2842. hr = pResultImageList->ImageListSetIcon (
  2843. reinterpret_cast<PLONG_PTR>((HICON)icon),
  2844. ILSI_LARGE_ICON (nResultImageIndex));
  2845. }
  2846. if (hr == S_OK)
  2847. pResultImageList->MapRsltImage (tempID, nResultImageIndex, &ret);
  2848. }
  2849. }
  2850. else if (m_resultImage == MMC_IMAGECALLBACK)
  2851. {
  2852. // ask snapin
  2853. // first call IComponent::Notify w/ MMCN_ADD_IMAGES;
  2854. CComponent* pComponent = pNode->GetPrimaryComponent ();
  2855. if (pComponent) {
  2856. IDataObjectPtr spDataObject;
  2857. HRESULT hr = pComponent->QueryDataObject (GetUserParam(), CCT_RESULT, &spDataObject);
  2858. if (spDataObject) {
  2859. hr = pComponent->Notify (spDataObject, MMCN_ADD_IMAGES,
  2860. (LPARAM)pResultImageList, (LPARAM)this);
  2861. if (hr == S_OK) {
  2862. RESULTDATAITEM rdi;
  2863. ZeroMemory (&rdi, sizeof(rdi));
  2864. rdi.mask = SDI_IMAGE;
  2865. rdi.lParam = GetUserParam();
  2866. rdi.nImage = 0;
  2867. hr = pComponent->GetDisplayInfo (&rdi);
  2868. // map user's number to our number
  2869. pResultImageList->MapRsltImage (tempID, rdi.nImage, &ret);
  2870. }
  2871. }
  2872. }
  2873. }
  2874. pFramePrivate->SetComponentID (id); // change back
  2875. return (UINT)ret;
  2876. }
  2877. /*+-------------------------------------------------------------------------*
  2878. * CMTSnapInNode::ScHandleCustomImages
  2879. *
  2880. * Retrieves images from a snap-in's About object and delegates to the
  2881. * overload of this function to assemble the images into their appropriate
  2882. * internal state.
  2883. *--------------------------------------------------------------------------*/
  2884. SC CMTSnapInNode::ScHandleCustomImages (const CLSID& clsidSnapin)
  2885. {
  2886. DECLARE_SC (sc, _T("CMTSnapInNode::ScHandleCustomImages"));
  2887. m_bHasBitmaps = false;
  2888. /*
  2889. * open the SnapIns key
  2890. */
  2891. MMC_ATL::CRegKey keySnapins;
  2892. sc.FromWin32 (keySnapins.Open (HKEY_LOCAL_MACHINE, SNAPINS_KEY, KEY_READ));
  2893. if (sc)
  2894. return (sc);
  2895. OLECHAR szSnapinCLSID[40];
  2896. if (StringFromGUID2 (clsidSnapin, szSnapinCLSID, countof(szSnapinCLSID)) == 0)
  2897. return (sc = E_UNEXPECTED);
  2898. /*
  2899. * open the key for the requested snap-in
  2900. */
  2901. USES_CONVERSION;
  2902. MMC_ATL::CRegKey keySnapin;
  2903. sc.FromWin32 (keySnapin.Open (keySnapins, OLE2T(szSnapinCLSID), KEY_READ));
  2904. if (sc)
  2905. return (sc);
  2906. // from snapin clsid, get "about" clsid, if any.
  2907. TCHAR szAboutCLSID[40] = {0};
  2908. DWORD dwCnt = sizeof(szAboutCLSID);
  2909. sc.FromWin32 (keySnapin.QueryValue (szAboutCLSID, _T("About"), &dwCnt));
  2910. if (sc)
  2911. return (sc);
  2912. if (szAboutCLSID[0] == 0)
  2913. return (sc = E_FAIL);
  2914. // create an instance of the About object
  2915. ISnapinAboutPtr spISA;
  2916. sc = spISA.CreateInstance (T2OLE (szAboutCLSID), NULL, MMC_CLSCTX_INPROC);
  2917. if (sc)
  2918. return (sc);
  2919. sc = ScCheckPointers (spISA, E_UNEXPECTED);
  2920. if (sc)
  2921. return (sc);
  2922. // get the images
  2923. // Documentation explicitly states these images are NOT owned by
  2924. // MMC, despite the are out parameters. So we cannot release them,
  2925. // even though most snapins will leak them anyway.
  2926. // see bugs #139613 & #140637
  2927. HBITMAP hbmpSmallImage = NULL;
  2928. HBITMAP hbmpSmallImageOpen = NULL;
  2929. HBITMAP hbmpLargeImage = NULL;
  2930. COLORREF crMask;
  2931. sc = spISA->GetStaticFolderImage (&hbmpSmallImage,
  2932. &hbmpSmallImageOpen,
  2933. &hbmpLargeImage,
  2934. &crMask);
  2935. if (sc)
  2936. return (sc);
  2937. /*
  2938. * if the snap-in didn't give us a complete set of bitmaps,
  2939. * use default images but don't fail
  2940. */
  2941. if (hbmpSmallImage == NULL || hbmpSmallImageOpen == NULL || hbmpLargeImage == NULL)
  2942. return (sc);
  2943. sc = ScHandleCustomImages (hbmpSmallImage, hbmpSmallImageOpen, hbmpLargeImage, crMask);
  2944. if (sc)
  2945. return (sc);
  2946. return (sc);
  2947. }
  2948. /*+-------------------------------------------------------------------------*
  2949. * CMTSnapInNode::ScHandleCustomImages
  2950. *
  2951. * Takes custom images for this snap-in and adds them to an imagelist for
  2952. * device-independence.
  2953. *--------------------------------------------------------------------------*/
  2954. SC CMTSnapInNode::ScHandleCustomImages (
  2955. HBITMAP hbmSmall, // I:small image
  2956. HBITMAP hbmSmallOpen, // I:small open image
  2957. HBITMAP hbmLarge, // I:large image
  2958. COLORREF crMask) // I:mask color, common between all bitmaps
  2959. {
  2960. DECLARE_SC (sc, _T("CMTSnapInNode::ScHandleCustomImages"));
  2961. /*
  2962. * validate input
  2963. */
  2964. sc = ScCheckPointers (hbmSmall, hbmSmallOpen, hbmLarge);
  2965. if (sc)
  2966. return (sc);
  2967. /*
  2968. * we need to make copies of the input bitmaps because the calls to
  2969. * ImageList_AddMasked (below) messes up the background color
  2970. */
  2971. WTL::CBitmap bmpSmallCopy = CopyBitmap (hbmSmall);
  2972. if (bmpSmallCopy.IsNull())
  2973. return (sc.FromLastError());
  2974. WTL::CBitmap bmpSmallOpenCopy = CopyBitmap (hbmSmallOpen);
  2975. if (bmpSmallOpenCopy.IsNull())
  2976. return (sc.FromLastError());
  2977. WTL::CBitmap bmpLargeCopy = CopyBitmap (hbmLarge);
  2978. if (bmpLargeCopy.IsNull())
  2979. return (sc.FromLastError());
  2980. /*
  2981. * preserve the images in imagelists for device independence
  2982. */
  2983. ASSERT (m_imlSmall.IsNull());
  2984. if (!m_imlSmall.Create (16, 16, ILC_COLOR8 | ILC_MASK, 2, 1) ||
  2985. (m_imlSmall.Add (bmpSmallCopy, crMask) == -1) ||
  2986. (m_imlSmall.Add (bmpSmallOpenCopy, crMask) == -1))
  2987. {
  2988. return (sc.FromLastError());
  2989. }
  2990. ASSERT (m_imlLarge.IsNull());
  2991. if (!m_imlLarge.Create (32, 32, ILC_COLOR8 | ILC_MASK, 1, 1) ||
  2992. (m_imlLarge.Add (bmpLargeCopy, crMask) == -1))
  2993. {
  2994. return (sc.FromLastError());
  2995. }
  2996. m_bHasBitmaps = TRUE;
  2997. sc = ScAddImagesToImageList ();
  2998. if (sc)
  2999. return (sc);
  3000. return (sc);
  3001. }
  3002. void CMTSnapInNode::SetPrimarySnapIn(CSnapIn * pSI)
  3003. {
  3004. DECLARE_SC (sc, _T("CMTSnapInNode::SetPrimarySnapIn"));
  3005. ASSERT(m_ComponentDataArray.size() == 0);
  3006. CComponentData* pCCD = new CComponentData(pSI);
  3007. int nID = AddComponentDataToArray(pCCD);
  3008. ASSERT(nID == 0);
  3009. SetPrimaryComponentData(pCCD);
  3010. if (m_bHasBitmaps == FALSE) {
  3011. sc = ScHandleCustomImages (pSI->GetSnapInCLSID());
  3012. if (sc)
  3013. sc.TraceAndClear();
  3014. if (m_bHasBitmaps)
  3015. SetDirty();
  3016. }
  3017. }
  3018. /***************************************************************************\
  3019. *
  3020. * METHOD: CMTSnapInNode::ScInitIComponent
  3021. *
  3022. * PURPOSE: Either loads component (if has a stream/storage)
  3023. * or initializes with a fresh stream/storage
  3024. *
  3025. * PARAMETERS:
  3026. * CComponent* pCComponent [in] component to initialize
  3027. * int viewID [in] view id of the component
  3028. *
  3029. * RETURNS:
  3030. * SC - result code
  3031. *
  3032. \***************************************************************************/
  3033. SC CMTSnapInNode::ScInitIComponent(CComponent* pCComponent, int viewID)
  3034. {
  3035. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScInitIComponent"));
  3036. // parameter chack
  3037. sc = ScCheckPointers( pCComponent );
  3038. if (sc)
  3039. return sc;
  3040. IComponent* pComponent = pCComponent->GetIComponent();
  3041. sc = ScCheckPointers( pComponent, E_UNEXPECTED );
  3042. if (sc)
  3043. return sc;
  3044. CLSID clsid = pCComponent->GetCLSID();
  3045. // initialize the snapin object
  3046. sc = ScInitComponentOrComponentData(pComponent, &m_ComponentPersistor, viewID, clsid );
  3047. if (sc)
  3048. return sc;
  3049. pCComponent->SetIComponentInitialized();
  3050. return sc;
  3051. }
  3052. /***************************************************************************\
  3053. *
  3054. * METHOD: CMTSnapInNode::ScInitIComponentData
  3055. *
  3056. * PURPOSE: Either loads component data (if has a stream/storage)
  3057. * or initializes with a fresh stream/storage
  3058. *
  3059. * PARAMETERS:
  3060. * CComponentData* pCComponentData
  3061. *
  3062. * RETURNS:
  3063. * SC - result code
  3064. *
  3065. \***************************************************************************/
  3066. SC CMTSnapInNode::ScInitIComponentData(CComponentData* pCComponentData)
  3067. {
  3068. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScInitIComponentData"));
  3069. // parameter check
  3070. sc = ScCheckPointers( pCComponentData );
  3071. if (sc)
  3072. return sc;
  3073. // Get the IComponentData to later obtain IPersist* from
  3074. IComponentData* const pIComponentData = pCComponentData->GetIComponentData();
  3075. sc = ScCheckPointers( pIComponentData, E_UNEXPECTED );
  3076. if (sc)
  3077. return sc;
  3078. const CLSID& clsid = pCComponentData->GetCLSID();
  3079. // initialize the snapin object
  3080. sc = ScInitComponentOrComponentData(pIComponentData, &m_CDPersistor, CDPersistor::VIEW_ID_DOCUMENT, clsid );
  3081. if (sc)
  3082. return sc;
  3083. pCComponentData->SetIComponentDataInitialized();
  3084. return sc;
  3085. }
  3086. /***************************************************************************\
  3087. *
  3088. * METHOD: CMTSnapInNode::ScInitComponentOrComponentData
  3089. *
  3090. * PURPOSE: Either loads snapin object (component or component data)
  3091. * or initializes with a fresh stream/storage
  3092. *
  3093. * PARAMETERS:
  3094. * IUnknown *pSnapin [in] - snapin to initialize
  3095. * CMTSnapinNodeStreamsAndStorages *pStreamsAndStorages
  3096. * [in] - collection of streams/storages
  3097. * int idView [in] - view id of component
  3098. * const CLSID& clsid [in] class is of the snapin
  3099. *
  3100. * RETURNS:
  3101. * SC - result code
  3102. *
  3103. \***************************************************************************/
  3104. SC CMTSnapInNode::ScInitComponentOrComponentData(IUnknown *pSnapin, CMTSnapinNodeStreamsAndStorages *pStreamsAndStorages, int idView, const CLSID& clsid )
  3105. {
  3106. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScInitComponentOrComponentData"));
  3107. // parameter check
  3108. sc = ScCheckPointers( pSnapin, pStreamsAndStorages );
  3109. if (sc)
  3110. return sc;
  3111. IPersistStreamPtr spIPersistStream;
  3112. IPersistStreamInitPtr spIPersistStreamInit;
  3113. IPersistStoragePtr spIPersistStorage;
  3114. // determine the interface supported and load/init
  3115. if ( (spIPersistStream = pSnapin) != NULL) // QI first for an IPersistStream
  3116. {
  3117. if ( pStreamsAndStorages->HasStream( idView, clsid ) )
  3118. {
  3119. // load
  3120. IStreamPtr spStream;
  3121. sc = pStreamsAndStorages->ScGetIStream( idView, clsid, &spStream);
  3122. if (sc)
  3123. return sc;
  3124. sc = spIPersistStream->Load( spStream );
  3125. if(sc)
  3126. return sc;
  3127. }
  3128. // for this interface there in no initialization if we have nothing to load from
  3129. }
  3130. else if ( (spIPersistStreamInit = pSnapin) != NULL) // QI for an IPersistStreamInit
  3131. {
  3132. if ( pStreamsAndStorages->HasStream( idView, clsid ) )
  3133. {
  3134. // load
  3135. IStreamPtr spStream;
  3136. sc = pStreamsAndStorages->ScGetIStream( idView, clsid, &spStream);
  3137. if (sc)
  3138. return sc;
  3139. sc = spIPersistStreamInit->Load( spStream );
  3140. if(sc)
  3141. return sc;
  3142. }
  3143. else
  3144. {
  3145. // init new
  3146. sc = spIPersistStreamInit->InitNew();
  3147. if (sc)
  3148. return sc;
  3149. }
  3150. }
  3151. else if ( (spIPersistStorage = pSnapin) != NULL) // QI for an IPersistStorage
  3152. {
  3153. bool bHasStorage = pStreamsAndStorages->HasStorage( idView, clsid );
  3154. IStoragePtr spStorage;
  3155. sc = pStreamsAndStorages->ScGetIStorage( idView, clsid, &spStorage);
  3156. if (sc)
  3157. return sc;
  3158. if ( bHasStorage )
  3159. {
  3160. sc = spIPersistStorage->Load( spStorage );
  3161. if (sc)
  3162. return sc;
  3163. }
  3164. else
  3165. {
  3166. sc = spIPersistStorage->InitNew( spStorage );
  3167. if (sc)
  3168. return sc;
  3169. }
  3170. }
  3171. return sc;
  3172. }
  3173. /**************************************************************************
  3174. // CMTSnapinNode::CloseView
  3175. //
  3176. // This method does any clean-up that is required before deleting
  3177. // a view. For now all we do is close any OCXs assocoiated with the view.
  3178. // This is done so the OCX can close before the view is hidden.
  3179. ***************************************************************************/
  3180. HRESULT CMTSnapInNode::CloseView(int idView)
  3181. {
  3182. // Locate associated node in specified view
  3183. CNodeList& nodes = GetNodeList();
  3184. ASSERT(&nodes != NULL);
  3185. if (&nodes == NULL)
  3186. return E_FAIL;
  3187. POSITION pos = nodes.GetHeadPosition();
  3188. while (pos)
  3189. {
  3190. CNode* pNode = nodes.GetNext(pos);
  3191. ASSERT(pNode != NULL);
  3192. if (pNode == NULL)
  3193. continue;
  3194. // if match found, tell node to close its controls
  3195. if (pNode->GetViewID() == idView)
  3196. {
  3197. CSnapInNode* pSINode = dynamic_cast<CSnapInNode*>(pNode);
  3198. ASSERT(pSINode != NULL);
  3199. pSINode->CloseControls();
  3200. break;
  3201. }
  3202. }
  3203. HRESULT hr = CMTNode::CloseView(idView);
  3204. ASSERT(hr == S_OK);
  3205. return hr == S_OK ? S_OK : E_FAIL;
  3206. }
  3207. HRESULT CMTSnapInNode::DeleteView(int idView)
  3208. {
  3209. HRESULT hr;
  3210. m_ComponentPersistor.RemoveView(idView);
  3211. hr = CMTNode::DeleteView(idView);
  3212. ASSERT(hr == S_OK);
  3213. return hr == S_OK ? S_OK : E_FAIL;
  3214. }
  3215. SC CMTSnapInNode::ScLoad()
  3216. {
  3217. SC sc;
  3218. CStream stream;
  3219. CLSID clsid;
  3220. sc = CMTNode::ScLoad();
  3221. if(sc)
  3222. goto Error;
  3223. stream.Attach(GetTreeStream());
  3224. sc = stream.ScRead(&clsid, sizeof(clsid));
  3225. if(sc)
  3226. goto Error;
  3227. // read bitmaps, if any
  3228. // we are ignoring error here, because we had gaps in the save code
  3229. // in the past and now we have console files to deal with
  3230. // see bug 96402 "Private: AV in FrontPage Server Extensions & HP ManageX"
  3231. ASSERT (sizeof(m_bHasBitmaps) == sizeof(BOOL));
  3232. sc = stream.ScRead(&m_bHasBitmaps, sizeof(BOOL), true /*bIgnoreErrors*/);
  3233. if(sc)
  3234. goto Error;
  3235. if (m_bHasBitmaps == TRUE)
  3236. {
  3237. WTL::CBitmap bmpSmall;
  3238. sc = ScLoadBitmap (stream, &bmpSmall.m_hBitmap);
  3239. if(sc)
  3240. goto Error;
  3241. WTL::CBitmap bmpSmallOpen;
  3242. sc = ScLoadBitmap (stream, &bmpSmallOpen.m_hBitmap);
  3243. if(sc)
  3244. goto Error;
  3245. WTL::CBitmap bmpLarge;
  3246. sc = ScLoadBitmap (stream, &bmpLarge.m_hBitmap);
  3247. if(sc)
  3248. goto Error;
  3249. COLORREF crMask;
  3250. sc = stream.ScRead(&crMask, sizeof(COLORREF));
  3251. if(sc)
  3252. goto Error;
  3253. sc = ScHandleCustomImages (bmpSmall, bmpSmallOpen, bmpLarge, crMask);
  3254. if (sc)
  3255. goto Error;
  3256. }
  3257. {
  3258. CSnapInsCache* const pCache = theApp.GetSnapInsCache();
  3259. ASSERT(pCache != NULL);
  3260. if (pCache == NULL)
  3261. return E_FAIL;
  3262. CSnapInPtr spSI;
  3263. sc = pCache->ScGetSnapIn(clsid, &spSI);
  3264. if (sc)
  3265. goto Error;
  3266. sc = ScCheckPointers(spSI, E_UNEXPECTED);
  3267. if (sc)
  3268. goto Error;
  3269. SetPrimarySnapIn(spSI);
  3270. pCache->SetDirty(FALSE);
  3271. }
  3272. // see if we have to do the preload thing
  3273. {
  3274. BOOL bPreload = FALSE;
  3275. sc = stream.ScRead(&bPreload, sizeof(BOOL), true /*bIgnoreErrors*/); // the preload bit is optional, do no error out.
  3276. if(sc)
  3277. goto Error;
  3278. SetPreloadRequired (bPreload);
  3279. }
  3280. // read all the streams and storages for this node
  3281. sc = ScReadStreamsAndStoragesFromConsole();
  3282. if(sc)
  3283. goto Error;
  3284. Cleanup:
  3285. return sc == S_OK ? S_OK : E_FAIL;
  3286. Error:
  3287. TraceError(TEXT("CMTSnapInNode::Load"), sc);
  3288. goto Cleanup;
  3289. }
  3290. HRESULT CMTSnapInNode::IsDirty()
  3291. {
  3292. DECLARE_SC (sc, _T("CMTSnapInNode::IsDirty"));
  3293. HRESULT hr = CMTNode::IsDirty();
  3294. ASSERT(SUCCEEDED(hr));
  3295. if (hr != S_FALSE)
  3296. {
  3297. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3298. return hr;
  3299. }
  3300. hr = AreIComponentDatasDirty();
  3301. ASSERT(hr == S_OK || hr == S_FALSE);
  3302. if (hr == S_OK)
  3303. {
  3304. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3305. return S_OK;
  3306. }
  3307. if (hr != S_FALSE)
  3308. {
  3309. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3310. return E_FAIL;
  3311. }
  3312. hr = AreIComponentsDirty();
  3313. ASSERT(hr == S_OK || hr == S_FALSE);
  3314. if (hr == S_OK)
  3315. {
  3316. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3317. return S_OK;
  3318. }
  3319. if (hr != S_FALSE)
  3320. {
  3321. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3322. return E_FAIL;
  3323. }
  3324. /*
  3325. * See if "preload" bit changed. If an error occurred while querying
  3326. * the snap-in, we'll assume that the preload bit hasn't changed.
  3327. */
  3328. PreloadState ePreloadState = m_ePreloadState;
  3329. SC scNoTrace = ScQueryPreloadRequired (ePreloadState);
  3330. if (scNoTrace.IsError() || (ePreloadState == m_ePreloadState))
  3331. {
  3332. TraceDirtyFlag(TEXT("CMTSnapinNode"), false);
  3333. return S_FALSE;
  3334. }
  3335. TraceDirtyFlag(TEXT("CMTSnapinNode"), true);
  3336. return S_OK;
  3337. }
  3338. /*+-------------------------------------------------------------------------*
  3339. * CMTSnapInNode::AreIComponentDatasDirty
  3340. *
  3341. * Returns S_OK if any of the IComponentDatas attached to this snap-in node
  3342. * (i.e. those of this snap-in and its extensions) is dirty, S_FALSE otherwise.
  3343. *--------------------------------------------------------------------------*/
  3344. HRESULT CMTSnapInNode::AreIComponentDatasDirty()
  3345. {
  3346. CComponentData* const pCCD = GetPrimaryComponentData();
  3347. #if 1
  3348. /*
  3349. * we used to check the primary component data explicitly, but that
  3350. * (if it exists) is always the first element in the IComponentData
  3351. * array. The loop below will handle it in a more generic manner.
  3352. */
  3353. ASSERT ((pCCD == NULL) || (pCCD == m_ComponentDataArray[0]));
  3354. #else
  3355. IComponentData* const pICCD = pCCD != NULL ?
  3356. pCCD->GetIComponentData() : NULL;
  3357. if ((pICCD != NULL) && (IsIUnknownDirty (pICCD) == S_OK))
  3358. return (S_OK);
  3359. #endif
  3360. /*
  3361. * check all of the IComponentDatas attached to this snap-in node
  3362. * to see if any one is dirty
  3363. */
  3364. UINT cComponentDatas = m_ComponentDataArray.size();
  3365. for (UINT i = 0; i < cComponentDatas; i++)
  3366. {
  3367. IComponentData* pICCD = (m_ComponentDataArray[i] != NULL)
  3368. ? m_ComponentDataArray[i]->GetIComponentData()
  3369. : NULL;
  3370. if ((pICCD != NULL) && (IsIUnknownDirty (pICCD) == S_OK))
  3371. return (S_OK);
  3372. }
  3373. return (S_FALSE);
  3374. }
  3375. /*+-------------------------------------------------------------------------*
  3376. * CMTSnapInNode::AreIComponentsDirty
  3377. *
  3378. * Returns S_OK if any of the IComponents attached to this snap-in node
  3379. * (in any view) is dirty, S_FALSE otherwise.
  3380. *--------------------------------------------------------------------------*/
  3381. HRESULT CMTSnapInNode::AreIComponentsDirty()
  3382. {
  3383. CNodeList& nodes = GetNodeList();
  3384. ASSERT(&nodes != NULL);
  3385. if (&nodes == NULL)
  3386. return E_FAIL;
  3387. POSITION pos = nodes.GetHeadPosition();
  3388. while (pos)
  3389. {
  3390. CNode* pNode = nodes.GetNext(pos);
  3391. ASSERT(pNode != NULL);
  3392. if (pNode == NULL)
  3393. return E_FAIL;
  3394. CSnapInNode* pSINode = dynamic_cast<CSnapInNode*>(pNode);
  3395. ASSERT(pSINode != NULL);
  3396. if (pSINode == NULL)
  3397. return E_FAIL;
  3398. const CComponentArray& components = pSINode->GetComponentArray();
  3399. const int end = components.size();
  3400. for (int i = 0; i < end; i++)
  3401. {
  3402. CComponent* pCC = components[i];
  3403. if ((NULL == pCC) || (pCC->IsInitialized() == FALSE) )
  3404. continue;
  3405. IComponent* pComponent = pCC->GetIComponent();
  3406. if (NULL == pComponent)
  3407. continue;
  3408. HRESULT hr = IsIUnknownDirty(pComponent);
  3409. ASSERT(hr == S_OK || hr == S_FALSE);
  3410. if (hr == S_OK)
  3411. return S_OK;
  3412. if (hr != S_FALSE)
  3413. return E_FAIL;
  3414. }
  3415. }
  3416. return S_FALSE;
  3417. }
  3418. /*+-------------------------------------------------------------------------*
  3419. * CMTSnapInNode::IsIUnknownDirty
  3420. *
  3421. * Checks an IUnknown* for any of the three persistence interfaces
  3422. * (IPersistStream, IPersistStreamInit, and IPersistStorage, in that order)
  3423. * and if any of them is supported, returns the result of that interface's
  3424. * IsDirty method.
  3425. *--------------------------------------------------------------------------*/
  3426. HRESULT CMTSnapInNode::IsIUnknownDirty(IUnknown* pUnk)
  3427. {
  3428. ASSERT(pUnk != NULL);
  3429. if (pUnk == NULL)
  3430. return E_POINTER;
  3431. // 1. Check for IPersistStream
  3432. IPersistStreamPtr spIPS = pUnk;
  3433. if (spIPS != NULL)
  3434. return spIPS->IsDirty();
  3435. // 2. Check for IPersistStreamInit
  3436. IPersistStreamInitPtr spIPSI = pUnk;
  3437. if (spIPSI != NULL)
  3438. return spIPSI->IsDirty();
  3439. // 3. Check for IPersistStorage
  3440. IPersistStoragePtr spIPStg = pUnk;
  3441. if (spIPStg != NULL)
  3442. return spIPStg->IsDirty();
  3443. return S_FALSE;
  3444. }
  3445. // local functions
  3446. inline long LongScanBytes (long bits)
  3447. {
  3448. bits += 31;
  3449. bits /= 8;
  3450. bits &= ~3;
  3451. return bits;
  3452. }
  3453. SC ScLoadBitmap (CStream &stream, HBITMAP* pBitmap)
  3454. {
  3455. DECLARE_SC(sc, TEXT("ScLoadBitmap"));
  3456. // parameter check
  3457. sc = ScCheckPointers(pBitmap);
  3458. if (sc)
  3459. return sc;
  3460. /*
  3461. * The bitmap we're going to CreateDIBitmap into should be empty.
  3462. * If it's not, it may indicate a bitmap leak. If you've investigated
  3463. * an instance where this assert fails and determined that *pBitmap
  3464. * isn't being leaked (be very sure!), set *pBitmap to NULL before
  3465. * calling ScLoadBitmap. DO NOT remove this assert because you
  3466. * think it's hyperactive.
  3467. */
  3468. ASSERT (*pBitmap == NULL);
  3469. // initialization
  3470. *pBitmap = NULL;
  3471. DWORD dwSize;
  3472. sc = stream.ScRead(&dwSize, sizeof(DWORD));
  3473. if(sc)
  3474. return sc;
  3475. CAutoArrayPtr<BYTE> spDib(new BYTE[dwSize]);
  3476. sc = ScCheckPointers(spDib, E_OUTOFMEMORY);
  3477. if (sc)
  3478. return sc;
  3479. // have a typed pointer for member access
  3480. typedef const BITMAPINFOHEADER * const LPCBITMAPINFOHEADER;
  3481. LPCBITMAPINFOHEADER pDib = reinterpret_cast<LPCBITMAPINFOHEADER>(&spDib[0]);
  3482. sc = stream.ScRead(spDib, dwSize);
  3483. if(sc)
  3484. return sc;
  3485. BYTE * bits = (BYTE*) (pDib+1);
  3486. int depth = pDib->biBitCount*pDib->biPlanes;
  3487. if (depth <= 8)
  3488. bits += (1<<depth)*sizeof(RGBQUAD);
  3489. // get a screen dc
  3490. WTL::CClientDC dc(NULL);
  3491. if (dc == NULL)
  3492. return sc.FromLastError(), sc;
  3493. HBITMAP hbitmap = CreateDIBitmap (dc, pDib, CBM_INIT, bits, (BITMAPINFO*)pDib, DIB_RGB_COLORS);
  3494. if (hbitmap == NULL)
  3495. return sc.FromLastError(), sc;
  3496. // return the bitmap
  3497. *pBitmap = hbitmap;
  3498. return sc;
  3499. }
  3500. /*+-------------------------------------------------------------------------*
  3501. *
  3502. * PersistBitmap
  3503. *
  3504. * PURPOSE: Saves Bitmap to / loads from XML doc.
  3505. *
  3506. * PARAMETERS:
  3507. * CPersistor &persistor :
  3508. * LPCTSTR name : name attribute of instance in XML
  3509. * HBITMAP hBitmap :
  3510. *
  3511. * RETURNS:
  3512. * void
  3513. *
  3514. *+-------------------------------------------------------------------------*/
  3515. void PersistBitmap(CPersistor &persistor, LPCTSTR name, HBITMAP& hBitmap)
  3516. {
  3517. DECLARE_SC(sc, TEXT("PersistBitmap"));
  3518. // combined from ScSaveBitmap & ScLoadBitmap
  3519. // get a screen dc
  3520. WTL::CClientDC dc(NULL);
  3521. if (dc == NULL)
  3522. sc.FromLastError(), sc.Throw();
  3523. CXMLAutoBinary binBlock;
  3524. if (persistor.IsStoring())
  3525. {
  3526. // check pointers
  3527. sc = ScCheckPointers(hBitmap);
  3528. if (sc)
  3529. sc.Throw();
  3530. // create memory dc
  3531. WTL::CDC memdc;
  3532. memdc.CreateCompatibleDC(dc);
  3533. if (memdc == NULL)
  3534. sc.FromLastError(), sc.Throw();
  3535. // get bitmap info
  3536. BITMAP bm;
  3537. if (0 == GetObject (hBitmap, sizeof(BITMAP), (LPSTR)&bm))
  3538. sc.FromLastError(), sc.Throw();
  3539. // TODO: lousy palette stuff
  3540. int depth;
  3541. switch(bm.bmPlanes*bm.bmBitsPixel)
  3542. {
  3543. case 1:
  3544. depth = 1;
  3545. break;
  3546. case 2:
  3547. case 3:
  3548. case 4:
  3549. depth = 4;
  3550. break;
  3551. case 5:
  3552. case 6:
  3553. case 7:
  3554. case 8:
  3555. depth = 8;
  3556. break;
  3557. default:
  3558. depth = 24;
  3559. break;
  3560. }
  3561. DWORD dwSize = sizeof(BITMAPINFOHEADER) + bm.bmHeight*LongScanBytes(depth*bm.bmWidth);
  3562. DWORD colors = 0;
  3563. if(depth <= 8)
  3564. {
  3565. colors = 1<<depth;
  3566. dwSize += colors*sizeof(RGBQUAD);
  3567. }
  3568. sc = binBlock.ScAlloc(dwSize);
  3569. if (sc)
  3570. sc.Throw();
  3571. CXMLBinaryLock sLock(binBlock); // will unlock in destructor
  3572. BITMAPINFOHEADER* dib = NULL;
  3573. sc = sLock.ScLock(&dib);
  3574. if (sc)
  3575. sc.Throw();
  3576. sc = ScCheckPointers(dib, E_UNEXPECTED);
  3577. if (sc)
  3578. sc.Throw();
  3579. BYTE * bits = colors*sizeof(RGBQUAD) + (BYTE *)&dib[1];
  3580. dib->biSize = sizeof(BITMAPINFOHEADER);
  3581. dib->biWidth = bm.bmWidth;
  3582. dib->biHeight = bm.bmHeight;
  3583. dib->biPlanes = 1;
  3584. dib->biBitCount = (WORD)depth;
  3585. dib->biCompression = 0;
  3586. dib->biSizeImage = dwSize; // includes palette and bih ??
  3587. dib->biXPelsPerMeter = 0;
  3588. dib->biYPelsPerMeter = 0;
  3589. dib->biClrUsed = colors;
  3590. dib->biClrImportant = colors;
  3591. HBITMAP hold = memdc.SelectBitmap (hBitmap);
  3592. if (hold == NULL)
  3593. sc.FromLastError(), sc.Throw();
  3594. int lines = GetDIBits (memdc, hBitmap, 0, bm.bmHeight, (LPVOID)bits, (BITMAPINFO*)dib, DIB_RGB_COLORS);
  3595. // see if we were successful
  3596. if (!lines)
  3597. sc.FromLastError();
  3598. else if(lines != bm.bmHeight)
  3599. sc = E_UNEXPECTED; // should not happen
  3600. // clean up gdi resources.
  3601. memdc.SelectBitmap(hold);
  3602. if(sc)
  3603. sc.Throw();
  3604. }
  3605. persistor.Persist(binBlock, name);
  3606. if (persistor.IsLoading())
  3607. {
  3608. /*
  3609. * The bitmap we're going to CreateDIBitmap into should be empty.
  3610. * If it's not, it may indicate a bitmap leak. If you've investigated
  3611. * an instance where this assert fails and determined that hBitmap
  3612. * isn't being leaked (be very sure!), set hBitmap to NULL before
  3613. * calling PersistBitmap. DO NOT remove this assert because you
  3614. * think it's hyperactive.
  3615. */
  3616. ASSERT (hBitmap == NULL);
  3617. hBitmap = NULL;
  3618. CXMLBinaryLock sLock(binBlock); // will unlock in destructor
  3619. BITMAPINFOHEADER* dib = NULL;
  3620. sc = sLock.ScLock(&dib);
  3621. if (sc)
  3622. sc.Throw();
  3623. sc = ScCheckPointers(dib, E_UNEXPECTED);
  3624. if (sc)
  3625. sc.Throw();
  3626. BYTE * bits = (BYTE *)&dib[1];
  3627. int depth = dib->biBitCount*dib->biPlanes;
  3628. if (depth <= 8)
  3629. bits += (1<<depth)*sizeof(RGBQUAD);
  3630. HBITMAP hbitmap = CreateDIBitmap (dc,
  3631. dib, CBM_INIT,
  3632. bits,
  3633. (BITMAPINFO*)dib,
  3634. DIB_RGB_COLORS);
  3635. if (hbitmap == NULL)
  3636. sc.FromLastError(), sc.Throw();
  3637. hBitmap = hbitmap;
  3638. }
  3639. }
  3640. /*+-------------------------------------------------------------------------*
  3641. *
  3642. * CMTSnapInNode::Persist
  3643. *
  3644. * PURPOSE: Persist snapin node
  3645. *
  3646. * PARAMETERS:
  3647. * CPersistor &persistor :
  3648. *
  3649. * RETURNS:
  3650. * void
  3651. *
  3652. *+-------------------------------------------------------------------------*/
  3653. void CMTSnapInNode::Persist(CPersistor& persistor)
  3654. {
  3655. DECLARE_SC(sc, TEXT("CMTSnapInNode::Persist"));
  3656. // save the base class.
  3657. CMTNode::Persist(persistor);
  3658. CLSID clsid;
  3659. ZeroMemory(&clsid,sizeof(clsid));
  3660. if (persistor.IsLoading())
  3661. {
  3662. // check if bitmaps are here
  3663. m_bHasBitmaps = persistor.HasElement(XML_TAG_NODE_BITMAPS, NULL);
  3664. /*
  3665. * load persisted properties, if present
  3666. */
  3667. if (persistor.HasElement (CSnapinProperties::_GetXMLType(), NULL))
  3668. {
  3669. /*
  3670. * create a properties object, since we don't have one yet
  3671. */
  3672. ASSERT (m_spProps == NULL);
  3673. CSnapinProperties* pSIProps = NULL;
  3674. sc = ScCreateSnapinProperties (&pSIProps);
  3675. if (sc)
  3676. sc.Throw();
  3677. if (pSIProps == NULL)
  3678. (sc = E_UNEXPECTED).Throw();
  3679. /*
  3680. * load the properties
  3681. */
  3682. persistor.Persist (*pSIProps);
  3683. }
  3684. }
  3685. else
  3686. {
  3687. clsid = GetPrimarySnapInCLSID();
  3688. /*
  3689. * persist properties, if present
  3690. */
  3691. if (m_spProps != NULL)
  3692. {
  3693. CSnapinProperties* pSIProps = CSnapinProperties::FromInterface(m_spProps);
  3694. if (pSIProps != NULL)
  3695. persistor.Persist (*pSIProps);
  3696. }
  3697. }
  3698. persistor.PersistAttribute(XML_ATTR_MT_NODE_SNAPIN_CLSID, clsid);
  3699. if (m_bHasBitmaps)
  3700. {
  3701. CPersistor persistorBitmaps(persistor, XML_TAG_NODE_BITMAPS);
  3702. /*
  3703. * Early versions of XML persistence saved device-dependent
  3704. * bitmaps. If there's a BinaryData element named "SmallOpen",
  3705. * this is a console saved by early XML persistence -- read it
  3706. * in a special manner.
  3707. */
  3708. if (persistor.IsLoading() &&
  3709. persistorBitmaps.HasElement (XML_TAG_VALUE_BIN_DATA,
  3710. XML_NAME_NODE_BITMAP_SMALL_OPEN))
  3711. {
  3712. WTL::CBitmap bmpSmall, bmpSmallOpen, bmpLarge;
  3713. std::wstring strMask;
  3714. PersistBitmap(persistorBitmaps, XML_NAME_NODE_BITMAP_SMALL, bmpSmall.m_hBitmap);
  3715. PersistBitmap(persistorBitmaps, XML_NAME_NODE_BITMAP_SMALL_OPEN, bmpSmallOpen.m_hBitmap);
  3716. PersistBitmap(persistorBitmaps, XML_NAME_NODE_BITMAP_LARGE, bmpLarge.m_hBitmap);
  3717. persistorBitmaps.PersistAttribute(XML_ATTR_NODE_BITMAPS_MASK, strMask);
  3718. COLORREF crMask = wcstoul(strMask.c_str(), NULL, 16);
  3719. sc = ScHandleCustomImages (bmpSmall, bmpSmallOpen, bmpLarge, crMask);
  3720. if (sc)
  3721. sc.Throw();
  3722. }
  3723. /*
  3724. * We either writing or reading a modern XML file that has persisted
  3725. * the images in device-independent imagelist. Read/write them that way.
  3726. */
  3727. else
  3728. {
  3729. persistorBitmaps.Persist (m_imlSmall, XML_NAME_NODE_BITMAP_SMALL);
  3730. persistorBitmaps.Persist (m_imlLarge, XML_NAME_NODE_BITMAP_LARGE);
  3731. if (persistor.IsLoading())
  3732. {
  3733. sc = ScAddImagesToImageList();
  3734. if (sc)
  3735. sc.Throw();
  3736. }
  3737. }
  3738. }
  3739. // setup snapins CD
  3740. if (persistor.IsLoading())
  3741. {
  3742. CSnapInsCache* const pCache = theApp.GetSnapInsCache();
  3743. if (pCache == NULL)
  3744. sc.Throw(E_FAIL);
  3745. CSnapInPtr spSI;
  3746. sc = pCache->ScGetSnapIn(clsid, &spSI);
  3747. if (sc)
  3748. sc.Throw();
  3749. if (spSI != NULL)
  3750. SetPrimarySnapIn(spSI);
  3751. else
  3752. sc.Throw(E_UNEXPECTED);
  3753. pCache->SetDirty(FALSE);
  3754. }
  3755. // when storing, ask snapins to save their data first
  3756. if ( persistor.IsStoring() )
  3757. {
  3758. sc = ScSaveIComponentDatas();
  3759. if (sc)
  3760. sc.Throw();
  3761. sc = ScSaveIComponents();
  3762. if (sc)
  3763. sc.Throw();
  3764. }
  3765. persistor.Persist(m_CDPersistor);
  3766. persistor.Persist(m_ComponentPersistor);
  3767. /*
  3768. * Save/load the preload bit. Do this last to avoid busting old .msc files.
  3769. */
  3770. BOOL bPreload = false;
  3771. if (persistor.IsStoring() && IsInitialized())
  3772. bPreload = IsPreloadRequired ();
  3773. persistor.PersistAttribute(XML_ATTR_MT_NODE_PRELOAD, CXMLBoolean(bPreload));
  3774. if (persistor.IsLoading())
  3775. SetPreloadRequired (bPreload);
  3776. }
  3777. /*+-------------------------------------------------------------------------*
  3778. * CMTSnapInNode::ScAddImagesToImageList
  3779. *
  3780. * Adds the small and small(open) bitmaps for the snap-in to the scope
  3781. * tree's imagelist.
  3782. *--------------------------------------------------------------------------*/
  3783. SC CMTSnapInNode::ScAddImagesToImageList()
  3784. {
  3785. DECLARE_SC (sc, _T("CMTSnapInNode::ScAddImagesToImageList"));
  3786. /*
  3787. * get the scope tree's imagelist
  3788. */
  3789. CScopeTree* pScopeTree = CScopeTree::GetScopeTree();
  3790. sc = ScCheckPointers (pScopeTree, E_UNEXPECTED);
  3791. if (sc)
  3792. return (sc);
  3793. WTL::CImageList imlScopeTree = pScopeTree->GetImageList();
  3794. if (imlScopeTree.IsNull())
  3795. return (sc = E_UNEXPECTED);
  3796. /*
  3797. * add images to scope tree's imagelist, first closed...
  3798. */
  3799. CSmartIcon icon;
  3800. icon.Attach (m_imlSmall.GetIcon (0));
  3801. if (icon == NULL)
  3802. return (sc.FromLastError());
  3803. SetImage (imlScopeTree.AddIcon (icon));
  3804. /*
  3805. * ...then open
  3806. */
  3807. icon.Attach (m_imlSmall.GetIcon (1));
  3808. if (icon == NULL)
  3809. return (sc.FromLastError());
  3810. SetOpenImage (imlScopeTree.AddIcon (icon));
  3811. return (sc);
  3812. }
  3813. CComponent* CMTSnapInNode::GetComponent(UINT nViewID, COMPONENTID nID,
  3814. CSnapIn* pSnapIn)
  3815. {
  3816. CNodeList& nodes = GetNodeList();
  3817. POSITION pos = nodes.GetHeadPosition();
  3818. CNode* pNode = NULL;
  3819. while (pos)
  3820. {
  3821. pNode = nodes.GetNext(pos);
  3822. if (pNode != NULL && pNode->GetViewID() == (int)nViewID)
  3823. break;
  3824. }
  3825. if(pNode == NULL)
  3826. return NULL;
  3827. ASSERT(pNode != NULL);
  3828. ASSERT(pNode->GetViewID() == (int)nViewID);
  3829. if (pNode->GetViewID() != (int)nViewID)
  3830. return NULL;
  3831. CSnapInNode* pSINode = dynamic_cast<CSnapInNode*>(pNode);
  3832. CComponent* pCC = pSINode->GetComponent(nID);
  3833. if (pCC == NULL)
  3834. pCC = pSINode->CreateComponent(pSnapIn, nID);
  3835. return pCC;
  3836. }
  3837. CNode* CMTSnapInNode::GetNode(CViewData* pViewData, BOOL fRootNode)
  3838. {
  3839. /*
  3840. * check for another CSnapInNode that already exists in this view
  3841. */
  3842. CSnapInNode* pExistingNode = FindNode (pViewData->GetViewID());
  3843. CSnapInNode* pNewNode;
  3844. /*
  3845. * if this is the first CSnapInNode for this view, create a unique one
  3846. */
  3847. if (fRootNode || (pExistingNode == NULL))
  3848. pNewNode = new CSnapInNode (this, pViewData, fRootNode);
  3849. /*
  3850. * otherwise, copy the node that's here
  3851. */
  3852. else
  3853. pNewNode = new CSnapInNode (*pExistingNode);
  3854. return (pNewNode);
  3855. }
  3856. /***************************************************************************\
  3857. *
  3858. * METHOD: CMTSnapInNode::Reset
  3859. *
  3860. * PURPOSE: Resets the node in order to reload extensions. basically it forces
  3861. * save-load-init sequence to refresh the snapin node
  3862. *
  3863. * PARAMETERS:
  3864. *
  3865. * RETURNS:
  3866. * void
  3867. *
  3868. \***************************************************************************/
  3869. void CMTSnapInNode::Reset()
  3870. {
  3871. DECLARE_SC(sc, TEXT("CMTSnapInNode::Reset"));
  3872. CSnapIn * pSnapIn = GetPrimarySnapIn();
  3873. ASSERT(pSnapIn != NULL);
  3874. // we will perform resetting of components and component datas
  3875. // by storing / loading them "the XML way"
  3876. // following that there is nothing what makes this node different
  3877. // from one loaded from XML, so we will change it's type
  3878. sc = ScSaveIComponentDatas();
  3879. if (sc)
  3880. sc.TraceAndClear(); // continue even on error
  3881. sc = ScSaveIComponents();
  3882. if (sc)
  3883. sc.TraceAndClear(); // continue even on error
  3884. // need to reset component XML streams/storage
  3885. sc = m_CDPersistor.ScReset();
  3886. if (sc)
  3887. sc.TraceAndClear(); // continue even on error
  3888. sc = m_ComponentPersistor.ScReset();
  3889. if (sc)
  3890. sc.TraceAndClear(); // continue even on error
  3891. // First Reset all the nodes
  3892. POSITION pos = m_NodeList.GetHeadPosition();
  3893. while (pos)
  3894. {
  3895. CSnapInNode* pSINode =
  3896. dynamic_cast<CSnapInNode*>(m_NodeList.GetNext(pos));
  3897. ASSERT(pSINode != NULL);
  3898. pSINode->Reset();
  3899. }
  3900. for (int i=0; i < m_ComponentDataArray.size(); i++)
  3901. delete m_ComponentDataArray[i];
  3902. m_ComponentDataArray.clear();
  3903. CMTNode::Reset();
  3904. ResetExpandedAtLeastOnce();
  3905. SetPrimarySnapIn(pSnapIn);
  3906. pos = m_NodeList.GetHeadPosition();
  3907. while (pos)
  3908. {
  3909. CSnapInNode* pSINode =
  3910. dynamic_cast<CSnapInNode*>(m_NodeList.GetNext(pos));
  3911. ASSERT(pSINode != NULL);
  3912. CComponent* pCC = new CComponent(pSnapIn);
  3913. pCC->SetComponentID(GetPrimaryComponentID());
  3914. pSINode->AddComponentToArray(pCC);
  3915. pSINode->SetPrimaryComponent(pCC);
  3916. }
  3917. Init();
  3918. pos = m_NodeList.GetHeadPosition();
  3919. while (pos)
  3920. {
  3921. CSnapInNode* pSINode =
  3922. dynamic_cast<CSnapInNode*>(m_NodeList.GetNext(pos));
  3923. ASSERT(pSINode != NULL);
  3924. pSINode->InitComponents();
  3925. }
  3926. }
  3927. /*+-------------------------------------------------------------------------*
  3928. * class CLegacyNodeConverter
  3929. *
  3930. *
  3931. * PURPOSE: Used to emulate the legacy node snapins' Save routines.
  3932. *
  3933. *+-------------------------------------------------------------------------*/
  3934. class CLegacyNodeConverter : public CSerialObjectRW
  3935. {
  3936. public:
  3937. CLegacyNodeConverter(LPCTSTR szName, LPCTSTR szView)
  3938. : m_strName(szName), m_strView(szView)
  3939. {
  3940. }
  3941. ~CLegacyNodeConverter()
  3942. {
  3943. // must call detach or the strings will be removed from the string table.
  3944. m_strName.Detach();
  3945. m_strView.Detach();
  3946. }
  3947. public:
  3948. // CSerialObject methods
  3949. virtual UINT GetVersion() {return 1;}
  3950. virtual HRESULT ReadSerialObject (IStream &stm, UINT nVersion) {ASSERT(0 && "Should not come here."); return E_UNEXPECTED;}
  3951. virtual HRESULT WriteSerialObject(IStream &stm);
  3952. private: // attributes - persisted
  3953. CStringTableString m_strName; // the name of the root node, which is the only node created by the snapin
  3954. CStringTableString m_strView; // the view displayed by the node.
  3955. };
  3956. /*+-------------------------------------------------------------------------*
  3957. *
  3958. * CLegacyNodeConverter::WriteSerialObject
  3959. *
  3960. * PURPOSE: Writes out the name and view strings using the format expected
  3961. * by the built in snapins.
  3962. *
  3963. * PARAMETERS:
  3964. * IStream & stm :
  3965. *
  3966. * RETURNS:
  3967. * HRESULT
  3968. *
  3969. *+-------------------------------------------------------------------------*/
  3970. HRESULT
  3971. CLegacyNodeConverter::WriteSerialObject(IStream &stm)
  3972. {
  3973. stm << m_strName;
  3974. stm << m_strView;
  3975. return S_OK;
  3976. }
  3977. /*+-------------------------------------------------------------------------*
  3978. *
  3979. * CMTSnapInNode::ScConvertLegacyNode
  3980. *
  3981. * PURPOSE: Reads in an legacy node and converts it to a built-in snapin node.
  3982. * 1) The original tree stream is read and the target URL or OCX is read.
  3983. * 2) The new Data stream with the munged CLSID name is created
  3984. * and the data required by the snapin is placed there. Because
  3985. * the bitmap etc is already loaded, and because the original
  3986. * stream is thrown away, we don't need to emulate the "tree"
  3987. * stream. Also, because this snapin has no view specific information,
  3988. * the views storage is not used.
  3989. *
  3990. * PARAMETERS: clsid: The CLSID of the built in snapin.
  3991. *
  3992. * RETURNS:
  3993. * SC
  3994. *
  3995. *+-------------------------------------------------------------------------*/
  3996. SC
  3997. CMTSnapInNode::ScConvertLegacyNode(const CLSID &clsid)
  3998. {
  3999. USES_CONVERSION;
  4000. SC sc;
  4001. std::wstring strView;
  4002. CStream stream;
  4003. CStream nodeStream = NULL;
  4004. int iStorageOrStream=0;
  4005. IStreamPtr spCDStream;
  4006. bool bIsHTMLNode = (&clsid == &CLSID_HTMLSnapin);
  4007. bool bIsOCXNode = (&clsid == &CLSID_OCXSnapin);
  4008. // 1. load the base class
  4009. sc = CMTNode::ScLoad();
  4010. if(sc)
  4011. goto Error;
  4012. // get the tree stream.
  4013. stream.Attach(GetTreeStream());
  4014. // 2. read the URL or OCX string as needed.
  4015. if(bIsHTMLNode)
  4016. {
  4017. WCHAR* szView = NULL;
  4018. // get the string length of the label, and read the string.
  4019. unsigned int stringLength;
  4020. sc = stream.ScRead(&stringLength, sizeof(stringLength));
  4021. if(sc)
  4022. goto Error;
  4023. szView = reinterpret_cast<wchar_t*>(alloca((stringLength+1)*sizeof(WCHAR))); // allocates on stack, don't free.
  4024. if (szView == NULL)
  4025. goto PointerError;
  4026. sc = stream.ScRead(szView, stringLength*2);
  4027. if(sc)
  4028. goto Error;
  4029. szView[stringLength] = TEXT('\0'); // null terminate the string.
  4030. strView = szView;
  4031. }
  4032. else if(bIsOCXNode)
  4033. {
  4034. CLSID clsidOCX;
  4035. // Read OCX clsid
  4036. sc = stream.ScRead(&clsidOCX, sizeof(clsidOCX));
  4037. if(sc)
  4038. goto Error;
  4039. {
  4040. WCHAR szCLSID[40];
  4041. if (0 == StringFromGUID2 (clsidOCX, szCLSID, countof(szCLSID)))
  4042. {
  4043. sc = E_UNEXPECTED;
  4044. goto Error;
  4045. }
  4046. strView = szCLSID;
  4047. }
  4048. }
  4049. // at this point, strView contains either the URL or OCX CLSID.
  4050. // 3. Write node name
  4051. sc = m_CDPersistor.ScGetIStream( clsid, &spCDStream );
  4052. if (sc)
  4053. goto Error;
  4054. nodeStream.Attach( spCDStream );
  4055. if(NULL == nodeStream.Get())
  4056. goto PointerError;
  4057. // 4. Write out the Data stream.
  4058. {
  4059. tstring strName = GetDisplayName();
  4060. CLegacyNodeConverter converter(strName.data(), OLE2CT(strView.data()));
  4061. // call the converter to write out the stream.
  4062. sc = converter.Write(nodeStream);
  4063. if(sc)
  4064. goto Error;
  4065. }
  4066. // at this point, the "data" stream should be correctly written out.
  4067. // 5. For OCX nodes, convert the view streams and storages
  4068. /* OLD NEW
  4069. 2 (node storage) 2 (node storage)
  4070. data data
  4071. tree tree
  4072. view view
  4073. 1 <--- streams and storages --------- 1
  4074. 2 <--- written by OCX, 1 per view -- \ 1jvmv2n4y1k471h9ujk86lite7 (OCX snap-in)
  4075. \ --------> ocx_stream (or ocx_storage)
  4076. \
  4077. ------> 2 1jvmv2n4y1k471h9ujk86lite7 (OCX snap-in)
  4078. ocx_stream (or ocx_storage)
  4079. */
  4080. if(bIsOCXNode)
  4081. {
  4082. for(iStorageOrStream = 1 /*NOT zero*/; ; iStorageOrStream++)
  4083. {
  4084. // create the name of the storage
  4085. CStr strStorageOrStream;
  4086. strStorageOrStream.Format(TEXT("%d"), iStorageOrStream);
  4087. // at this point strStorageOrStream should contain a number like "1"
  4088. CStorage storageView(GetViewStorage());
  4089. // rename the storage or stream labelled "1" to "temp" under the same parent.
  4090. sc = storageView.ScMoveElementTo(T2COLE(strStorageOrStream), storageView, L"temp", STGMOVE_MOVE);
  4091. if(sc == SC(STG_E_FILENOTFOUND)) // loop end condition - no more streams or storages
  4092. {
  4093. sc.Clear();
  4094. break;
  4095. }
  4096. if(sc)
  4097. goto Error;
  4098. // now we create the storage with the same name, eg "1"
  4099. {
  4100. WCHAR name[MAX_PATH];
  4101. GetComponentStorageName(name, clsid); // the name of the snapin component
  4102. CStorage storageNewView, storageSnapIn;
  4103. sc = storageNewView.ScCreate(storageView, T2COLE(strStorageOrStream),
  4104. STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
  4105. L"\\node\\#\\view\\#\\storage" /*CHANGE*/);
  4106. if(sc)
  4107. goto Error;
  4108. // create the snapin's storage underneath the view's storage
  4109. sc = storageSnapIn.ScCreate(storageNewView, name,
  4110. STGM_WRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
  4111. L"\\node\\#\\view\\#\\storage\\#\\snapinStorage");
  4112. if(sc)
  4113. goto Error;
  4114. // move the "temp" stream or storage to the storage called L"ocx_streamorstorage"
  4115. // (which is what the OCX snapin expects.)
  4116. sc = storageView.ScMoveElementTo(L"temp", storageSnapIn, L"ocx_streamorstorage", STGMOVE_MOVE);
  4117. if(sc)
  4118. goto Error;
  4119. }
  4120. }
  4121. }
  4122. // 6. now do the same thing that CMTSnapInNode::ScLoad would do.
  4123. {
  4124. CSnapInsCache* const pCache = theApp.GetSnapInsCache();
  4125. ASSERT(pCache != NULL);
  4126. if (pCache == NULL)
  4127. goto FailedError;
  4128. CSnapInPtr spSI;
  4129. sc = pCache->ScGetSnapIn(clsid, &spSI);
  4130. ASSERT(!sc.IsError() && spSI != NULL);
  4131. if (!sc.IsError() && spSI != NULL)
  4132. SetPrimarySnapIn(spSI);
  4133. pCache->SetDirty(FALSE);
  4134. if(sc)
  4135. goto Error;
  4136. }
  4137. // always set the preload bit.
  4138. SetPreloadRequired (true);
  4139. // Some actions (loading bitmaps for example) performed here invalidate the node
  4140. // and set the dirty flag. Since coverting legacy node may be done any time again
  4141. // the converted node should not be assumed as changed.
  4142. ClearDirty();
  4143. // read all the streams and storages for this node
  4144. sc = ScReadStreamsAndStoragesFromConsole();
  4145. if(sc)
  4146. goto Error;
  4147. Cleanup:
  4148. return sc;
  4149. FailedError:
  4150. sc = E_FAIL;
  4151. goto Error;
  4152. PointerError:
  4153. sc = E_POINTER;
  4154. Error:
  4155. TraceError(TEXT("CMTSnapInNode::ScConvertLegacyNode"), sc);
  4156. goto Cleanup;
  4157. }
  4158. HRESULT copyStream(IStream* dest, IStream* src)
  4159. {
  4160. ASSERT(dest != NULL);
  4161. ASSERT(src != NULL);
  4162. if (dest == NULL || src == NULL)
  4163. return E_POINTER;
  4164. const LARGE_INTEGER loc = {0,0};
  4165. ULARGE_INTEGER newLoc;
  4166. HRESULT hr = src->Seek(loc, STREAM_SEEK_SET, &newLoc);
  4167. ASSERT(SUCCEEDED(hr));
  4168. if (FAILED(hr))
  4169. return E_FAIL;
  4170. hr = dest->Seek(loc, STREAM_SEEK_SET, &newLoc);
  4171. ASSERT(SUCCEEDED(hr));
  4172. if (FAILED(hr))
  4173. return E_FAIL;
  4174. const ULARGE_INTEGER size = {0,0};
  4175. hr = dest->SetSize(size);
  4176. ASSERT(SUCCEEDED(hr));
  4177. if (FAILED(hr))
  4178. return hr;
  4179. STATSTG statstg;
  4180. hr = src->Stat(&statstg, STATFLAG_NONAME);
  4181. ASSERT(hr == S_OK);
  4182. if (hr != S_OK)
  4183. return E_FAIL;
  4184. ULARGE_INTEGER cr;
  4185. ULARGE_INTEGER cw;
  4186. hr = src->CopyTo(dest, statstg.cbSize, &cr, &cw);
  4187. #if 0 // for debugging...
  4188. for (long i = 0; true; i++)
  4189. {
  4190. BYTE b;
  4191. long bytesRead;
  4192. hr = src->Read(&b, sizeof(b), &bytesRead);
  4193. if (hr != S_OK)
  4194. return S_OK;
  4195. long bytesWritten;
  4196. hr = dest->Write(&b, bytesRead, &bytesWritten);
  4197. ASSERT(hr == S_OK);
  4198. ASSERT(bytesWritten == bytesRead);
  4199. if (hr != S_OK || bytesWritten != bytesRead)
  4200. return E_FAIL;
  4201. }
  4202. #endif
  4203. return S_OK;
  4204. }
  4205. //############################################################################
  4206. //############################################################################
  4207. //
  4208. // Helper functions
  4209. //
  4210. //############################################################################
  4211. //############################################################################
  4212. void DisplayPolicyErrorMessage(const CLSID& clsid, bool bExtension)
  4213. {
  4214. CStr strMessage;
  4215. if (bExtension)
  4216. strMessage.LoadString(GetStringModule(), IDS_EXTENSION_NOTALLOWED);
  4217. else
  4218. strMessage.LoadString(GetStringModule(), IDS_SNAPIN_NOTALLOWED);
  4219. // Get the snapin name for the error message.
  4220. CSnapInsCache* pSnapInsCache = theApp.GetSnapInsCache();
  4221. ASSERT(pSnapInsCache != NULL);
  4222. CSnapInPtr spSnapIn;
  4223. SC sc = pSnapInsCache->ScFindSnapIn(clsid, &spSnapIn);
  4224. if (!sc.IsError() && (NULL != spSnapIn))
  4225. {
  4226. WTL::CString strName;
  4227. sc = spSnapIn->ScGetSnapInName (strName);
  4228. if (!sc.IsError())
  4229. {
  4230. strMessage += _T("\n");
  4231. strMessage += strName;
  4232. strMessage += _T(".");
  4233. }
  4234. }
  4235. ::MessageBox(NULL, strMessage, _T("MMC"), MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL);
  4236. }
  4237. /***************************************************************************\
  4238. *
  4239. * METHOD: CMMCSnapIn::get_Vendor
  4240. *
  4241. * PURPOSE: returns vendor info for snapin. Implements OM property SnapIn.Vendor
  4242. *
  4243. * PARAMETERS:
  4244. * PBSTR pbstrVendor [out] - vendor info
  4245. *
  4246. * RETURNS:
  4247. * HRESULT - result code
  4248. *
  4249. \***************************************************************************/
  4250. STDMETHODIMP CMMCSnapIn::get_Vendor( PBSTR pbstrVendor )
  4251. {
  4252. DECLARE_SC(sc, TEXT("CMMCSnapIn::get_Vendor"));
  4253. sc = ScCheckPointers(pbstrVendor);
  4254. if (sc)
  4255. return sc.ToHr();
  4256. // init out parameter
  4257. *pbstrVendor = NULL;
  4258. // get the snapin about
  4259. CSnapinAbout *pSnapinAbout = NULL;
  4260. sc = ScGetSnapinAbout(pSnapinAbout);
  4261. if (sc)
  4262. return sc.ToHr();
  4263. // recheck the pointer
  4264. sc = ScCheckPointers(pSnapinAbout, E_UNEXPECTED);
  4265. if (sc)
  4266. return sc.ToHr();
  4267. *pbstrVendor = ::SysAllocString( pSnapinAbout->GetCompanyName() );
  4268. return sc.ToHr();
  4269. }
  4270. /***************************************************************************\
  4271. *
  4272. * METHOD: CMMCSnapIn::get_Version
  4273. *
  4274. * PURPOSE: returns version info for snapin. Implements OM property SnapIn.Version
  4275. *
  4276. * PARAMETERS:
  4277. * PBSTR pbstrVersion [out] - version info
  4278. *
  4279. * RETURNS:
  4280. * HRESULT - result code
  4281. *
  4282. \***************************************************************************/
  4283. STDMETHODIMP CMMCSnapIn::get_Version( PBSTR pbstrVersion )
  4284. {
  4285. DECLARE_SC(sc, TEXT("CMMCSnapIn::get_Version"));
  4286. sc = ScCheckPointers(pbstrVersion);
  4287. if (sc)
  4288. return sc.ToHr();
  4289. // init out parameter
  4290. *pbstrVersion = NULL;
  4291. // get the snapin about
  4292. CSnapinAbout *pSnapinAbout = NULL;
  4293. sc = ScGetSnapinAbout(pSnapinAbout);
  4294. if (sc)
  4295. return sc.ToHr();
  4296. // recheck the pointer
  4297. sc = ScCheckPointers(pSnapinAbout, E_UNEXPECTED);
  4298. if (sc)
  4299. return sc.ToHr();
  4300. *pbstrVersion = ::SysAllocString( pSnapinAbout->GetVersion() );
  4301. return sc.ToHr();
  4302. }
  4303. /***************************************************************************\
  4304. *
  4305. * METHOD: CMMCSnapIn::GetMTSnapInNode
  4306. *
  4307. * PURPOSE: helper. returns mtnode for the snapin
  4308. *
  4309. * PARAMETERS:
  4310. *
  4311. * RETURNS:
  4312. * CMTSnapInNode * - node
  4313. *
  4314. \***************************************************************************/
  4315. CMTSnapInNode * CMMCSnapIn::GetMTSnapInNode()
  4316. {
  4317. CMTSnapInNode *pMTSnapInNode = NULL;
  4318. SC sc = ScGetTiedObject(pMTSnapInNode);
  4319. if (sc)
  4320. return NULL;
  4321. return pMTSnapInNode;
  4322. }
  4323. /***************************************************************************\
  4324. *
  4325. * METHOD: CMMCSnapIn::ScGetSnapinAbout
  4326. *
  4327. * PURPOSE: helper. returns snapins about object
  4328. *
  4329. * PARAMETERS:
  4330. * CSnapinAbout*& pAbout [out] - snapins about object
  4331. *
  4332. * RETURNS:
  4333. * SC - result code
  4334. *
  4335. \***************************************************************************/
  4336. SC CMMCSnapIn::ScGetSnapinAbout(CSnapinAbout*& pAbout)
  4337. {
  4338. DECLARE_SC(sc, TEXT("CMMCSnapIn::ScGetSnapinAbout"));
  4339. // init out param
  4340. pAbout = NULL;
  4341. // If the snapin object is already created just return it.
  4342. if (NULL != (pAbout = m_spSnapinAbout.get()))
  4343. return sc;
  4344. // get snapins clsid
  4345. CLSID clsidSnapin = GUID_NULL;
  4346. sc = GetSnapinClsid(clsidSnapin);
  4347. if (sc)
  4348. return sc;
  4349. CLSID clsidAbout; // get the about class-id.
  4350. sc = ScGetAboutFromSnapinCLSID(clsidSnapin, clsidAbout);
  4351. if (sc)
  4352. return sc;
  4353. if (clsidSnapin == GUID_NULL)
  4354. return sc = E_FAIL;
  4355. // Create about object
  4356. m_spSnapinAbout = SnapinAboutPtr (new CSnapinAbout);
  4357. if (! m_spSnapinAbout.get())
  4358. return sc = E_OUTOFMEMORY;
  4359. // and initialize it.
  4360. if (!m_spSnapinAbout->GetSnapinInformation(clsidAbout))
  4361. return sc = E_FAIL;
  4362. pAbout = m_spSnapinAbout.get();
  4363. return sc;
  4364. }
  4365. /*+-------------------------------------------------------------------------*
  4366. * CMTSnapInNode::IsPreloadRequired
  4367. *
  4368. * Returns true if the snap-in wants MMCN_PRELOAD notifications, false
  4369. * otherwise.
  4370. *--------------------------------------------------------------------------*/
  4371. BOOL CMTSnapInNode::IsPreloadRequired () const
  4372. {
  4373. DECLARE_SC (sc, _T("CMTSnapInNode::IsPreloadRequired"));
  4374. /*
  4375. * if we don't know whether the snap-in wants MMCN_PRELOAD (because
  4376. * we haven't asked it yet), ask now
  4377. */
  4378. if (m_ePreloadState == ePreload_Unknown)
  4379. {
  4380. /*
  4381. * assume preload isn't required
  4382. */
  4383. m_ePreloadState = ePreload_False;
  4384. sc = ScQueryPreloadRequired (m_ePreloadState);
  4385. if (sc)
  4386. sc.TraceAndClear();
  4387. }
  4388. return (m_ePreloadState == ePreload_True);
  4389. }
  4390. /*+-------------------------------------------------------------------------*
  4391. * CMTSnapInNode::ScQueryPreloadRequired
  4392. *
  4393. * Asks the snap-in whether it requires preload notification by asking its
  4394. * data object for the CCF_SNAPIN_PRELOADS format.
  4395. *
  4396. * Returns in ePreload:
  4397. *
  4398. * ePreload_True snap-in requires MMCN_PRELOAD
  4399. * ePreload_False snap-in doesn't require MMCN_PRELOAD
  4400. *
  4401. * If anything fails during the process of asking the snap-in for
  4402. * CCF_SNAPIN_PRELOADS, the value of ePreload is unchanged.
  4403. *--------------------------------------------------------------------------*/
  4404. SC CMTSnapInNode::ScQueryPreloadRequired (
  4405. PreloadState& ePreload) const /* O:preload state for snap-in */
  4406. {
  4407. DECLARE_SC (sc, _T("CMTSnapInNode::ScQueryPreloadRequired"));
  4408. /*
  4409. * make sure we have a primary ComponentData
  4410. */
  4411. CComponentData* pCCD = GetPrimaryComponentData();
  4412. sc = ScCheckPointers (pCCD, E_UNEXPECTED);
  4413. if (sc)
  4414. return (sc);
  4415. /*
  4416. * get the data object for this node
  4417. */
  4418. IDataObjectPtr spDataObject;
  4419. sc = pCCD->QueryDataObject(GetUserParam(), CCT_SCOPE, &spDataObject);
  4420. if (sc)
  4421. return (sc);
  4422. sc = ScCheckPointers (spDataObject, E_UNEXPECTED);
  4423. if (sc)
  4424. return (sc);
  4425. /*
  4426. * CCF_SNAPIN_PRELOADS is an optional clipboard format, so it's not
  4427. * an error if ExtractData fails.
  4428. */
  4429. BOOL bPreload = (ePreload == ePreload_True) ? TRUE : FALSE;
  4430. if (SUCCEEDED (ExtractData (spDataObject, GetPreLoadFormat(),
  4431. (BYTE*)&bPreload, sizeof(BOOL))))
  4432. {
  4433. ePreload = (bPreload) ? ePreload_True : ePreload_False;
  4434. }
  4435. return (sc);
  4436. }
  4437. /***************************************************************************\
  4438. *
  4439. * METHOD: CMTSnapInNode::ScReadStreamsAndStoragesFromConsole
  4440. *
  4441. * PURPOSE: Enumerates old (structured storage based) console.
  4442. * Enumerates the streams and storages under the snapin node.
  4443. * For each stream/storage found adds a copy to m_CDPpersistor
  4444. * or m_ComponentPersistor, indexed by a hash value (name in storage).
  4445. * These entries will be recognized and stored by a CLSID when
  4446. * CLSID is known ( when request by CLSID is made )
  4447. *
  4448. * PARAMETERS:
  4449. *
  4450. * RETURNS:
  4451. * SC - result code
  4452. *
  4453. \***************************************************************************/
  4454. SC CMTSnapInNode::ScReadStreamsAndStoragesFromConsole()
  4455. {
  4456. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScReadStreamsAndStoragesFromConsole"));
  4457. IStorage* pNodeCDStorage = GetStorageForCD();
  4458. sc = ScCheckPointers( pNodeCDStorage, E_POINTER );
  4459. if (sc)
  4460. return sc;
  4461. IEnumSTATSTGPtr spEnum;
  4462. sc = pNodeCDStorage->EnumElements( 0, NULL, 0, &spEnum );
  4463. if (sc)
  4464. return sc;
  4465. // recheck pointer
  4466. sc = ScCheckPointers( spEnum, E_POINTER );
  4467. if (sc)
  4468. return sc;
  4469. // reset enumeration
  4470. sc = spEnum->Reset();
  4471. if (sc)
  4472. return sc;
  4473. // enumerate the items ( each entry is for separate component data )
  4474. while (1)
  4475. {
  4476. STATSTG statstg;
  4477. ZeroMemory( &statstg, sizeof(statstg) );
  4478. ULONG cbFetched = 0;
  4479. sc = spEnum->Next( 1, &statstg, &cbFetched );
  4480. if (sc)
  4481. return sc;
  4482. if ( sc != S_OK ) // - done
  4483. {
  4484. sc.Clear();
  4485. break;
  4486. }
  4487. // attach to the out param
  4488. CCoTaskMemPtr<WCHAR> spName( statstg.pwcsName );
  4489. // make a copy of streams and storages
  4490. if ( statstg.type == STGTY_STREAM )
  4491. {
  4492. IStreamPtr spStream;
  4493. sc = OpenDebugStream(pNodeCDStorage, spName, STGM_READ | STGM_SHARE_EXCLUSIVE,
  4494. L"\\node\\#\\data\\clsid", &spStream);
  4495. if (sc)
  4496. return sc;
  4497. sc = m_CDPersistor.ScInitIStream( spName, spStream );
  4498. if (sc)
  4499. return sc;
  4500. }
  4501. else if ( statstg.type == STGTY_STORAGE )
  4502. {
  4503. IStoragePtr spStorage;
  4504. sc = OpenDebugStorage(pNodeCDStorage, spName, STGM_READ | STGM_SHARE_EXCLUSIVE,
  4505. L"\\node\\#\\data\\clsid", &spStorage);
  4506. if (sc)
  4507. return sc;
  4508. sc = m_CDPersistor.ScInitIStorage( spName, spStorage );
  4509. if (sc)
  4510. return sc;
  4511. }
  4512. }
  4513. // view streams/storages
  4514. IStorage *pNodeComponentStorage = GetViewStorage();
  4515. sc = ScCheckPointers( pNodeComponentStorage, E_POINTER );
  4516. if (sc)
  4517. return sc;
  4518. spEnum = NULL;
  4519. sc = pNodeComponentStorage->EnumElements( 0, NULL, 0, &spEnum );
  4520. if (sc)
  4521. return sc;
  4522. // recheck pointer
  4523. sc = ScCheckPointers( spEnum, E_POINTER );
  4524. if (sc)
  4525. return sc;
  4526. // reset enumeration
  4527. sc = spEnum->Reset();
  4528. if (sc)
  4529. return sc;
  4530. // enumerate the items ( each entry is for separate view )
  4531. while (1)
  4532. {
  4533. STATSTG statstg;
  4534. ZeroMemory( &statstg, sizeof(statstg) );
  4535. ULONG cbFetched = 0;
  4536. sc = spEnum->Next( 1, &statstg, &cbFetched );
  4537. if (sc)
  4538. return sc;
  4539. if ( sc != S_OK ) // done
  4540. {
  4541. sc.Clear();
  4542. break;
  4543. }
  4544. // attach to the out param
  4545. CCoTaskMemPtr<WCHAR> spName( statstg.pwcsName );
  4546. // read the view storage
  4547. if ( statstg.type == STGTY_STORAGE )
  4548. {
  4549. int idView = CMTNode::GetViewIdFromStorageName(spName);
  4550. IStoragePtr spViewStorage;
  4551. sc = OpenDebugStorage(pNodeComponentStorage, spName, STGM_READ | STGM_SHARE_EXCLUSIVE,
  4552. L"\\node\\#\\view\\#", &spViewStorage);
  4553. if (sc)
  4554. return sc;
  4555. // enumerate what's inside a view storage
  4556. IEnumSTATSTGPtr spViewEnum;
  4557. sc = spViewStorage->EnumElements( 0, NULL, 0, &spViewEnum );
  4558. if (sc)
  4559. return sc;
  4560. // recheck pointer
  4561. sc = ScCheckPointers( spViewEnum, E_POINTER );
  4562. if (sc)
  4563. return sc;
  4564. // reset enumeration
  4565. sc = spViewEnum->Reset();
  4566. if (sc)
  4567. return sc;
  4568. // enumerate the items ( each entry is for separate component in a view )
  4569. while (1)
  4570. {
  4571. STATSTG statstg;
  4572. ZeroMemory( &statstg, sizeof(statstg) );
  4573. ULONG cbFetched = 0;
  4574. sc = spViewEnum->Next( 1, &statstg, &cbFetched );
  4575. if (sc)
  4576. return sc;
  4577. if ( sc != S_OK ) // - done
  4578. {
  4579. sc.Clear();
  4580. break;
  4581. }
  4582. // attach to the out param
  4583. CCoTaskMemPtr<WCHAR> spName( statstg.pwcsName );
  4584. // make a copy of streams and storages
  4585. if ( statstg.type == STGTY_STREAM )
  4586. {
  4587. IStreamPtr spStream;
  4588. sc = OpenDebugStream(spViewStorage, spName, STGM_READ | STGM_SHARE_EXCLUSIVE,
  4589. L"\\node\\#\\view\\#\\clsid", &spStream);
  4590. if (sc)
  4591. return sc;
  4592. sc = m_ComponentPersistor.ScInitIStream( idView, spName, spStream );
  4593. if (sc)
  4594. return sc;
  4595. }
  4596. else if ( statstg.type == STGTY_STORAGE )
  4597. {
  4598. IStoragePtr spStorage;
  4599. sc = OpenDebugStorage(spViewStorage, spName, STGM_READ | STGM_SHARE_EXCLUSIVE,
  4600. L"\\node\\#\\view\\#\\clsid", &spStorage);
  4601. if (sc)
  4602. return sc;
  4603. sc = m_ComponentPersistor.ScInitIStorage( idView, spName, spStorage );
  4604. if (sc)
  4605. return sc;
  4606. }
  4607. }
  4608. }
  4609. }
  4610. // by now we should have loaded everything from the console file
  4611. return sc;
  4612. }
  4613. /***************************************************************************\
  4614. *
  4615. * METHOD: CMTSnapInNode::ScSaveIComponentDatas
  4616. *
  4617. * PURPOSE: Saves IComponentDatass for all snapins under this static scope node
  4618. *
  4619. * PARAMETERS:
  4620. *
  4621. *
  4622. * RETURNS:
  4623. * SC - result code
  4624. *
  4625. \***************************************************************************/
  4626. SC CMTSnapInNode::ScSaveIComponentDatas( )
  4627. {
  4628. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScSaveIComponentDatas"));
  4629. // if node is not initialized ( not expanded ) - nothing to save
  4630. // old data will be persisted.
  4631. if ( !IsInitialized() )
  4632. return sc;
  4633. // go for every component data we have
  4634. for( int i = 0; i< GetNumberOfComponentDatas(); i++ )
  4635. {
  4636. CComponentData* pCD = GetComponentData(i);
  4637. sc = ScCheckPointers(pCD, E_UNEXPECTED);
  4638. if (sc)
  4639. return sc;
  4640. sc = ScSaveIComponentData( pCD );
  4641. if (sc)
  4642. return sc;
  4643. }
  4644. return sc;
  4645. }
  4646. /***************************************************************************\
  4647. *
  4648. * METHOD: CMTSnapInNode::ScSaveIComponentData
  4649. *
  4650. * PURPOSE: Determines snapin's IComponentData persistence capabilities (QI for IPersistXXXX)
  4651. * And asks it to save giving maintained stream/storage as a media.
  4652. *
  4653. * PARAMETERS:
  4654. * CComponentData* pCD [in] component data
  4655. *
  4656. * RETURNS:
  4657. * SC - result code
  4658. *
  4659. \***************************************************************************/
  4660. SC CMTSnapInNode::ScSaveIComponentData( CComponentData* pCD )
  4661. {
  4662. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScSaveIComponentData"));
  4663. sc = ScCheckPointers(pCD);
  4664. if (sc)
  4665. return sc;
  4666. // check if the component is initialized
  4667. if ( !pCD->IsIComponentDataInitialized() )
  4668. {
  4669. // compatibility with mmc1.2 - give another chance.
  4670. sc = ScInitIComponentData(pCD);
  4671. if (sc)
  4672. return sc;
  4673. }
  4674. // Check first for an IComponentData
  4675. IComponentData* const pICCD = pCD->GetIComponentData();
  4676. sc = ScCheckPointers( pICCD, E_UNEXPECTED );
  4677. if (sc)
  4678. return sc;
  4679. // Get the snapin name for the error message.
  4680. CSnapInPtr spSnapin = pCD->GetSnapIn();
  4681. // now ask the snapin to save the data
  4682. sc = ScAskSnapinToSaveData( pICCD, &m_CDPersistor, CDPersistor::VIEW_ID_DOCUMENT, pCD->GetCLSID(), spSnapin );
  4683. if (sc)
  4684. return sc;
  4685. return sc;
  4686. }
  4687. /***************************************************************************\
  4688. *
  4689. * METHOD: CMTSnapInNode::ScSaveIComponents
  4690. *
  4691. * PURPOSE: Saves IComponents for all snapins under this static scope node
  4692. *
  4693. * PARAMETERS:
  4694. *
  4695. *
  4696. * RETURNS:
  4697. * SC - result code
  4698. *
  4699. \***************************************************************************/
  4700. SC CMTSnapInNode::ScSaveIComponents( )
  4701. {
  4702. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScSaveIComponents"));
  4703. // if node is not initialized ( not expanded ) - nothing to save
  4704. // old data will be persisted.
  4705. if ( !IsInitialized() )
  4706. return sc;
  4707. // go for every CNode in every view
  4708. CNodeList& nodes = GetNodeList();
  4709. POSITION pos = nodes.GetHeadPosition();
  4710. while (pos)
  4711. {
  4712. CNode* pNode = nodes.GetNext( pos );
  4713. sc = ScCheckPointers( pNode, E_UNEXPECTED );
  4714. if (sc)
  4715. return sc;
  4716. CSnapInNode* pSINode = dynamic_cast<CSnapInNode*>(pNode);
  4717. sc = ScCheckPointers(pSINode, E_UNEXPECTED);
  4718. if (sc)
  4719. return sc;
  4720. const int viewID = pNode->GetViewID();
  4721. const CComponentArray& components = pSINode->GetComponentArray();
  4722. const int size = components.size();
  4723. for (int i = 0; i < size; i++)
  4724. {
  4725. CComponent* pCC = components[i];
  4726. if ( pCC != NULL )
  4727. {
  4728. sc = ScSaveIComponent( pCC, viewID);
  4729. if (sc)
  4730. return sc;
  4731. }
  4732. }
  4733. }
  4734. return sc;
  4735. }
  4736. /***************************************************************************\
  4737. *
  4738. * METHOD: CMTSnapInNode::ScSaveIComponent
  4739. *
  4740. * PURPOSE: Determines snapin's IComponent persistence capabilities (QI for IPersistXXXX)
  4741. * And asks it to save giving maintained stream/storage as a media.
  4742. *
  4743. * PARAMETERS:
  4744. * CComponent* pCComponent [in] component
  4745. * int viewID [in] view id for which the component is created
  4746. *
  4747. * RETURNS:
  4748. * SC - result code
  4749. *
  4750. \***************************************************************************/
  4751. SC CMTSnapInNode::ScSaveIComponent( CComponent* pCComponent, int viewID )
  4752. {
  4753. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScSaveIComponent"));
  4754. // parameter check
  4755. sc = ScCheckPointers( pCComponent );
  4756. if (sc)
  4757. return sc;
  4758. const CLSID& clsid = pCComponent->GetCLSID();
  4759. // check if the component is initialized (compatibility with mmc 1.2)
  4760. // give another chance to load
  4761. if ( !pCComponent->IsIComponentInitialized() )
  4762. {
  4763. sc = ScInitIComponent(pCComponent, viewID);
  4764. if (sc)
  4765. return sc;
  4766. }
  4767. // get IComponent
  4768. IComponent* pComponent = pCComponent->GetIComponent();
  4769. sc = ScCheckPointers(pComponent, E_UNEXPECTED);
  4770. if (sc)
  4771. return sc;
  4772. // Get the snapin to get name for the error message.
  4773. CSnapInPtr spSnapin = pCComponent->GetSnapIn();
  4774. // now ask the snapin to save the data
  4775. sc = ScAskSnapinToSaveData( pComponent, &m_ComponentPersistor, viewID, clsid, spSnapin );
  4776. if (sc)
  4777. return sc;
  4778. return sc;
  4779. }
  4780. /***************************************************************************\
  4781. *
  4782. * METHOD: CMTSnapInNode::ScAskSnapinToSaveData
  4783. *
  4784. * PURPOSE: Determines snapin persistence capabilities (QI for IPersistXXXX)
  4785. * And asks it to save giving maintained stream/storage as a media.
  4786. * This method is called to save both Components and ComponentDatas
  4787. *
  4788. * PARAMETERS:
  4789. * IUnknown *pSnapin [in] snapin which data needs to be saved
  4790. * CMTSnapinNodeStreamsAndStorages *pStreamsAndStorages
  4791. * [in] collection of streams/storage where to save
  4792. * int idView [in] view id - key for saved data
  4793. * const CLSID& clsid [in] class id - key for saved data
  4794. * CSnapIn *pCSnapin [in] pointer to CSnapin, used for display name on error
  4795. *
  4796. * RETURNS:
  4797. * SC - result code
  4798. *
  4799. \***************************************************************************/
  4800. SC CMTSnapInNode::ScAskSnapinToSaveData( IUnknown *pSnapin,
  4801. CMTSnapinNodeStreamsAndStorages *pStreamsAndStorages,
  4802. int idView , const CLSID& clsid, CSnapIn *pCSnapin )
  4803. {
  4804. DECLARE_SC(sc, TEXT("CMTSnapInNode::ScAskSnapinToSaveData"));
  4805. sc = ScCheckPointers( pSnapin, pStreamsAndStorages );
  4806. if (sc)
  4807. return sc;
  4808. IPersistStreamPtr spIPS;
  4809. IPersistStoragePtr spIPStg;
  4810. IPersistStreamInitPtr spIPSI;
  4811. // QI for IPersistStream
  4812. if ( (spIPS = pSnapin) != NULL)
  4813. {
  4814. // get the object for persistence
  4815. CXML_IStream *pXMLStream = NULL;
  4816. sc = pStreamsAndStorages->ScGetXmlStream( idView, clsid, pXMLStream );
  4817. if (sc)
  4818. return sc;
  4819. // recheck the pointer
  4820. sc = ScCheckPointers( pXMLStream, E_UNEXPECTED );
  4821. if (sc)
  4822. return sc;
  4823. // save data to stream
  4824. sc = pXMLStream->ScRequestSave( spIPS.GetInterfacePtr() );
  4825. if (sc)
  4826. goto DisplaySnapinError;
  4827. }
  4828. else if ( (spIPSI = pSnapin) != NULL) // QI for IPersistStreamInit
  4829. {
  4830. // get the object for persistence
  4831. CXML_IStream *pXMLStream = NULL;
  4832. sc = pStreamsAndStorages->ScGetXmlStream( idView, clsid, pXMLStream );
  4833. if (sc)
  4834. return sc;
  4835. // recheck the pointer
  4836. sc = ScCheckPointers( pXMLStream, E_UNEXPECTED );
  4837. if (sc)
  4838. return sc;
  4839. // save data to stream
  4840. sc = pXMLStream->ScRequestSave( spIPSI.GetInterfacePtr() );
  4841. if (sc)
  4842. goto DisplaySnapinError;
  4843. }
  4844. else if ( (spIPStg = pSnapin) != NULL) // QI for IPersistStorage
  4845. {
  4846. // get the object for persistence
  4847. CXML_IStorage *pXMLStorage = NULL;
  4848. sc = pStreamsAndStorages->ScGetXmlStorage( idView, clsid, pXMLStorage );
  4849. if (sc)
  4850. return sc;
  4851. // recheck the pointer
  4852. sc = ScCheckPointers( pXMLStorage, E_UNEXPECTED );
  4853. if (sc)
  4854. return sc;
  4855. // save data to storage
  4856. sc = pXMLStorage->ScRequestSave( spIPStg.GetInterfacePtr() );
  4857. if (sc)
  4858. goto DisplaySnapinError;
  4859. }
  4860. return sc;
  4861. // display snapin failure
  4862. DisplaySnapinError:
  4863. // need to inform the world...
  4864. CStr strMessage;
  4865. strMessage.LoadString(GetStringModule(), IDS_SNAPIN_SAVE_FAILED);
  4866. if (pCSnapin != NULL)
  4867. {
  4868. WTL::CString strName;
  4869. if (!pCSnapin->ScGetSnapInName(strName).IsError())
  4870. {
  4871. strMessage += _T("\n");
  4872. strMessage += strName;
  4873. strMessage += _T(".");
  4874. }
  4875. }
  4876. ::MessageBox(NULL, strMessage, _T("Error"), MB_OK | MB_ICONEXCLAMATION);
  4877. return sc;
  4878. }