使用windowsAPI来设置电脑静音和取消静音,网上找的例子,自己封装成函数。
#ifndef CSETCOMPUTERMUTE_H
#define CSETCOMPUTERMUTE_H
#include <mmdeviceapi.h>
#include <endpointvolume.h>
class CSetComputerMute
{
public:
CSetComputerMute();
public:
HRESULT InitDevice();
BOOL GetDeviceState();
HRESULT SetDeviceMute(int nMute);
private:
IMMDeviceEnumerator *deviceEnumerator;
IMMDevice *defaultDevice;
IAudioEndpointVolume *endpointVolume;
BOOL deviceMuteState;
};
#endif // CSETCOMPUTERMUTE_H
#include "csetcomputermute.h"
#include <mmdeviceapi.h>
#include <endpointvolume.h>
CSetComputerMute::CSetComputerMute()
{
deviceEnumerator = NULL;
defaultDevice = NULL;
endpointVolume = NULL;
deviceMuteState = FALSE;
}
HRESULT CSetComputerMute::InitDevice()
{
HRESULT hRes = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
hRes = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
hRes = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
return hRes;
}
BOOL CSetComputerMute::GetDeviceState()
{
HRESULT hRes = endpointVolume->GetMute(&deviceMuteState);
return deviceMuteState;
}
HRESULT CSetComputerMute::SetDeviceMute(int nMute)
{
HRESULT hRes = endpointVolume->SetMute(nMute, NULL);
return hRes;
}