Click here to Skip to main content
Click here to Skip to main content

Change Master Volume in Visual C++

By , 29 Jul 2011
 

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(&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;
}

The nVolume parameter must be between 0.0 and 1.0.

0.0 means mute and 1.0 means 100.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sayyed Mostafa Hashemi
Software Developer (Senior)
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
I am Mostafa Hashemi.I was born in Tehran. I graduated from IUMS university in 2002 (1381). Although my university field different from programming, but I like it. Since I acquainted with computer, I have been interested in Visual C++. I have been beginning to program in Visual C++ in 2003.
Thanks,
http://www.sischer.ir

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberShigetsugu23 Aug '12 - 20:18 
QuestionYou should use attributionmemberLonWolve7 Aug '12 - 17:49 
GeneralMy vote of 1memberLonWolve7 Aug '12 - 17:48 
This is basically copy-paste from Larry Osterman's blog and no attribution is made. http://blogs.msdn.com/b/larryosterman/archive/2007/03/06/how-do-i-change-the-master-volume-in-windows-vista.aspx
GeneralCurious... I haven't played with volume apps too much since ...memberAlbert Holguin2 Aug '11 - 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 29 Jul 2011
Article Copyright 2011 by Sayyed Mostafa Hashemi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid