Undefined reference to CLSID_MMDeviceEnumerator an

2019-05-12 15:06发布

Trying to compile an 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;
}

Compiling with: gcc main.c libole32.a -Wall -Wextra -o a

Even though CLSID_MMDeviceEnumerator is defined in mmdeviceapi.h it isn't found. Actually removing my extern definitions from the sample code gives the same result since both externs seems to be defined in the mmdeviceapi.h

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

Why aren't COM identifiers found?

1条回答
姐就是有狂的资本
2楼-- · 2019-05-12 16:00

The solution, when using MinGW-w64, is to include the header #include <initguid.h>, before including headers that contain COM identifiers such as mmdeviceapi.h, endpointvolue.h.

Then no extra declarations are needed and identifiers can be used directly.

Solution:

#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;
}
查看更多
登录 后发表回答