• 【Win32】对指定进程进行禁音


    使用例子如下:

    CAudioMgr AudioMgr;
    AudioMgr.SetProcessMute(GetCurrentProcessId());

    H文件的内容如下:

    #pragma once
    
    #include <Audiopolicy.h>
    
    class CAudioMgr
    {
    public:
        CAudioMgr();
        ~CAudioMgr();
    
    public:
        HRESULT GetHResult() const { return m_hRes; }
        BOOL    SetProcessMute(const CString& strProcName);
        BOOL    SetProcessMute(DWORD Pid);
    
    private:
        BOOL    __GetAudioSessionMgr2();
    
    private:
        HRESULT                 m_hRes;
        IAudioSessionManager2*  m_lpAudioSessionMgr;
    };

    CPP文件内容如下:

    #include "stdafx.h"
    #include <mmdeviceapi.h>
    #include <Psapi.h>
    #include "AudioManager.h"
    
    #pragma comment(lib, "Psapi.lib")
    
    CAudioMgr::CAudioMgr()
        : m_hRes( ERROR_SUCCESS )
        , m_lpAudioSessionMgr( NULL )
    {
        ::CoInitialize(NULL);
    }
    
    CAudioMgr::~CAudioMgr()
    {
        ::CoUninitialize();
    }
    
    BOOL CAudioMgr::SetProcessMute(DWORD Pid)
    {
        if ( !this->__GetAudioSessionMgr2() || m_lpAudioSessionMgr == NULL )
        {
            return FALSE;
        }
    
        CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator;
        m_hRes = m_lpAudioSessionMgr->GetSessionEnumerator(&pAudioSessionEnumerator);
        if ( FAILED(m_hRes) || pAudioSessionEnumerator == NULL )
        {
            return FALSE;
        }
    
        int nCount = 0;
        m_hRes = pAudioSessionEnumerator->GetCount(&nCount);
    
        for ( int i = 0; i < nCount; ++i )
        {
            CComPtr<IAudioSessionControl> pAudioSessionControl;
            m_hRes = pAudioSessionEnumerator->GetSession(i, &pAudioSessionControl);
            if ( FAILED(m_hRes) || pAudioSessionControl == NULL )
            {
                continue;
            }
    
            CComQIPtr<IAudioSessionControl2> pAudioSessionControl2(pAudioSessionControl);
            if ( pAudioSessionControl2 == NULL )
            {
                continue;
            }
    
            DWORD dwPid = 0;
            m_hRes = pAudioSessionControl2->GetProcessId(&dwPid);
            if( FAILED(m_hRes) )
            {
                continue;
            }
    
            if ( dwPid == Pid )
            {
                CComQIPtr<ISimpleAudioVolume> pSimpleAudioVolume(pAudioSessionControl2);
                if ( pSimpleAudioVolume == NULL )
                {
                    continue;
                }
    
                m_hRes = pSimpleAudioVolume->SetMute(TRUE, NULL);
                break;
            }
        }
    
        return SUCCEEDED(m_hRes);
    }
    
    BOOL CAudioMgr::SetProcessMute(const CString& strProcName)
    {
        if ( !this->__GetAudioSessionMgr2() || m_lpAudioSessionMgr == NULL )
        {
            return FALSE;
        }
    
        CComPtr<IAudioSessionEnumerator> pAudioSessionEnumerator;
        m_hRes = m_lpAudioSessionMgr->GetSessionEnumerator(&pAudioSessionEnumerator);
        if ( FAILED(m_hRes) || pAudioSessionEnumerator == NULL )
        {
            return FALSE;
        }
    
        int nCount = 0;
        m_hRes = pAudioSessionEnumerator->GetCount(&nCount);
    
        for ( int i = 0; i < nCount; ++i )
        {
            CComPtr<IAudioSessionControl> pAudioSessionControl;
            m_hRes = pAudioSessionEnumerator->GetSession(i, &pAudioSessionControl);
            if ( FAILED(m_hRes) || pAudioSessionControl == NULL )
            {
                continue;
            }
    
            CComQIPtr<IAudioSessionControl2> pAudioSessionControl2(pAudioSessionControl);
            if ( pAudioSessionControl2 == NULL )
            {
                continue;
            }
    
            DWORD dwPid = 0;
            m_hRes = pAudioSessionControl2->GetProcessId(&dwPid);
            if( FAILED(m_hRes) )
            {
                continue;
            }
    
            CHandle hProcess;
            hProcess.Attach(OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid));
            if ( hProcess == NULL )
            {
                continue;
            }
    
            TCHAR szName[MAX_PATH] = { 0 };
            ::GetModuleBaseName(hProcess, NULL, szName, MAX_PATH);
            if ( strProcName.CompareNoCase(szName) == 0 )
            {
                CComQIPtr<ISimpleAudioVolume> pSimpleAudioVolume(pAudioSessionControl2);
                if ( pSimpleAudioVolume == NULL )
                {
                    continue;
                }
                m_hRes = pSimpleAudioVolume->SetMute(TRUE, NULL);
                break;
            }
        }
    
        return SUCCEEDED(m_hRes);
    }
    
    BOOL CAudioMgr::__GetAudioSessionMgr2()
    {
        if ( m_lpAudioSessionMgr == NULL )
        {
            CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
    
            m_hRes = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL);
            if (  FAILED(m_hRes) || (pMMDeviceEnumerator == NULL) )
            {
                return FALSE;
            }
    
            CComPtr<IMMDevice> pDefaultDevice;
            m_hRes = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultDevice);
            if( FAILED(m_hRes) || pDefaultDevice == NULL )
            {
                return FALSE;
            }
    
            m_hRes = pDefaultDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**)&m_lpAudioSessionMgr);
            if ( FAILED(m_hRes) || (m_lpAudioSessionMgr == NULL) )
            {
                return FALSE;
            }
        }
    
        return TRUE;
    }
  • 相关阅读:
    oracle单行函数 之 转换函数
    oracle单行函数 之 时间函数
    oracle单行函数 之 数字函数
    oracle单行函数 之 字符函数
    oracle 之 如何链接别人电脑的oracle
    轻应用介绍
    Web 目录枚举与遍历漏洞解决
    接口测试工具(Postman)
    Tomcat 编码不一致导致乱码
    持久化配置管理 diamond 使用简介
  • 原文地址:https://www.cnblogs.com/yvqvan/p/8716450.html
Copyright © 2020-2023  润新知