描述:
大家好:
我想创建一个Com 对象 它可以存储其它Com对象,也就是这个Com对象是另外一个Com 对象的集合。 例如 IPoint 是一个点对象, IPoints 是一个点集合对象具有Add(LPDISPATCH point) RemoveItem(short Index ) short GetCount()
LPDISPATCH Item( short Index ) 等方法,来操作点对象,点对象在客户端
创建后作为参数传给方法。有朋友做过这方面的例子吗?
下面是我写的程序,但是总是出现内存引用错误,大家帮分析一下
interface IGraphGroupItem : IDispatch
{
[propget, id(1), helpstring("property ItemColor")] HRESULT ItemColor([out, retval] OLE_COLOR *pVal);
[propput, id(1), helpstring("property ItemColor")] HRESULT ItemColor([in] OLE_COLOR newVal);
[propget, id(2), helpstring("property ItemSymbolStyle")] HRESULT ItemSymbolStyle([out, retval] short* pVal);
[propput, id(2), helpstring("property ItemSymbolStyle")] HRESULT ItemSymbolStyle([in] short newVal);
[id(3), helpstring("method GetItemValueAsString")]
};
interface IGraphGroup : IDispatch
{
[id(1), helpstring("method AddItem")]
HRESULT AddItem([in]IGraphGroupItem* lpItem);
[id(2), helpstring("RemoveItem")]
HRESULT RemoveItem([in]short pValue);
[id(3), helpstring("method GetItem")]
HRESULT GetItem([in]short sIndex , [out,retval]IGraphGroupItem** lpItem);
[id(4), helpstring("GetCount")]
HRESULT GetCount([out,retval]short* pValue);
};
#if !defined(AFX_GRAPHGROUP_H__72C58F15_9053_43DA_BB48_843CC67257B1__INCLUDED_)
#define AFX_GRAPHGROUP_H__72C58F15_9053_43DA_BB48_843CC67257B1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// GraphGroup.h : header file
//
#include "GraphGroupItem.h"
/////////////////////////////////////////////////////////////////////////////
// CGraphGroup command target
class CGraphGroup : public CCmdTarget
{
DECLARE_DYNCREATE(CGraphGroup)
DECLARE_OLECREATE(CGraphGroup)
CGraphGroup(); // protected constructor used by dynamic creation
CGraphGroup(const CGraphGroup& group);
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGraphGroup)
public:
virtual void OnFinalRelease();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CGraphGroup();
void Clear();
protected:
// Generated message map functions
//{{AFX_MSG(CGraphGroup)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(CGraphGroup)
afx_msg void AddItem(LPDISPATCH lpItem);
afx_msg void RemoveItem(short sItem);
afx_msg LPDISPATCH GetItem(short sIndex);
afx_msg short GetCount();
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
private:
CArray<CGraphGroupItem*,CGraphGroupItem*> m_Group;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_GRAPHGROUP_H__72C58F15_9053_43DA_BB48_843CC67257B1__INCLUDED_)
IMPLEMENT_DYNCREATE(CGraphGroup, CCmdTarget)
CGraphGroup::CGraphGroup()
{
EnableAutomation();
m_TitleVisible = TRUE;
m_GroupTitleText = _T("");
}
CGraphGroup::CGraphGroup(const CGraphGroup& group)
{
CGraphGroupItem *pItem;
CGraphGroupItem *pNewItem;
EnableAutomation();
m_TitleVisible = group.m_TitleVisible;
m_GroupTitleText = group.m_GroupTitleText;
for ( short i = 0 ; i < group.m_Group.GetSize() ; i++ )
{
pItem = group.m_Group.GetAt(i);
pNewItem = new CGraphGroupItem( *pItem );
m_Group.Add(pNewItem);
}
}
CGraphGroup::~CGraphGroup()
{
Clear();
}
void CGraphGroup::OnFinalRelease()
{
// When the last reference for an automation object is released
// OnFinalRelease is called. The base class will automatically
// deletes the object. Add additional cleanup required for your
// object before calling the base class.
CCmdTarget::OnFinalRelease();
}
BEGIN_MESSAGE_MAP(CGraphGroup, CCmdTarget)
//{{AFX_MSG_MAP(CGraphGroup)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CGraphGroup, CCmdTarget)
//{{AFX_DISPATCH_MAP(CGraphGroup)
DISP_PROPERTY_EX(CGraphGroup, "GroupTitle", GetGroupTitle, SetGroupTitle, VT_BSTR)
DISP_PROPERTY_EX(CGraphGroup, "TitleVisible", GetTitleVisible, SetTitleVisible, VT_BOOL)
DISP_FUNCTION(CGraphGroup, "GetMaxValue", GetMaxValue, VT_BOOL, VTS_PVARIANT)
DISP_FUNCTION(CGraphGroup, "GetTotalValue", GetTotalValue, VT_BOOL, VTS_PR8 VTS_PI4)
DISP_FUNCTION(CGraphGroup, "GetTitleTextLength", GetTitleTextLength, VT_I2, VTS_NONE)
DISP_FUNCTION(CGraphGroup, "AddItem", AddItem, VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION(CGraphGroup, "RemoveItem", RemoveItem, VT_EMPTY, VTS_I2)
DISP_FUNCTION(CGraphGroup, "GetItem", GetItem, VT_DISPATCH, VTS_I2)
DISP_FUNCTION(CGraphGroup, "GetCount", GetCount, VT_I2, VTS_NONE)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
// Note: we add support for IID_IGraphGroup to support typesafe binding
// from VBA. This IID must match the GUID that is attached to the
// dispinterface in the .ODL file.
// {1DDBA0A3-7542-4D29-BD9B-9F730