Hi!
I have a simple problem which I couldn't solve. It is about changing master sound level in Windows Vista / Windows 7 using the application compiled with Visual Studio 2010. There is the code given below which I have used in order to adjust master volume level, but nothing have happened. Can anyone point out to me the mistake inside the source code?
Thank you in advance!
CODE
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <windows.h>
#include <tchar.h>
#pragma comment(lib, "winmm.lib");
int _tmain(int argc, _TCHAR* argv[])
{
double nVolume = 0.1; // Mute is 0 and full blast is 1.0
bool ChangeVolume((double) nVolume);
}
bool ChangeVolume(double nVolume,bool bScalar)
{
bScalar = true;
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);
//printf("Current volume in dB is: %f\n", currentVolume);
hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
//CString strCur=L"";
//strCur.Format(L"%f",currentVolume);
//AfxMessageBox(strCur);
// printf("Current volume as a scalar is: %f\n", currentVolume);
if (bScalar==false)
{
hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
}
else if (bScalar==true)
{
hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
}
endpointVolume->Release();
CoUninitialize();
return FALSE;
}