简体   繁体   中英

QT Include Windows SDK Library

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM