【问题标题】:COM function returns E_POINTERCOM 函数返回 E_POINTER
【发布时间】:2013-11-07 05:29:38
【问题描述】:

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

【问题讨论】:

  • 你会得到E_POINTER,因为你很幸运。 inputTagInterface 是一个未初始化的指针变量。这与 COM 完全无关——这是非常基本的 C++。 COM 返回一个错误代码(如果可以的话)。 C++ 崩溃(除非它不能)。
  • @IInspectable 感谢您的回复您的意思是说我传递的参数没有初始化?
  • @IInspectable 那么如何初始化参数呢?
  • 看起来该参数未在 put_InputCollectionInterface 中使用,您是如何获得 InternalCollection 对象的?我怀疑后者是问题,而不是 inputTagInterface

标签: c++ com


【解决方案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 方法,我想您在未引用的代码上还有其他问题。

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2021-04-14
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2011-09-14
    • 2013-06-28
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多