Undefined reference to CLSID_MMDeviceEnumerator and IID_IMMDeviceEnumerator

感情迁移 提交于 2019-12-10 10:13:23

问题


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:


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


来源:https://stackoverflow.com/questions/31666420/undefined-reference-to-clsid-mmdeviceenumerator-and-iid-immdeviceenumerator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!