IPropertyBag和IPersistPropertyBag的问题

waster2000 2003-08-20 10:08:35
msdn上好像说IPropertyBag要在客户端实现,而IPersistPropertyBag要在com里实现.
后者我用IPersistPropertyBagImpl模板实现了,可是前者我该怎么做啊.我的目的是客户端共享一个对象,要保存对象的某些值,在对象创建时重新获得这些状态,谁有过这样的经验,谢谢大虾了
...全文
200 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
brave_heart 2003-08-20
  • 打赏
  • 举报
回复
// 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> // IPropertyBag

namespace 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 methods

CStdPropBag()
{
///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()

// IPropertyBag

STDMETHODIMP 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;
}

// IPropertyBagVb

STDMETHODIMP Read(BSTR s, VARIANT* pv)
{
return Read(s,pv,0);
}

STDMETHODIMP Write(BSTR s, VARIANT v)
{
return Write(s,&v);
}

};

}; // namespace

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧