使用例子如下:

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 = ;
m_hRes = pAudioSessionEnumerator->GetCount(&nCount); for ( int i = ; 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 = ;
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 = ;
m_hRes = pAudioSessionEnumerator->GetCount(&nCount); for ( int i = ; 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 = ;
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] = { };
::GetModuleBaseName(hProcess, NULL, szName, MAX_PATH);
if ( strProcName.CompareNoCase(szName) == )
{
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;
}

【Win32】对指定进程进行禁音的更多相关文章

  1. [Win32]获取指定进程的父进程PID

    // // #include <Windows.h> #include <winnt.h> #include <winternl.h> typedef NTSTAT ...

  2. 使用VC++通过远程进程注入来实现HOOK指定进程的某个API

    前阵子读到一篇关于<HOOK API入门之Hook自己程序的MessageBoxW>的博客,博客地址:http://blog.csdn.net/friendan/article/detai ...

  3. Win32 OpenProcess打开进程失败,返回5无权限操作

    Win32 OpenProcess打开进程失败,返回5无权限操作,相信你会碰到这样的事,在IDE中可以,单独却不可以了,其实这时就需要提权了,否则是无法打开的,OpenProcess提权至Debug即 ...

  4. win32进程名查找进程PID

    1. #include <Psapi.h> #pragma comment(lib, "Psapi.lib") DWORD GetProcIDFromName(LPCT ...

  5. Linux kill 杀死指定进程

    一  杀死指定进程 现知道有一个php线程正在运行,需要杀死 root 26278 1 0 2015 ? 00:00:31 /usr/local/php/bin/php /var/www/html/r ...

  6. Android Studio怎样选择查看指定进程的log?

    今天说一个简单的东东,关于Android Studio在logcat中怎样查看指定进程的log. 关于过滤你自己所开发的应用的log.在这篇文章中:Android Studio怎样给log加入过滤项( ...

  7. linux:如何指定进程运行的CPU

    coolshell最新的文章<性能调优攻略>在"多核CPU调优"章节,提到"我们不能任由操作系统负载均衡,因为我们自己更了解自己的程序,所以,我们可以手动地为 ...

  8. mac查看当前调用tcp的进程并关闭指定进程

    查看所有tcp进程 监听的端口 lsof -iTCP -sTCP:LISTEN 查看指定端口信息 lsof -i: 关闭指定进程 kill -

  9. [转] 指定进程运行的CPU

    转自:https://www.cnblogs.com/liuhao/archive/2012/06/21/2558069.html coolshell最新的文章<性能调优攻略>在“多核CP ...

随机推荐

  1. jquery插件制作,下拉菜单

    要求输入框点击出现下拉菜单,并实现以下功能: 1.首先点击地点标签页,选择好地点: 2.自动显示相应节点标签页显示节点信息,选择好节点 3.自动显示相应的连接点,选择连接点,连接点被选中并被传送的输入 ...

  2. 静态网页、动态网页、apache和tomcat之间区别和联系

    1.静态网页 静态网页:在网站设计中, 纯粹的HTML(标准通用标志语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的拓拓展名是.html或者. ...

  3. 12个值得关注的顶级可视化JS库 涉及图表、动画、时间处理,表格操作

    本文是译文,原文是https://da-14.com/blog/top-11...我在原文的基础上加了百度的Echats图表库,这个也是毫不逊色其他图表库的.另外Handsontable电子表格库也是 ...

  4. Redis报错:redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snap

    首先找到出现错误的原因: redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but ...

  5. destoon调用方法汇总 ---转载

    根目录.模板目录和样式目录:{DT_PATH}{DT_SKIN}导入头脚:{template 'header'}{template 'footer'}对应模块首页:{$MODULE[$moduleid ...

  6. Windows里安装wireshark或者ethereal工具(包括汉化破解)(图文详解)

    不多说,直接上干货! https://www.wireshark.org/download.html 我这里,读取的是,来自于https://www.ll.mit.edu/ideval/data/19 ...

  7. ActiveMQ:Exception occurred while processing this request, check the log for more information!

    出现上面错误的原因有以下两种 1 jdk的版本和activemq的版本不符 安装完ActiveMQ之后,通过http://IP:8161登陆到控制台. 通过测试代码给服务端发送队列消息,在控制台点击q ...

  8. VMware虚拟机配置

    VMware虚拟机配置 背景 在做学习Linux时经常会在win环境下安装虚拟机,这其中涉及到一些细节操作,需要对虚拟机工作模式加以理解. 本文在学习hadoop分布式环境搭建时写作. 1.虚拟交换机 ...

  9. PL/SQL:these query result are not updateable,include the ROWID to get updateab -----for update

    these query result are not updateable,include the ROWID to get updateab 原因: 其实,选中一个表后,右键,如果选择“query ...

  10. jpages中文api

    需要引入的文件 :   containerID 字符串 默认值: 必需!(无默认值) 要分页的项目的容器ID.它可以是UL,OL,DIV等.    first  String || 假 默认值: fa ...