c++ - COM 函数返回 E_POINTER

标签 c++ com

作为新手,学习 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/

相关文章:

C++ IPC 替代本地 COM 接口(interface)?

c++ - 本地分配的内存可以用于将来使用吗?

c++ - 将输入流从 PortAudio 馈送到 webrtc::AudioProcessing

c++ - 是否允许在模板特化时从 int 转换为 long (此代码应该编译)?

delphi - 通过Delphi Olevariant类型获取COM对象的成员

c# - 收到错误 "The ' VFPOLEDB。 1' provider is not registered on the local machine"即使在安装和注册提供商之后

c# - 如何创建向 COM 公开的只读属性?

c++ - 在具有专门化程序的模板类上正确使用 const 返回值

c++ - 我可以从 CUDA 内核函数调用 __device__ 函数吗?

c++ - Microsoft Word 连接点接收器示例/MFC 出现问题