如何通过 MMDevice API 使用获取对音频设备的访问权限?

How do I use get access to audio devices through the MMDevice API?

我正在尝试使用程序管理音频流,但我什至无法访问音频设备。作为旁注,我是 C++ 的新手,我在 Java 中学习了编码,所以我对指针的概念以及 HRESULT 和其他 windows 的概念是新的。在 this page it says that I must first enumerate an audio endpoint device. I then did some more searching to find out how to do so and was lead to the Enumerating Audio Devices page. That page told me to use the MMDevice API 上。此页面显示此代码:

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
hr = CoCreateInstance(
     CLSID_MMDeviceEnumerator, NULL,
     CLSCTX_ALL, IID_IMMDeviceEnumerator,
     (void**)&pEnumerator);

我尝试使用这个和

(void**)&pEnumerator

没有用,所以我找到了一些使用

的其他代码
reinterpret_cast<>();

为了处理这个错误。但是我 运行 遇到的问题是我没有得到指针。我的代码如下。

CoInitializeEx(NULL, COINIT_MULTITHREADED);

IMMDeviceEnumerator *pEnumerator;

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);

//print CLSID to see if I get any ids
std::cout << CLSID_MMDeviceEnumerator.Data1 << "\n";
std::cout << CLSID_MMDeviceEnumerator.Data2 << "\n";
std::cout << CLSID_MMDeviceEnumerator.Data3 << "\n";
std::cout << CLSID_MMDeviceEnumerator.Data4 << "\n";

//temp variable to hold the result of the CoCreateInstance call
LPVOID *ppv = NULL;

HRESULT retrivedDeviceEnumerator = CoCreateInstance(
    CLSID_MMDeviceEnumerator,
    NULL,
    CLSCTX_ALL,
    IID_IMMDeviceEnumerator,
    ppv
);
//I'm sure there is a better way to print and end the line
//print mem location to see if I am getting a result
std::cout << ppv << "\n";
//cast the result to correct type
pEnumerator = (reinterpret_cast<IMMDeviceEnumerator*>(ppv));

这段代码为 CLSID 行打印了一些数字,所以我认为这些数字得到了正确的值,但是当我打印 ppv 指针时我得到的是 00000000。

比较好的代码片段from there:

CoInitialize(NULL);
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, 
    __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
...