0

I am new to Qt and I want to use the AudioSessionManager2 from the Windows SDK. In Qt Creator using this example I've written appended code below.

When compiling I get the messages

undefined reference to `__imp_CoCreateInstance'
undefined reference to `IID_IAudioSessionManager2'

In the documentation for the core audio APIs, Microsoft states the APIs are implemented in Mmdevapi.dll and Audioses.dll. While I hoped to find two matching .lib files the SDK I downloaded with Visual Studio, I only found mmdevapi.lib. After copying it to my project and adding it to the qt project file like below I still had no success with the same error message.

How am I supposed to know which lib files to import for which functions?
How do I get these lib files?
Did I import the lib file correctly?


audiomanager.h:

#ifndef AUDIOMANAGER_H
#define AUDIOMANAGER_H

#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include "utils/SafeRelease.h"


class AudioManager {

public:
    AudioManager();
    ~AudioManager();

private:
    IAudioSessionManager2 *pSessionManager = nullptr;
    HRESULT init();


};

#endif // AUDIOMANAGER_H

audiomanager.cpp

#include "audiomanager.h"


AudioManager::AudioManager () {
    this->init();
}


AudioManager::~AudioManager () {
    SafeRelease(&pSessionManager);
}



HRESULT AudioManager::init () {
    HRESULT hr = S_OK;

    // initialize needed vars
    IMMDeviceEnumerator *pDeviceEnumerator = nullptr;
    IMMDevice *pDevice = nullptr;

    // get device enumerator
    hr = CoCreateInstance(
                __uuidof(MMDeviceEnumerator),
                nullptr,
                CLSCTX_ALL,
                IID_PPV_ARGS(&pDeviceEnumerator)
                );
    if (FAILED(hr))
        goto done;

    // get the default audio output (for consoles)
    // !!! only for consoles? (games, system, etc.), no  music, video
    pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
    if (FAILED(hr))
        goto done;

    // get the session manager
    hr = pDevice->Activate(
                IID_IAudioSessionManager2,
                CLSCTX_ALL,
                nullptr,
                reinterpret_cast<void**>(&pSessionManager)
                );

done:
    SafeRelease(&pDeviceEnumerator);
    SafeRelease(&pDevice);
    return hr;
}

Line from Qt Project file

LIBS += "$$PWD\..\libs\mmdevapi.lib"
Brainiac
  • 191
  • 1
  • 12
  • 1
    `CoCreateInstance` is part of `Ole32.lib`, – ChrisMM Dec 14 '19 at 21:55
  • For the other one, I'm not entirely sure, but try `uuid.lib` (other IID_ are in there) – ChrisMM Dec 14 '19 at 22:01
  • Thanks for the comments. @ChrisMM `Ole32.Lib` was correct - How do I find that out on my own? Are there any docs or any hints anywhere? Unfortunately `Uuid.lib` did not containt `IID_IAudioSessionManager2` – Brainiac Dec 14 '19 at 23:54
  • You can look up functions on [docs.micrsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance), and at the bottom it lists the header, dll, and library. I can't find one for the AudioSessionManager though, since the docs only refer to the header file, not library. – ChrisMM Dec 15 '19 at 00:03

1 Answers1

0

After hours of trying I figured out it would work with

// get the session manager
hr = pDevice->Activate(
            __uuidof(IAudioSessionManager2),
            CLSCTX_ALL,
            nullptr,
            reinterpret_cast<void**>(&pSessionManager)
            );

instead of

// get the session manager
hr = pDevice->Activate(
            IID_IAudioSessionManager2,
            CLSCTX_ALL,
            nullptr,
            reinterpret_cast<void**>(&pSessionManager)
            );

I don't know, why I cannot find proper libraries for the IID_IAudioSessionManager, nor why the Microsoft source code example lists this, but the other provided option seems to work.

Brainiac
  • 191
  • 1
  • 12