msdn上好像说IPropertyBag要在客户端实现,而IPersistPropertyBag要在com里实现.
后者我用IPersistPropertyBagImpl模板实现了,可是前者我该怎么做啊.我的目的是客户端共享一个对象,要保存对象的某些值,在对象创建时重新获得这些状态,谁有过这样的经验,谢谢大虾了

解决方案 »

  1.   

    // This ATL object provides a clean, simple implementation of IPropertyBag,
    plus
    // a non-com introspection method (similar to IPropertyBag2's).// This is just an ATL class that exposes a COM interface -- it is not
    // a proper COMponent coclass.  You should create it like so://    CComObject<CStdPropBag>* pBag = 0;
    //    CComObject<CStdPropBag>::CreateInstance(&pBag); // refcnt == 0
    // Optionally to be followed by something like this://    CComPtr<IPropertyBag> spBag = pBag; // refcnt == 1// NOTE 1:  This implementation of IPropertyBag::Read() does not perform
    // variant type-coersion, as discussed in the MS docs, because it is a
    // silly thing to do, imho.// NOTE 2:  This implementation also exposes a slightly simplified, VB-
    // friendly version of IPropertyBag, which I call IPropertyBagVb.#pragma once#include <ocidl.h> // IPropertyBagnamespace ATLX {struct __declspec(uuid("0075a082-4b64-11d3-9ba3-00e02905d02b"))
       IPropertyBagVb : IUnknown
       {
       virtual HRESULT __stdcall
       Read(BSTR s, VARIANT* pv) = 0;   virtual HRESULT __stdcall
       Write(BSTR s, VARIANT v) = 0;
       };class ATL_NO_VTABLE CStdPropBag :
       public CComObjectRootEx<CComSingleThreadModel>,
       public IPropertyBag,
       public IPropertyBagVb
    {
    private:   // Declare CStdProp as a nested class, with
       // public members for simplicity
       struct CStdProp
       {
       CComBSTR name;
       CComVariant value;   bool operator==(const CStdProp& that)
          { return (name == that.name); }
       };   // Vector of properties
       CSimpleArray<CStdProp> m_aProps;public:// Non-COM public methodsCStdPropBag()
       {
       ///ATLTRACE(">>>CStdPropBag\n");
       }~CStdPropBag()
       {
       ///ATLTRACE("CStdPropBag<<<\n");
       }void Clear()
       {
       m_aProps.RemoveAll();
       }bool GetPropInfo(int idx, BSTR* pName, VARIANT* pValue)
       {
       if (idx < 0) return false;
       if (idx >= m_aProps.GetSize()) return false;   if (!pName) return false;
       else (*pName) = 0;   if (!pValue) return false;
       else VariantClear(pValue);   (*pName) = m_aProps[idx].name.Copy();
       VariantCopy(pValue,&(m_aProps[idx].value));   return true;
       }BEGIN_COM_MAP(CStdPropBag)
       COM_INTERFACE_ENTRY(IPropertyBag)
       COM_INTERFACE_ENTRY(IPropertyBagVb)
    END_COM_MAP()// IPropertyBagSTDMETHODIMP Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrLog)
       {
       if (!pszPropName) return E_INVALIDARG;   if (!pVar) return E_POINTER;
       else VariantClear(pVar);   // Try to find an existing prop by that name
       CStdProp x;
       x.name = pszPropName;   int i = m_aProps.Find(x);
       if (i < 0) return E_INVALIDARG; // prop name not found   // Return a copy of it
       VariantCopy(pVar,&(m_aProps[i].value));
       return S_OK;
       }STDMETHODIMP Write(LPCOLESTR pszPropName, VARIANT* pVar)
       {
       if (!pszPropName) return E_INVALIDARG;
       if (!pVar) return E_INVALIDARG;   // Erase any existing props by that name
       CStdProp x;
       x.name = pszPropName;   m_aProps.Remove(x);   // Add the new property
       VariantCopy(&(x.value),pVar);   m_aProps.Add(x);   return S_OK;
       }// IPropertyBagVbSTDMETHODIMP Read(BSTR s, VARIANT* pv)
       {
       return Read(s,pv,0);
       }STDMETHODIMP Write(BSTR s, VARIANT v)
       {
       return Write(s,&v);
       }};}; // namespace