Reputation: 5290
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?
Upvotes: 3
Views: 4363
Reputation: 5290
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;
}
Upvotes: 8