IMFActivate :: ActivateObject返回错误代码“尚未调用CoInitialize". [英] IMFActivate::ActivateObject return error code "CoInitialize has not been called."

查看:55
本文介绍了IMFActivate :: ActivateObject返回错误代码“尚未调用CoInitialize".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Studio 2013中编写一个简单的多媒体应用程序,我需要枚举连接到计算机的摄像头设备并创建媒体源对象以链接到其中一个.我使用Media Foundation SDK并尝试在此处运行该指南: https://msdn.microsoft.com/zh-CN/library/windows/desktop/dd940326(v = vs.85).aspx :

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;
}

问题在于这部分代码:

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

无法创建媒体源对象(返回的HRESULT为0x800401F0(CO_E_NOTINITIALIZED)—未调用CoInitialize.).错误代码是什么意思,什么可能是导致失败的问题?我正在使用WIN8.1.

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.

推荐答案

Com库需要通过任一线程初始化每个线程

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.

对于所有使用COM的线程,请在程序开始时执行此操作,不要忘记调用各自的Uninitialize函数

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

这篇关于IMFActivate :: ActivateObject返回错误代码“尚未调用CoInitialize".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆