作为新手,学习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
?
答案 0 :(得分: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
方法,我认为您在未引用的代码上存在其他问题。