0

I'm trying to implement a collections of objects via VC6 that can be accessed by a VB6 programs. I haven't been able to make it come together.

I have defined _CopyVariantFromAdaptItf and _CopyItfFromAdaptItf (from "ATL internals"). Here are my defines to be used in my collections definitions:

#define CComEnumVariantOnListOfItemInterface(ItemInterface) \
    CComEnumOnSTL<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT,_CopyVariantFromAdaptItf<ItemInterface>,vector< CAdapt< CComPtr<ItemInterface> > > >

#define ClassInterfaceCollImpl(ContainerInterface,ItemInterface) \
    ICollectionOnSTLImpl< IDispatchImpl< ContainerInterface, &__uuidof(ContainerInterface) >,\
    vector< CAdapt< CComPtr<ItemInterface> > >,ItemInterface*,_CopyItfFromAdaptItf<ItemInterface>,\
    CComEnumVariantOnListOfItemInterface(ItemInterface) >

#define ContainerInterface      IFields
#define ContainerClass          CFields
#define ContainerClsid          CLSID_Fields
#define ItemInterface           IField
#define DllRegID                IDR_FIELDS

typedef std::vector< CAdapt< CComPtr<ItemInterface> > > ContainerType;

and this the preamble for the container class:

class ATL_NO_VTABLE ContainerClass : 
    public ClassInterfaceCollImpl(ContainerInterface,ItemInterface),
    public ISupportErrorInfo,
    public CComObjectRoot,
    public CComCoClass<ContainerClass,&ContainerClsid>
{
public:
    ContainerClass();
BEGIN_COM_MAP(ContainerClass)
    COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(ContainerInterface)
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
//DECLARE_NOT_AGGREGATABLE(CFields) 
// Remove the comment from the line above if you don't want your object to 
// support aggregation. 

DECLARE_REGISTRY_RESOURCEID(DllRegID)
// ISupportsErrorInfo
    STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IFields
private:
    int GetIndex( BSTR name );

etc....

and in my attempted implementation, I'm trying to scan the included objects for a matching name that is accessed in the object's function pName(). Note that I'm showing two different approaches that I have tried:

int CFields::GetIndex( BSTR name )
{

    long count = 0;
#if (1)
    Count(&count);
    for (unsigned int i=0; i<count; i++)
        if (wcscmp( name, m_coll.m_T->pName())
            return (i);
#else
    std::vector<CAdapt<CComPtr<IField> > >::iterator itr;
        for (itr = m_coll.begin(); itr != m_coll.end(); itr++)
            if (wcscmp( name, itr->m_T->pName()))
                count++;
            else
                return (count);

#endif
    throw E_INVALIDARG_13;
}

If I try the #if (1), I get an error that m_T is not a member of "vector<...". If I try m_coll->m_T, I get an error that "vector<..." does not have overloaded ->, which I would expect. If I try the #if (0), I get an error that pName() is not a member of '_NoAddRefReleaseOnCComPtr

Thanks in advance for any help you can give me!

Vance

4

1 回答 1

0

我知道我是在自言自语,但我希望能帮助别人,以防我偶然发现一个正确的答案。

好的,我有一个工作系统,并且基于对另一个问题的响应如何从 ATL 对象集合中检索对象指针?,这就是我最终的结果。我添加了这一行:

COM_INTERFACE_ENTRY_IID(CLSID_Fields, CFields)

在 BEGIN_COM_MAP(CFields) 和 END_COM_MAP() 行之间,以便 getItemObjectPtr 可以工作。

我将 ItemInterface 改回 IField 并将 GetIndex 方法更改为:

    long count = 0;
    ContainerType::iterator itr;
for (itr = m_coll.begin(); itr != m_coll.end(); itr++)
    if (wcscmp( name, getItemObjectPtr( itr->m_T )->pName()) == 0)
        count++;
    else
        return (count);
    throw (HRESULT)E_INVALIDARG_13;

并且方法 getItemObjectPtr 定义为:

CFields * CrecordSet::getItemObjectPtr( IFields *ppFields )
{
    CComQIPtr<CFields, &CLSID_Fields> pFields = ppFields;
    return (pFields);
}
于 2013-05-14T16:23:02.967 回答