#ifdef SPEAKERDEVMUTECONTROL
# define SPEAKERDEVMUTECONTROL_EXPORT __declspec(dllexport)
#else
# define SPEAKERDEVMUTECONTROL_EXPORT __declspec(dllimport)
#endif
SPEAKERDEVMUTECONTROL_EXPORT bool setSpeakerDevName(const chConstStringA& strDevName);
SPEAKERDEVMUTECONTROL_EXPORT bool cancleSpeakerDevMute();
SPEAKERDEVMUTECONTROL_EXPORT bool returnSpeakerDevMute();
SPEAKERDEVMUTECONTROL_EXPORT int GetDevIDByName(chStringA strDevName);
SPEAKERDEVMUTECONTROL_EXPORT BOOL getSystemMuteStatusLaterWindowVista(BOOL& bMuted);
SPEAKERDEVMUTECONTROL_EXPORT BOOL setSystemMuteLaterWindowVista(BOOL bMuted = FALSE);
SPEAKERDEVMUTECONTROL_EXPORT BOOL setSystemMuteLaterWindowVistaByDevID(UINT devID, BOOL bMuted = FALSE);
SPEAKERDEVMUTECONTROL_EXPORT BOOL getSystemMuteStatusLaterWindowVistaByDevID(UINT devID, BOOL& bMuted);
//////////////////////
#include "../ETLLib/ETLLib.hpp"
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0600
#include <Windows.h>
#include <endpointvolume.h>
#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <FunctionDiscoveryKeys_devpkey.h>
#include "SpeakerDevMuteControl.h"
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
#define INIT_DEFAULT_IAUDIOENDPOINT \
CoInitialize(NULL);\
hr = S_OK;\
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),\
NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator),(void**)&m_pEnumerator);\
if(SUCCEEDED(hr))\
{\
hr = m_pEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &m_pDevice);\
if (SUCCEEDED(hr))\
{\
hr = m_pDevice->Activate(__uuidof(IAudioEndpointVolume),\
CLSCTX_ALL, NULL, (void**)&m_pEndptVolCtrl);\
}\
}
#define INIT_IAUDIOENDPOINT_BYID(devID) \
CoInitialize(NULL);\
hr = S_OK;\
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),\
NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator),(void**)&m_pEnumerator);\
if(SUCCEEDED(hr))\
{\
hr = m_pEnumerator->EnumAudioEndpoints(eAll, DEVICE_STATEMASK_ALL, &m_pCollection);\
if (SUCCEEDED(hr))\
{\
hr = m_pCollection->Item(devID, &m_pDevice);\
if (SUCCEEDED(hr))\
{\
hr = m_pDevice->Activate(__uuidof(IAudioEndpointVolume),\
CLSCTX_ALL, NULL, (void**)&m_pEndptVolCtrl);\
}\
}\
}
#define INIT_DEVICECOLLECTION \
CoInitialize(NULL);\
hr = S_OK;\
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),\
NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator),(void**)&m_pEnumerator);\
if(SUCCEEDED(hr))\
{\
hr = m_pEnumerator->EnumAudioEndpoints(eAll, DEVICE_STATEMASK_ALL, &m_pCollection);\
}
#define UNINIT_IAUDIOENDPPINTVOLUME \
SAFE_RELEASE(m_pEndptVolCtrl);\
SAFE_RELEASE(m_pDevice);\
SAFE_RELEASE(m_pEnumerator);\
SAFE_RELEASE(m_pCollection);\
CoUninitialize();
int GetDevIDByName(chStringA strDevName)
{
if (strDevName.empty())
{
return -1;
}
chStringW strDevNameW = chUTF82W(strDevName);
IAudioEndpointVolume *m_pEndptVolCtrl = NULL;
IMMDevice *m_pDevice = NULL;
IMMDeviceCollection *m_pCollection = NULL;
IMMDeviceEnumerator *m_pEnumerator = NULL;
HRESULT hr = S_OK;
INIT_DEVICECOLLECTION;
if (SUCCEEDED(hr))
{
UINT nDevCounts = 0;
m_pCollection->GetCount(&nDevCounts);
for (int i = 0; i < (int)nDevCounts; i++)
{
hr = m_pCollection->Item(i, &m_pDevice);
DWORD state;
m_pDevice->GetState(&state);
if (state == DEVICE_STATE_NOTPRESENT )
{
continue;
}
IPropertyStore* pRopertyStore;
hr = m_pDevice->OpenPropertyStore(STGM_READ, &pRopertyStore);
if (SUCCEEDED(hr))
{
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pRopertyStore->GetValue(PKEY_Device_FriendlyName, &varName);
typedef uCTextHelper<WCHAR, false> WCI;
if (WCI::strncmp(strDevNameW.c_ptr(), varName.pwszVal, strDevNameW.length()) == 0)
{
return i;
}
}
}
}
return -1;
}
BOOL setSystemMuteLaterWindowVista(BOOL bMuted /*= FALSE*/)
{
BOOL bReresult = FALSE;
IAudioEndpointVolume *m_pEndptVolCtrl = NULL;
IMMDevice *m_pDevice = NULL;
IMMDeviceCollection *m_pCollection = NULL;
IMMDeviceEnumerator *m_pEnumerator = NULL;
HRESULT hr = S_OK;
INIT_DEFAULT_IAUDIOENDPOINT;
if (SUCCEEDED(hr))
{
hr = m_pEndptVolCtrl->SetMute(bMuted, NULL);
if (SUCCEEDED(hr))
{
bReresult = TRUE;
}
}
UNINIT_IAUDIOENDPPINTVOLUME;
return bReresult;
}
BOOL getSystemMuteStatusLaterWindowVista(BOOL& bMuted)
{
BOOL bReresult = FALSE;
IAudioEndpointVolume *m_pEndptVolCtrl = NULL;
IMMDevice *m_pDevice = NULL;
IMMDeviceCollection *m_pCollection = NULL;
IMMDeviceEnumerator *m_pEnumerator = NULL;
HRESULT hr = S_OK;
INIT_DEFAULT_IAUDIOENDPOINT;
if (SUCCEEDED(hr))
{
hr = m_pEndptVolCtrl->GetMute(&bMuted);
if (SUCCEEDED(hr))
{
bReresult = TRUE;
}
}
UNINIT_IAUDIOENDPPINTVOLUME;
return bReresult;
}
BOOL setSystemMuteLaterWindowVistaByDevID(UINT devID, BOOL bMuted /*= FALSE*/)
{
if (devID == -1)
{
return setSystemMuteLaterWindowVista(bMuted);
}
BOOL bReresult = FALSE;
IMMDeviceCollection *m_pCollection = NULL;
IMMDevice *m_pDevice = NULL;
IAudioEndpointVolume *m_pEndptVolCtrl = NULL;
IMMDeviceEnumerator *m_pEnumerator = NULL;
HRESULT hr = S_OK;
INIT_IAUDIOENDPOINT_BYID(devID);
if (SUCCEEDED(hr))
{
hr = m_pEndptVolCtrl->SetMute(bMuted, NULL);
if (SUCCEEDED(hr))
{
bReresult = TRUE;
}
}
UNINIT_IAUDIOENDPPINTVOLUME;
return bReresult;
}
BOOL getSystemMuteStatusLaterWindowVistaByDevID(UINT devID, BOOL& bMuted)
{
if (devID == -1)
{
return getSystemMuteStatusLaterWindowVista(bMuted);
}
BOOL bReresult = FALSE;
IMMDevice *m_pDevice = NULL;
IMMDeviceCollection *m_pCollection = NULL;
IAudioEndpointVolume *m_pEndptVolCtrl = NULL;
IMMDeviceEnumerator *m_pEnumerator = NULL;
HRESULT hr = S_OK;
INIT_IAUDIOENDPOINT_BYID(devID);
if (SUCCEEDED(hr))
{
hr = m_pEndptVolCtrl->GetMute(&bMuted);
if (SUCCEEDED(hr))
{
bReresult = TRUE;
}
}
UNINIT_IAUDIOENDPPINTVOLUME;
return bReresult;
}
static chStringA s_strDevName;
static BOOL bMuted = FALSE;
static bool bCancelMuted = false;
bool setSpeakerDevName(const chConstStringA& strDevName)
{
if (strDevName != s_strDevName)
{
s_strDevName = strDevName;
return true;
}
return false;
}
bool cancleSpeakerDevMute()
{
if (s_strDevName.empty())
{
return false;
}
bCancelMuted = true;
UINT iDevID = GetDevIDByName(s_strDevName);
getSystemMuteStatusLaterWindowVistaByDevID(iDevID, bMuted);
setSystemMuteLaterWindowVistaByDevID(iDevID, FALSE);
}
bool returnSpeakerDevMute()
{
if (s_strDevName.empty() || !bCancelMuted)
{
return false;
}
bCancelMuted = false;
UINT iDevID = GetDevIDByName(s_strDevName);
setSystemMuteLaterWindowVistaByDevID(iDevID, bMuted);
}