0

I am trying to manage the audio streams with a program, but I am having trouble even getting access to an audio device. As a side note I am new to C++ and I learned coding in Java so I'm new to the idea of pointers as well as the ideas of the HRESULT and other windows stuff. On 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. This page shows this code:

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

I tried to use this and the

(void**)&pEnumerator

did not work, so I found some other code that used a

reinterpret_cast<>();

in order to deal with this error. But the problem that I am running into is that I am not getting a pointer. My code is below.

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

This code prints some numbers for the CLSID lines so I think that those are getting the correct values, but I am getting 00000000 when I print the ppv pointer.

Will
  • 3
  • 4
  • Hopefully & is not actually in the source code you tried, be careful copy/pasting code from a web site. Very important to pay attention to HRESULT, it tells you why a function failed. Declare the pointer correctly first, should be IMMDeviceEnumerator* ppv. And pass it correctly to CoCreateInstance(), last argument should be (void**)&ppv. Once you have a valid pointer, be sure to not use a C or C++ cast, use its QueryInterface() function. – Hans Passant Jul 04 '18 at 16:26

1 Answers1

1

Relatively good code snippet 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);
...
Roman R.
  • 68,205
  • 6
  • 94
  • 158