IMFActivate::ActivateObject return error code “CoInitialize has not been called.”

萝らか妹 提交于 2019-12-20 03:07:56

问题


I'm writing a simple multimedia application in Visual Studio 2013 and I need to enumerate camera devices connected to my computer and create a media source object to link to one of them. I use Media Foundation SDK and tried to run the guide here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd940326(v=vs.85).aspx :

#include <Mfapi.h>
#include <mfidl.h>
#include <mfobjects.h>
#include <iostream>

#pragma comment(lib, "Mfplat")
#pragma comment(lib, "Mf")

template <class T> void SafeRelease(T **ppT) {
  if (*ppT) {
    (*ppT)->Release();
    *ppT = NULL;
  }
}

HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource) {
  *ppSource = NULL;

  IMFMediaSource *pSource = NULL;
  IMFAttributes *pAttributes = NULL;
  IMFActivate **ppDevices = NULL;

  // Create an attribute store to specify the enumeration parameters.
  HRESULT hr = MFCreateAttributes(&pAttributes, 1);
  if (FAILED(hr))
  {
    goto done;
  }

  // Source type: video capture devices
  hr = pAttributes->SetGUID(
    MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
    MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID
  );
  if (FAILED(hr))
  {
    goto done;
  }

  // Enumerate devices.
  UINT32 count;
  hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
  if (FAILED(hr))
  {
    goto done;
  }

  if (count == 0)
  {
    hr = E_FAIL;
    goto done;
  }

  // Create the media source object.
  hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
  if (FAILED(hr))
  {
    std::cout << "Failed to create device object" << hr <<std::endl;
    goto done;
  }

  *ppSource = pSource;
  (*ppSource)->AddRef();

  DWORD chs;
  (*ppSource)->GetCharacteristics(&chs);
  std::cout << chs << std::endl;

 done:
   SafeRelease(&pAttributes);

   for (DWORD i = 0; i < count; i++)
   {
     SafeRelease(&ppDevices[i]);
   }
   CoTaskMemFree(ppDevices);
   SafeRelease(&pSource);
   return hr;
  }

int main(int argc, char* argv[]) {
  IMFMediaSource* ppSource;
  CreateVideoDeviceSource(&ppSource);
  std::cout << "END" << std::endl;

  return 0;
}

The problem is that this part of the code :

 // Create the media source object.
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
{
  goto done;
}

fails to create a media source object(the HRESULT returned is 0x800401F0 (CO_E_NOTINITIALIZED)--"CoInitialize has not been called. "). What does the error code mean and What could be the problem causing the failure ? I'm using WIN8.1.


回答1:


Com Libraries need to be initialized for each thread, through either of

  • CoInitialize
  • CoInitializeEx
  • OleInitialize

depending on which services are to be used in this thread.

Do this at the start of your program, for all threads that use COM, and don't forget to call the respective Uninitialize function



来源:https://stackoverflow.com/questions/30518220/imfactivateactivateobject-return-error-code-coinitialize-has-not-been-called

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