正确实现处理指向接口的指针的方法

时间:2011-10-04 11:00:25

标签: c++ com vb6

我有一个用C ++实现的COM对象。我在VB6应用程序中使用此对象。

问题是如何实现获取和返回指向接口的指针的方法。 以下是IDL的示例:

[...]
interface ICOMCvDC : IUnknown
{
    HRESULT GetPen([retval][out] ICOMCvPen** ppPen);
    HRESULT SetPen([in] ICOMCvPen* pPen);
};

以下是组件类对象的骨架:

class COMCvDC : public ICOMCvDC
{
public:
    ...
    STDMETHODDECL GetPen(
        /* [retval][out] */ ICOMCvPen** ppPen);
    STDMETHODDECL SetPen(
        /* [in] */ ICOMCvPen* pPen);
    ...

protected:
    ICOMCvPen* m_pen;
};

...

STDMETHODIMP COMCvDC::GetPen(
    /* [retval][out] */ ICOMCvPen** ppPen)
{
    *ppPen = m_pen;
    return S_OK;
}

STDMETHODIMP COMCvDC::SetPen(
    /* [in] */ ICOMCvPen* pPen)
{
    m_pen = pPen;
    return S_OK;
}

我是COM的初学者,所以我不确定我是否以正确的方式做到了。我觉得我需要在某些接口指针上使用QueryInterface方法。另外有趣的是,在解释代码时,VB6正在做什么:

Dim pen1 As ICOMCvPen
Set pen1 = dc1.GetPen()

是否在AddRef方法返回的接口指针上调用GetPen方法?

更新1

我已经实现了两个测试对象(COMCvTest和COMCvTestFactory),它们只记录被调用的所有方法。然后我执行了以下VB6代码:

Dim test1 As ICOMCvTest
Set test1 = New COMCvTest
Debug.Print "Ref: " & test1.GetReferenceCounter
Set test1 = Nothing

以下是这些对象生命周期的日志:

COMCvTestFactory::COMCvTestFactory(); m_cRef = 1
COMCvTestFactory::QueryInterface() --- begin ---
    IID is {00000001-0000-0000-C000-000000000046}
    IID is IID_IClassFactory
    COMCvTestFactory::AddRef(); m_cRef = 2 (was 1)
COMCvTestFactory::QueryInterface() ---- end ----
COMCvTestFactory::Release(); m_cRef = 1 (was 2)
COMCvTestFactory::CreateInstance() --- begin ---
    COMCvTest::COMCvTest(); m_cRef = 1
    COMCvTest::QueryInterface() --- begin ---
        IID is {00000000-0000-0000-C000-000000000046}
        IID is IID_IUnknown
        COMCvTest::AddRef(); m_cRef = 2 (was 1)
    COMCvTest::QueryInterface() ---- end ----
    COMCvTest::Release(); m_cRef = 1 (was 2)
COMCvTestFactory::CreateInstance() ---- end ----
COMCvTest::AddRef(); m_cRef = 2 (was 1)
COMCvTest::Release(); m_cRef = 1 (was 2)
COMCvTestFactory::Release(); m_cRef = 0 (was 1); deleting object
COMCvTestFactory::~COMCvTestFactory()
COMCvTest::QueryInterface() --- begin ---
    IID is {00000000-0000-0000-C000-000000000046}
    IID is IID_IUnknown
    COMCvTest::AddRef(); m_cRef = 2 (was 1)
COMCvTest::QueryInterface() ---- end ----
COMCvTest::QueryInterface() --- begin ---
    IID is {9F660698-1950-4DE8-BB5F-C8D2D61F7367}
    IID is IID_ICOMCvTest
    COMCvTest::AddRef(); m_cRef = 3 (was 2)
COMCvTest::QueryInterface() ---- end ----
COMCvTest::QueryInterface() --- begin ---
    IID is {7FD52380-4E07-101B-AE2D-08002B2EC713}
    IID is IID_IPersistStreamInit
COMCvTest::QueryInterface() --- begin ---
    IID is {37D84F60-42CB-11CE-8135-00AA004BB851}
    IID is IID_IPersistPropertyBag
COMCvTest::Release(); m_cRef = 2 (was 3)
COMCvTest::Release(); m_cRef = 1 (was 2)
COMCvTest::GetReferenceCounter; m_cRef = 1
COMCvTest::Release(); m_cRef = 0 (was 1); deleting object
COMCvTest::~COMCvTest()

似乎VB6正在尝试从COM对象查询IPersistStreamInitIPersistPropertyBag接口。为什么?另外我不明白为什么在查询IUnknown接口指针之前有ICOMCvTest接口的查询?

2 个答案:

答案 0 :(得分:1)

接口指针管理没有什么特别之处,除了一件事:你必须处理AddRef / Release调用。这就是为什么强烈建议使用自动管理的智能指针包装器(C ++伪代码)的原因:

CComPtr<ICOMCvPen> m_pen;

HRESULT Get(IPen** ppPen) { ... *ppPen = CComPtr<ICOMCvPen>(m_pen).Detach();  ... }
HRESULT Set(IPen* pPen) { ... m_pen = pPen;  ... }

就是这样!如果你不使用CComPtr,你必须一直做“if(!x)x-&gt; Release()”的东西,这很容易犯错误。

回复:更新1

    如果IUnknown是VB6的CoCreateInstance中的参数,则可以先查询
  • IID_IUnknown;那么它QueryInterface是感兴趣的界面。
  • IPersistStreamInitIPersistPropertyBag的查询可能是VB6运行时标准对象初始化的一部分;如果该对象应该加载表单,它可以从持久数据初始化。

另请注意,通常在初始化期间从对象中预先查询某些接口,以便在以后需要时准备就绪,尤其是如果要重复使用它们。

答案 1 :(得分:1)

当然,QueryInterface()使它成为一个干净的单线程,利用任何错误处理内置QI并处理你必须添加的引用计数:

STDMETHODIMP COMCvDC::GetPen(ICOMCvPen** ppPen)
{
    if (m_pen) return m_pen->QueryInterface(__uuidof(ICOMCvPen), (void**)ppPen);
    else {
        *ppPen = 0;
        return E_FAIL;
    }
}

STDMETHODIMP COMCvDC::SetPen(ICOMCvPen* pPen)
{
    if (m_pen) m_pen->Release();
    m_pen = pPen;
    return S_OK;
}

不要忘记在构造函数中将m_pen初始化为NULL并在析构函数中释放它。或使用智能指针。