Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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