To change volume in Vista and Windows 7, use this code:
#include <mmdeviceapi.h>
#include <endpointvolume.h>
bool ChangeVolume(double nVolume,bool bScalar)
{
HRESULT hr=NULL;
bool decibels = false;
bool scalar = false;
double newVolume=nVolume;
CoInitialize(NULL);
IMMDeviceEnumerator *deviceEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
IMMDevice *defaultDevice = NULL;
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
deviceEnumerator->Release();
deviceEnumerator = NULL;
IAudioEndpointVolume *endpointVolume = NULL;
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
defaultDevice->Release();
defaultDevice = NULL;
float currentVolume = 0;
endpointVolume->GetMasterVolumeLevel(¤tVolume);
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
if (bScalar==false)
{
hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
}
else if (bScalar==true)
{
hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
}
endpointVolume->Release();
CoUninitialize();
return FALSE;
}
The nVolume parameter must be between 0.0 and 1.0.
0.0 means mute and 1.0 means 100.