Handling CoCreateInstance return value

五迷三道 提交于 2019-12-19 23:26:28

问题


Here's a code sample creating a COM object:

CComPtr<IBaseFilter> pFilter;
auto hr = CoCreateInstance(CLSID_DMOWrapperFilter, NULL,
    CLSCTX_INPROC_SERVER, IID_IBaseFilter, reinterpret_cast<void**>(&pFilter));

I've seen somewhere that checking if CoCreateInstance() succeeded should look like this:

if (SUCCEEDED(hr) && pFilter != nullptr)
{
  // code goes here
}

What if I would check only hr? Wouldn't it be enough? Should I also check that filter != nullptr?

//would this be enough?
if (SUCCEEDED(hr))
{
  // code goes here
}

This question also concerns other COM methods like QueryInterface().


回答1:


Having S_OK result from CoCreateInstance you are guaranteed to get a non-NULL interface pointer, so you don't need to check it additionally. To make it more reliable and be able to detect issues early, you might want to use ATLASSERT there to compare against NULL. This does not produce code in release builds, but generates an early warning in debug if anything goes wrong (esp. you edit or copy paste code later and change the logic of obtaining the pointer.

CComPtr<IBaseFilter> pFilter;
HRESULT hr = CoCreateInstance(CLSID_DMOWrapperFilter, NULL, CLSCTX_INPROC_SERVER,
  IID_IBaseFilter, reinterpret_cast<VOID**>(&pFilter));
if(SUCCEEDED(hr))
{
  ATLASSERT(pFilter); // hr is of the interest because might explain failure
                      // pFilter is expected to be non-NULL in case of S_OK
  const CComQIPtr<IDMOWrapperFilter> pDmoWrapperFilter = pFilter;
  if(pDmoWrapperFilter)
  {
    // We're not really interested in QueryInterface's HRESULT since it's
    // either S_OK or E_NOINTERFACE, hr will typically explain nothing special.
    // More important is whether we finally obtained the pointer or not
  }
}



回答2:


I think it is redundant and not needed to check both.

If it does fail though, the return value will tell you which error occurred. That's why there's 2 ways of determining if the function succeeded or not.



来源:https://stackoverflow.com/questions/18997021/handling-cocreateinstance-return-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!