这是一个使用该函数的非常简单的程序:
#include <windows.h>
#include <tchar.h>
#include <atlstr.h>
#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <functiondiscoverykeys.h>
#include <iostream>
using namespace std;
int main()
{
HRESULT hr;
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
pMMDeviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
//cout << hr;
return 0;
}
当我尝试运行它时,我收到以下错误:
Debug Assertion Failed!
Program: ...
File: c:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h
Line: 154
Expression: p!=0
这有什么问题?我现在正在尝试学习如何使用此功能。谢谢!
编辑:
我已将程序更改为:
//#include <windows.h>
//#include <tchar.h>
#include <atlstr.h>
#include <mmdeviceapi.h>
//#include <devicetopology.h>
//#include <functiondiscoverykeys.h>
#include <iostream>
using namespace std;
// helper class to CoInitialize/CoUninitialize
class CCoInitialize {
private:
HRESULT m_hr;
public:
CCoInitialize(PVOID pReserved, HRESULT &hr)
: m_hr(E_UNEXPECTED) { hr = m_hr = CoInitialize(pReserved); }
~CCoInitialize() { if (SUCCEEDED(m_hr)) { CoUninitialize(); } }
};
int main()
{
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
HRESULT hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (FAILED(hr)) {
cout << "failed" << endl;
return __LINE__;
}
CCoInitialize ci(NULL, hr);
pMMDeviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
//cout << hr;
return 0;
}
当我运行它时,我得到“失败”的输出。发生了什么?
编辑:
好的,现在我已经对代码进行了足够的更改,让它一直运行而不会出现任何故障。IE,
HRESULT hr = S_OK;
cout << hr;
// initialize COM
CCoInitialize ci(NULL, hr);
if (FAILED(hr)) {
cout << "failed1" << endl;
return __LINE__;
}
cout << hr;
// get enumerator
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (FAILED(hr)) {
cout << "failed2" << endl;
return __LINE__;
}
cout << hr;
// get default render/capture endpoints
CComPtr<IMMDevice> pRenderEndpoint;
hr = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pRenderEndpoint);
if (FAILED(hr)) {
cout << "failed3" << endl;
return __LINE__;
}
cout << hr;
return 0;
我之前在这个例子中遇到的一些问题(见答案的评论)只是通过删除一些代码来解决的。但是当我运行 main() 函数的这个新主体时,我得到了输出“0000”,这意味着 cout << hr 总是计算为“0”。这是一件好事吗?我现在可以获得关于默认设备的哪些信息?小时。和 hr-> 并没有真正调出任何菜单,所以我有点不知所措。谢谢!