Jump to content

Master Sound Level Windows Vista/7


Airforcex6
 Share

Recommended Posts

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(&currentVolume);

//printf("Current volume in dB is: %f\n", currentVolume);

hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);

//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;

}

Link to comment
Share on other sites

Thank you anyway, I have found the code that works well!

Here it is:

CODE

#include <WinSDKVer.h>

#define _WIN32_WINNT _WIN32_WINNT_VISTA

#include <SDKDDKVer.h>

#define WIN32_LEAN_AND_MEAN

// Windows Header Files:

#include <windows.h>

#include <tchar.h>

#include <mmdeviceapi.h>

#include <endpointvolume.h>

int APIENTRY _tWinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

//double newVolume = _ttof(lpCmdLine);

double newVolume = 0.1; // Put the value between 0.0 and 1.0

CoInitialize(NULL);

IMMDeviceEnumerator* deviceEnumerator = NULL;

if(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator) == S_OK) {

IMMDevice* defaultDevice = NULL;

if(deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice) == S_OK) {

IAudioEndpointVolume* endpointVolume = NULL;

if(defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume) == S_OK) {

endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);

endpointVolume->Release();

}

defaultDevice->Release();

}

deviceEnumerator->Release();

}

CoUninitialize();

return 0;

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...