Click here to Skip to main content
13,899,460 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 29 Jul 2011
Licenced CPOL

Change Master Volume in Visual C++

, 29 Jul 2011
Rate this:
Please Sign up or sign in to vote.
How to set/change the master volume.

To change volume in Vista and Windows 7, use this code:

#include <mmdeviceapi.h>
#include <endpointvolume.h>

bool ChangeVolume(double nVolume,bool bScalar)

    bool decibels = false;
    bool scalar = false;
    double newVolume=nVolume;

    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 = NULL;

    IAudioEndpointVolume *endpointVolume = NULL;
    hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
         CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
    defaultDevice = NULL;

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

    hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
    //CString strCur=L"";

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


    return FALSE;

The nVolume parameter must be between 0.0 and 1.0.

0.0 means mute and 1.0 means 100.


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


About the Author

You may also be interested in...


Comments and Discussions

QuestionIt is very nice. Pin
Jungryul Kim13-Feb-19 21:51
memberJungryul Kim13-Feb-19 21:51 
PraiseExcellent Pin
Member 80819842-Mar-18 15:34
memberMember 80819842-Mar-18 15:34 
Questionunable to use this method. Pin
Member 116484109-Oct-15 1:38
memberMember 116484109-Oct-15 1:38 
GeneralMy vote of 5 Pin
wanggxii21-Aug-13 4:26
memberwanggxii21-Aug-13 4:26 
GeneralWin7 Volume control Pin
jimlundberg12-Jul-13 17:23
memberjimlundberg12-Jul-13 17:23 
GeneralMy vote of 5 Pin
ThatsAlok3-Jun-13 20:06
memberThatsAlok3-Jun-13 20:06 
GeneralMy vote of 5 Pin
Shigetsugu23-Aug-12 20:18
memberShigetsugu23-Aug-12 20:18 
QuestionYou should use attribution Pin
LonWolve7-Aug-12 17:49
memberLonWolve7-Aug-12 17:49 
GeneralMy vote of 1 Pin
LonWolve7-Aug-12 17:48
memberLonWolve7-Aug-12 17:48 
GeneralCurious... I haven't played with volume apps too much since ... Pin
Albert Holguin2-Aug-11 11:53
professionalAlbert Holguin2-Aug-11 11:53 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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