Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB

  1. // CreateRegBag.h : Declaration of the CCreateRegBag
  2. #ifndef __CREATEREGBAG_H_
  3. #define __CREATEREGBAG_H_
  4. #include <comcat.h>
  5. #include <objsafe.h>
  6. #include <propertybag2impl.h>
  7. #include <objectwithsiteimplsec.h>
  8. #include <throw.h>
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CRegBagBase
  11. class ATL_NO_VTABLE CRegBagBase : public CComObjectRootEx<CComMultiThreadModel>,
  12. public IPropertyBag,
  13. public IPropertyBag2Impl<CRegBagBase>,
  14. public IObjectWithSiteImplSec<CRegBagBase>
  15. {
  16. BEGIN_COM_MAP(CRegBagBase)
  17. COM_INTERFACE_ENTRY(IPropertyBag2)
  18. COM_INTERFACE_ENTRY(IPropertyBag)
  19. COM_INTERFACE_ENTRY(IObjectWithSite)
  20. END_COM_MAP_WITH_FTM()
  21. CRegBagBase() {}
  22. virtual ~CRegBagBase() {}
  23. // IPropertyBag
  24. STDMETHOD(Read)(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog *pErrorLog);
  25. STDMETHOD(Write)(LPCOLESTR pszPropName, VARIANT *pVar);
  26. protected:
  27. // internal utility functions
  28. HRESULT DeleteSubKey(CRegKey& hk, LPCOLESTR pszPropName);
  29. HRESULT DeleteValue(CRegKey& hk, LPCOLESTR pszPropName);
  30. HRESULT SaveObject(CRegKey& hk, LPCOLESTR pszPropName, VARIANT* pV);
  31. HRESULT RegConvertToVARIANT(VARIANT *pVar, DWORD dwType, LPBYTE pbData, DWORD cbSize);
  32. CRegKey m_hk;
  33. };
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CRegBag
  36. class CRegBag :
  37. public CComObject<CRegBagBase>
  38. {
  39. public:
  40. CRegBag(HKEY hk, LPCTSTR subkey, DWORD ulOptions, REGSAM samDesired) {
  41. DWORD rc = m_hk.Open(hk, subkey, samDesired, ulOptions);
  42. if (rc != ERROR_SUCCESS) {
  43. THROWCOM(HRESULT_FROM_WIN32(rc));
  44. }
  45. }
  46. virtual ~CRegBag() {}
  47. // IPropertyBag2
  48. STDMETHOD(CountProperties)(ULONG * pcProperties);
  49. STDMETHOD(GetPropertyInfo)(ULONG iProperty, ULONG cProperties, PROPBAG2 * pPropBag, ULONG * pcProperties);
  50. STDMETHOD(LoadObject)(LPCOLESTR pstrName, ULONG dwHint, IUnknown * pUnkObject, IErrorLog * pErrLog);
  51. };
  52. template<class BAGTYPE, class PERSISTTYPE> HRESULT LoadPersistedObject(PERSISTTYPE pPersistObj,
  53. CLSID& clsid,
  54. VARIANT* pVar,
  55. HKEY hk,
  56. LPCOLESTR pszPropName,
  57. IErrorLog *pErrorLog) {
  58. ASSERT(pVar && hk && pszPropName);
  59. HRESULT hr = NOERROR;
  60. if (!pPersistObj && ((pVar->vt == VT_UNKNOWN) ? pVar->punkVal : pVar->pdispVal)) {
  61. // got passed an object to initialize and the object doesn't support persist
  62. return E_NOINTERFACE;
  63. } else if (!pPersistObj) {
  64. hr = pPersistObj.CoCreateInstance(clsid);
  65. if (FAILED(hr)) {
  66. // we ignore subkeys if they don't look like objects
  67. // but if they have a default value that successfully converts to a guid
  68. // and if that object can't be created then its an error
  69. return hr;
  70. }
  71. hr = pPersistObj->InitNew();
  72. if (FAILED(hr) && hr != E_NOTIMPL) {
  73. return hr;
  74. }
  75. ASSERT(pVar->vt == VT_UNKNOWN || pVar->vt == VT_DISPATCH);
  76. switch(pVar->vt) {
  77. case VT_DISPATCH:
  78. pVar->pdispVal = PQDispatch(pPersistObj);
  79. if (pVar->pdispVal) {
  80. pVar->pdispVal->AddRef();
  81. break;
  82. } else {
  83. pVar->vt = VT_UNKNOWN;
  84. }
  85. // fall thru
  86. case VT_UNKNOWN:
  87. pVar->punkVal = pPersistObj;
  88. (pVar->punkVal)->AddRef();
  89. }
  90. }
  91. USES_CONVERSION;
  92. BAGTYPE pBag(new CRegBag(hk, OLE2CT(pszPropName), 0, KEY_READ));
  93. if (!pBag) {
  94. return E_OUTOFMEMORY;
  95. }
  96. hr = pPersistObj->Load(pBag, pErrorLog);
  97. return hr;
  98. };
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CCreateRegBag
  101. class ATL_NO_VTABLE CCreateRegBag :
  102. public CComObjectRootEx<CComMultiThreadModel>,
  103. public CComCoClass<CCreateRegBag, &CLSID_CreatePropBagOnRegKey>,
  104. public IObjectWithSiteImplSec<CCreateRegBag>,
  105. public ICreatePropBagOnRegKey
  106. {
  107. public:
  108. CCreateRegBag()
  109. {
  110. }
  111. REGISTER_NONAUTOMATION_OBJECT_WITH_TM(IDS_PROJNAME,
  112. IDS_REG_CREATEREGBAG_DESC,
  113. LIBID_MSVidCtlLib,
  114. CLSID_CreatePropBagOnRegKey, tvBoth);
  115. DECLARE_PROTECT_FINAL_CONSTRUCT()
  116. BEGIN_COM_MAP(CCreateRegBag)
  117. COM_INTERFACE_ENTRY(ICreatePropBagOnRegKey)
  118. COM_INTERFACE_ENTRY(IObjectWithSite)
  119. END_COM_MAP_WITH_FTM()
  120. BEGIN_CATEGORY_MAP(CVidCtl)
  121. IMPLEMENTED_CATEGORY(CATID_PersistsToPropertyBag)
  122. END_CATEGORY_MAP()
  123. // ICreateRegBag
  124. public:
  125. // ICreatePropBagOnRegKey
  126. STDMETHOD(Create)(HKEY hkey, LPCOLESTR subkey, DWORD options, DWORD sam, REFIID iid, LPVOID* ppBag);
  127. };
  128. #endif //__CREATEREGBAG_H_