Search code examples
cwindowscomuuid

__uuidof in C Master Volume Windows


I want to change master volume with C, but __uuidof is only for C++; what can I use instead of that?

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

I only found that: Alternative to __uuidof in C


Solution

  • CLSID_MMDeviceEnumerator and IID_IMMDeviceEnumerator are defined in your API's header file, i.e. <mmdeviceapi.h>.

    You have to use these definitions in your C code, instead of __uuidof, as this is only available for C++ code.

    Note that you need to include <initguid.h> before <mmdeviceapi.h>:

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

    Then this code should work:

    hr = CoCreateInstance(
        &CLSID_MMDeviceEnumerator, (*)
        NULL,
        CLSCTX_ALL, 
        &IID_IMMDeviceEnumerator,  (*)
        (void**)&deviceEnumerator
    );
    

    (*) Note that I've used the & (address-of), since in C++ you have references, but in C code you need to be explicit using pointers.