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

2

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.

c++
ms-media-foundation
asked on Stack Overflow May 28, 2015 by Calvin Hu • edited May 29, 2015 by Calvin Hu

1 Answer

3

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

answered on Stack Overflow May 29, 2015 by peterchen

User contributions licensed under CC BY-SA 3.0