作为新手,学习 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
?
最佳答案
“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
方法,我想您在未引用的代码上还有其他问题。
关于c++ - COM 函数返回 E_POINTER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19222159/