繁体   English   中英

windows 核心音频 api IAudioSessionControl2::GetProcessId 返回未引用的错误代码

[英]windows core audio api IAudioSessionControl2::GetProcessId returns unreferenced error code

我正在尝试使用 Windows 核心音频 api 开发混音器应用程序。 运行测试代码时,我在调用IAudioSessionControl2::GetProcessId时遇到错误

测试代码:

#include <iostream>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <endpointvolume.h>

#define CHECK_RES(res, retCode) if(res != S_OK){CoUninitialize();std::cerr << "error code: " << res << std::endl;return retCode;}
#define SAFE_RELEASE(ptr) if(ptr!=NULL){ptr->Release();ptr=NULL;}

#define CODE(code) std::cout << #code << ": " << code << std::endl;

int main()
{

    HRESULT res = CoInitialize(NULL);
    CHECK_RES(res, -1);

    IMMDeviceEnumerator* pDeviceEnumerator = NULL;
    res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID*)&pDeviceEnumerator);
    CHECK_RES(res, -2);

    EDataFlow dataFlow = EDataFlow::eRender;
    ERole role = ERole::eMultimedia;
    IMMDevice* pDevice = NULL;
    res = pDeviceEnumerator->GetDefaultAudioEndpoint(dataFlow, role, &pDevice);
    CHECK_RES(res, -3);

    IAudioSessionManager2* pSessionManager = NULL;
    res = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (LPVOID*)&pSessionManager);
    CHECK_RES(res, -4);

    IAudioSessionEnumerator* pAudioSessionsEnumerator = NULL;
    res = pSessionManager->GetSessionEnumerator(&pAudioSessionsEnumerator);
    CHECK_RES(res, -5);

    int size = 0;
    res = pAudioSessionsEnumerator->GetCount(&size);
    CHECK_RES(res, -6);
    IAudioSessionControl2* pSessionControl = NULL;
    for (int i = 0; i < size; i++) {
        LPWSTR name = 0;
        res = pAudioSessionsEnumerator->GetSession(i, (IAudioSessionControl**)&pSessionControl);
        CHECK_RES(res, -7);

        // here res is 0, but name is 0 too 
        res = pSessionControl->GetSessionInstanceIdentifier(&name);
        std::cout << "res for name operation is : " << res << ", name: " << name << std::endl;

        // if instead of pSessionControl->GetSessionInstanceIdentifier I run 
        // pSessionControl->GetSessionIdentifier, above res code is -2147467262
        // and below res code goes -2

        DWORD id = NULL;
        // here I get res = -1 when I run this method
        // res can be -2 if GetSessionIdentifier is ran
        res = pSessionControl->GetProcessId(&id);
        std::cout << "res for id operation is : " << res << ", id: " << id << std::endl;

        SAFE_RELEASE(pSessionControl);


        CoTaskMemFree(name);
    }

    // tried to compare to know error codes but none matched
    CODE(AUDCLNT_E_DEVICE_INVALIDATED);
    CODE(AUDCLNT_E_RESOURCES_INVALIDATED);

    SAFE_RELEASE(pDeviceEnumerator);
    SAFE_RELEASE(pDevice);
    SAFE_RELEASE(pAudioSessionsEnumerator);
    SAFE_RELEASE(pSessionManager);

    CoUninitialize();
    return 0;

}

但是,如果有任何其他解决方案来检索链接到 IAudioSessionControl 的进程,我会很高兴听到它。

我正在尝试使用 Windows 核心音频 api 开发混音器应用程序。 运行测试代码时,我在调用IAudioSessionControl2::GetProcessId时遇到错误

测试代码:

#include <iostream>
#include <mmdeviceapi.h>
#include <audiopolicy.h>
#include <endpointvolume.h>

#define CHECK_RES(res, retCode) if(res != S_OK){CoUninitialize();std::cerr << "error code: " << res << std::endl;return retCode;}
#define SAFE_RELEASE(ptr) if(ptr!=NULL){ptr->Release();ptr=NULL;}

#define CODE(code) std::cout << #code << ": " << code << std::endl;

int main()
{

    HRESULT res = CoInitialize(NULL);
    CHECK_RES(res, -1);

    IMMDeviceEnumerator* pDeviceEnumerator = NULL;
    res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (LPVOID*)&pDeviceEnumerator);
    CHECK_RES(res, -2);

    EDataFlow dataFlow = EDataFlow::eRender;
    ERole role = ERole::eMultimedia;
    IMMDevice* pDevice = NULL;
    res = pDeviceEnumerator->GetDefaultAudioEndpoint(dataFlow, role, &pDevice);
    CHECK_RES(res, -3);

    IAudioSessionManager2* pSessionManager = NULL;
    res = pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (LPVOID*)&pSessionManager);
    CHECK_RES(res, -4);

    IAudioSessionEnumerator* pAudioSessionsEnumerator = NULL;
    res = pSessionManager->GetSessionEnumerator(&pAudioSessionsEnumerator);
    CHECK_RES(res, -5);

    int size = 0;
    res = pAudioSessionsEnumerator->GetCount(&size);
    CHECK_RES(res, -6);
    IAudioSessionControl2* pSessionControl = NULL;
    for (int i = 0; i < size; i++) {
        LPWSTR name = 0;
        res = pAudioSessionsEnumerator->GetSession(i, (IAudioSessionControl**)&pSessionControl);
        CHECK_RES(res, -7);

        // here res is 0, but name is 0 too 
        res = pSessionControl->GetSessionInstanceIdentifier(&name);
        std::cout << "res for name operation is : " << res << ", name: " << name << std::endl;

        // if instead of pSessionControl->GetSessionInstanceIdentifier I run 
        // pSessionControl->GetSessionIdentifier, above res code is -2147467262
        // and below res code goes -2

        DWORD id = NULL;
        // here I get res = -1 when I run this method
        // res can be -2 if GetSessionIdentifier is ran
        res = pSessionControl->GetProcessId(&id);
        std::cout << "res for id operation is : " << res << ", id: " << id << std::endl;

        SAFE_RELEASE(pSessionControl);


        CoTaskMemFree(name);
    }

    // tried to compare to know error codes but none matched
    CODE(AUDCLNT_E_DEVICE_INVALIDATED);
    CODE(AUDCLNT_E_RESOURCES_INVALIDATED);

    SAFE_RELEASE(pDeviceEnumerator);
    SAFE_RELEASE(pDevice);
    SAFE_RELEASE(pAudioSessionsEnumerator);
    SAFE_RELEASE(pSessionManager);

    CoUninitialize();
    return 0;

}

但是,如果有任何其他解决方案来检索链接到 IAudioSessionControl 的进程,我会很高兴听到它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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