TCPropBagOnRegKey.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef __TCPropBagOnRegKey_h__
  2. #define __TCPropBagOnRegKey_h__
  3. /////////////////////////////////////////////////////////////////////////////
  4. // TCPropBagOnRegKey.h | Declaration of the CTCPropBagOnRegKey class,
  5. // which implements the CLSID_TCPropBagOnRegKey component object.
  6. //
  7. #include "resource.h" // main symbols
  8. #include "SrcInc.h"
  9. #include <TCObj.h>
  10. #include <..\TCLib\ObjectLock.h>
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CTCPropBagOnRegKey
  13. class ATL_NO_VTABLE CTCPropBagOnRegKey :
  14. public IDispatchImpl<ITCPropBagOnRegKey, &IID_ITCPropBagOnRegKey, &LIBID_TCObj>,
  15. public IPropertyBag,
  16. public CComCoClass<CTCPropBagOnRegKey, &CLSID_TCPropBagOnRegKey>,
  17. public CComObjectRoot
  18. {
  19. // Declarations
  20. public:
  21. DECLARE_REGISTRY_RESOURCEID(IDR_TCPropBagOnRegKey)
  22. // Category Map
  23. public:
  24. BEGIN_CATEGORY_MAP(CTCPropBagOnRegKey)
  25. IMPLEMENTED_CATEGORY(CATID_TCObj)
  26. END_CATEGORY_MAP()
  27. // Interface Map
  28. public:
  29. BEGIN_COM_MAP(CTCPropBagOnRegKey)
  30. COM_INTERFACE_ENTRY(ITCPropBagOnRegKey)
  31. COM_INTERFACE_ENTRY(IDispatch)
  32. COM_INTERFACE_ENTRY(IPropertyBag)
  33. END_COM_MAP()
  34. // Construction / Destruction
  35. public:
  36. CTCPropBagOnRegKey();
  37. void FinalRelease();
  38. void Init(HKEY hkey, const _bstr_t& strSubkey,
  39. CTCPropBagOnRegKey* pParent);
  40. protected:
  41. void ClosePrevious();
  42. // Attributes
  43. public:
  44. _bstr_t _GetSubkey();
  45. _bstr_t _GetServer(const _bstr_t& strName);
  46. HRESULT _SetServer(const _bstr_t& strName, const _bstr_t& strServer);
  47. HRESULT _GetObjectCLSID(const _bstr_t& strName, CLSID& clsid);
  48. // Operations
  49. public:
  50. HRESULT _CreateObject(const _bstr_t& strName, IUnknown** ppunkObj,
  51. IErrorLog* pErrorLog = NULL, IUnknown* punkOuter = NULL);
  52. HRESULT _CreateLocalObject(const _bstr_t& strName, IUnknown** ppunkObj,
  53. IErrorLog* pErrorLog = NULL, IUnknown* punkOuter = NULL);
  54. HRESULT _CreateRemoteObject(const _bstr_t& strServer,
  55. const _bstr_t& strName, IUnknown** ppunkObj,
  56. IErrorLog* pErrorLog = NULL);
  57. HRESULT _LoadObject(const _bstr_t& strName, IUnknown* punkObj,
  58. IErrorLog* pErrorLog = NULL);
  59. HRESULT _SaveObject(const _bstr_t& strName, IUnknown* punkObj,
  60. BOOL bClearDirty = FALSE, BOOL bSaveAllProperties = TRUE);
  61. // Implementation
  62. public:
  63. HRESULT _GetObjectCLSID(const _bstr_t& strName, CLSID& clsid,
  64. CRegKey& subkey, _bstr_t& strObjectID, IErrorLog* pErrorLog);
  65. HRESULT _ReadVariantFromValue(CRegKey& key, const _bstr_t& strPropName,
  66. DWORD nType, int cbData, CComVariant& v, IErrorLog* pErrorLog);
  67. HRESULT _LogError(const _bstr_t& strFn, const _bstr_t& strDesc, HRESULT hr,
  68. LPCOLESTR pszPropName, IErrorLog* pErrorLog = NULL);
  69. VARTYPE _GetSubkeyVarType(CRegKey& key, const _bstr_t& strPropName);
  70. bool _IsSubkeySafeArray(CRegKey& key, const _bstr_t& strPropName);
  71. bool _IsSubkeyObject(CRegKey& key, const _bstr_t& strPropName);
  72. HRESULT _ReadSafeArray(CRegKey& key, const _bstr_t& strPropName,
  73. VARIANT* pVar, IErrorLog* pErrorLog);
  74. HRESULT _WriteSafeArray(CRegKey& key, const _bstr_t& strPropName,
  75. VARIANT* pVar);
  76. HRESULT _ReadVariant(CRegKey& key, const _bstr_t& strValueName,
  77. VARIANT* pVar, IErrorLog* pErrorLog);
  78. HRESULT _WriteVariant(CRegKey& key, const _bstr_t& strValueName,
  79. VARIANT* pVar);
  80. VARTYPE _VarTypeFromUnknown(IUnknown* punk);
  81. static bool KeyExists(HKEY hkey, const _bstr_t& strValueName);
  82. static LONG QueryString(HKEY hkey, const _bstr_t& strValueName,
  83. _bstr_t& strOut);
  84. // ITCPropBagOnRegKey Interface Methods
  85. public:
  86. STDMETHODIMP CreateKey(BSTR bstrSubKey, VARIANT_BOOL bReadOnly);
  87. STDMETHODIMP OpenKey(BSTR bstrSubKey, VARIANT_BOOL bReadOnly);
  88. STDMETHODIMP CreateObject(IUnknown** ppunkObj);
  89. STDMETHODIMP CreateLocalObject(IUnknown** ppunkObj);
  90. STDMETHODIMP CreateRemoteObject(BSTR bstrServer, IUnknown** ppunkObj);
  91. STDMETHODIMP LoadObject(IUnknown* punkObj);
  92. STDMETHODIMP SaveObject(IUnknown* punkObj, VARIANT_BOOL bClearDirty,
  93. VARIANT_BOOL bSaveAllProperties, VARIANT_BOOL bSaveCreationInfo);
  94. STDMETHODIMP put_Server(BSTR bstrServer);
  95. STDMETHODIMP get_Server(BSTR* pbstrServer);
  96. STDMETHODIMP get_ObjectCLSID(BSTR* pbstrCLSID);
  97. // IPropertyBag Interface Methods
  98. public:
  99. STDMETHODIMP Read(LPCOLESTR pszPropName, VARIANT* pVar,
  100. IErrorLog* pErrorLog);
  101. STDMETHODIMP Write(LPCOLESTR pszPropName, VARIANT* pVar);
  102. // Types
  103. protected:
  104. typedef TCObjectLock<CTCPropBagOnRegKey> XLock;
  105. // Data Members
  106. protected:
  107. // Data Members
  108. protected:
  109. _bstr_t m_strSubkey;
  110. CTCPropBagOnRegKey* m_pParent;
  111. CRegKey m_key;
  112. bool m_bOwnKey;
  113. // Constants
  114. protected:
  115. const static _bstr_t m_szVariantType;
  116. const static _bstr_t m_szServer;
  117. const static _bstr_t m_szProgID;
  118. const static _bstr_t m_szCLSID;
  119. const static _bstr_t m_szLowerBound;
  120. const static _bstr_t m_szElementCount;
  121. const static _bstr_t m_szElementFmt;
  122. const static USHORT m_wChangeTypeFlags;
  123. };
  124. /////////////////////////////////////////////////////////////////////////////
  125. // Construction / Destruction
  126. inline void CTCPropBagOnRegKey::Init(HKEY hkey, const _bstr_t& strSubkey,
  127. CTCPropBagOnRegKey* pParent)
  128. {
  129. m_key.Attach(hkey);
  130. m_strSubkey = strSubkey;
  131. m_pParent = pParent;
  132. m_bOwnKey = false;
  133. }
  134. inline void CTCPropBagOnRegKey::ClosePrevious()
  135. {
  136. // Detach from the current key if we don't own it
  137. XLock lock(this);
  138. if (!m_bOwnKey)
  139. m_key.Detach();
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. #endif // !__TCPropBagOnRegKey_h__