Undefined reference to CLSID_MMDeviceEnumerator and IID_IMMDeviceEnumerator

Attempting to compile the example code using COM and CoCreateInstance () using MinGW-w64 in C fails.

#include <windows.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>

#include <stdlib.h>
#include <stdio.h>

extern const CLSID CLSID_MMDeviceEnumerator;
extern const IID IID_IMMDeviceEnumerator;

int main( void )
{
    CoInitialize( NULL );

    LPVOID device = NULL;
    const HRESULT ok = CoCreateInstance(    &CLSID_MMDeviceEnumerator, NULL, 
                                            CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, 
                                            &device );  
    CoUninitialize();

return EXIT_SUCCESS;
}

      

Compile with: gcc main.c libole32.a -Wall -Wextra -oa

Even though CLSID_MMDeviceEnumerator is defined in mmdeviceapi.h, it is not found. In fact, removing my extern definitions from the example code gives the same result as both externs are apparently defined in the mmdeviceapi.h file

When I used __uuidof and compiled with g ++ the code worked, but this C "replacement" for __uuidof doesn't.

Why weren't COM IDs found?

+3


source to share


1 answer


The solution, using MinGW-w64, should include a title #include <initguid.h>

before including headers containing COM identifiers such as mmdeviceapi.h

, endpointvolue.h

.

Then no additional declarations are needed, and the identifiers can be used directly.



Decision:

#include <windows.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    CoInitialize( NULL );

    LPVOID device = NULL;
    const HRESULT ok = CoCreateInstance(    &CLSID_MMDeviceEnumerator, NULL, 
                                            CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, 
                                            &device );  
    CoUninitialize();

return EXIT_SUCCESS;
}

      

+5


source







All Articles