0

作为新手,学习 COM 概念非常困难。请向我解释以下错误。为什么会发生这种情况我有带有以下函数体的 com 代码。

STDMETHODIMP CCollectionBase::put_InputCollectionInterface(
    IUnknown *InputTagInterface)
{
    ICollection* inputCollectionInterface;
    HRESULT hr = QueryInterface(__uuidof(ICollection),
        (void**)&inputCollectionInterface);
    if (FAILED(hr)) return hr;

    //m_inputCollectionInterface  is global variable of ICollection    
    m_inputCollectionInterface = inputCollectionInterface;
    return S_OK;
}

我正在以下列方式调用函数。

ITag* inputTagInterface;
//InternalCollection is ICollectionBase object
hr = InternalCollection->put_InputCollectionInterface(inputTagInterface);

但我得到的是E_POINTER。为什么呢E_POINTER

4

1 回答 1

1

“Garbage In, Garbage Out”,你将一个随机指针传递给函数,你在里面做了一个错误的调用,所以期待奇怪的事情回来。

不正确的事情是:

STDMETHODIMP CCollectionBase::put_InputCollectionInterface(
    IUnknown *InputTagInterface)
{
    ICollection* inputCollectionInterface;
    // 1. You are calling QueryInterface() on the wrong object,
    //    most likely you were going to query the interface of
    //    interest of the argument pointer
    if (!InputTagInterface) return E_NOINTERFACE;
    HRESULT hr = InputTagInterface->QueryInterface(
          __uuidof(ICollection), (void**)&inputCollectionInterface);
    if (FAILED(hr)) return hr;

    //m_inputCollectionInterface  is global variable of ICollection
    m_inputCollectionInterface = inputCollectionInterface;
    return S_OK;
}

ITag* inputTagInterface;
// 2. You need to initialize the value here to make sure you are passing
//    valid non-NULL argument below
hr = InternalCollection->put_InputCollectionInterface(inputTagInterface);

由于您E_POINTER来自CCollectionBase::QueryInterface方法,我想您在未引用的代码上还有其他问题。

于 2013-10-07T11:03:07.543 回答