[HELP] Change Master Volume for windows [C++]

Hello World !

I just want to know if I am able to change the master volume by coding. Example

Computer volume at 20%

SetVol(100);

If any of you know how to change the master volume, please let me know :)

Sincerely, Neahle.
Last edited on
Hi Neahle,

Here is some console code using DevC.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <initguid.h> 
#include <mmdeviceapi.h>
#include <EndPointVolume.h>
#include <iostream>

int main(int argc, char** argv) 
{
 HRESULT hr = CoInitialize(NULL);
 if(hr == S_OK)
   {
   IMMDeviceEnumerator *pIMMDeviceEnumerator = NULL;
   hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
                         IID_IMMDeviceEnumerator, (LPVOID*)&pIMMDeviceEnumerator);
   if(hr == S_OK)
     {
     IMMDevice *defaultDevice = NULL;
     hr = pIMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
     pIMMDeviceEnumerator->Release();
     pIMMDeviceEnumerator = NULL;
     if(hr == S_OK) //
       {
       IAudioEndpointVolume *EndPointVolume = NULL;
       hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
                                    CLSCTX_ALL, NULL, (LPVOID *)&EndPointVolume);
       defaultDevice->Release();
       defaultDevice = NULL;       

       float Volume = 0; //Scalar volume is from zero to 1.00 in 0.01 step
       hr = EndPointVolume->GetMasterVolumeLevelScalar(&Volume);
       if(hr == S_OK) //       
         {
         std::cout <<  "\nVolume is now " << Volume;
         std::cout << "\nPlease enter a volume value between zero and 100: ";
         std::cin >> Volume;
         if (Volume >= 1) 
           {
           Volume = Volume / 100; //Adjust to zero to 1.00 in 0.01 step
           }
         hr = EndPointVolume->SetMasterVolumeLevelScalar((float)Volume, NULL);
         if(hr == S_OK) //       
           {
           std::cout <<  "\nVolume is set to " << Volume * 100;
           }
         else
           {
           std::cout <<  "\nInvalid value ";
           }
         EndPointVolume->Release();
         EndPointVolume = NULL;  
         }
       }
     }
     CoUninitialize();
   }

 Beep(1000, 100);

 std::cout << "\nSpace or click..."; while (GetKeyState(VK_SPACE) + GetKeyState(VK_LBUTTON) >= 0){Sleep(20);}

 return 0;
}  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <initguid.h> 
#include <mmdeviceapi.h>
#include <EndPointVolume.h>
#include <iostream>

int main(int argc, char** argv) 
{
 HRESULT hr = CoInitialize(NULL);
 if(hr == S_OK)
   {
   IMMDeviceEnumerator *pIMMDeviceEnumerator = NULL;
   hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
                         IID_IMMDeviceEnumerator, (LPVOID*)&pIMMDeviceEnumerator);
   if(hr == S_OK)
     {
     IMMDevice *defaultDevice = NULL;
     hr = pIMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
     pIMMDeviceEnumerator->Release();
     pIMMDeviceEnumerator = NULL;
     if(hr == S_OK) //
       {
       IAudioEndpointVolume *EndPointVolume = NULL;
       hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
                                    CLSCTX_ALL, NULL, (LPVOID *)&EndPointVolume);
       defaultDevice->Release();
       defaultDevice = NULL;       

       float Volume = 0; //Scalar volume is from zero to 1.00 in 0.01 step
       hr = EndPointVolume->GetMasterVolumeLevelScalar(&Volume);
       if(hr == S_OK) //       
         {
         std::cout <<  "\nVolume is now " << Volume;
         std::cout << "\nPlease enter a volume value between zero and 100: ";
         std::cin >> Volume;
         if (Volume >= 1) 
           {
           Volume = Volume / 100; //Adjust to zero to 1.00 in 0.01 step
           }
         hr = EndPointVolume->SetMasterVolumeLevelScalar((float)Volume, NULL);
         if(hr == S_OK) //       
           {
           std::cout <<  "\nVolume is set to " << Volume * 100;
           }
         else
           {
           std::cout <<  "\nInvalid value ";
           }
         EndPointVolume->Release();
         EndPointVolume = NULL;  
         }
       }
     }
     CoUninitialize();
   }

 Beep(1000, 100);

 std::cout << "\nSpace or click..."; while (GetKeyState(VK_SPACE) + GetKeyState(VK_LBUTTON) >= 0){Sleep(20);}

 return 0;
}  


Are u C programmer root? :) @Neahle #include <stdio> is enough.and add using namespace std; before of int main() motor func.
Topic archived. No new replies allowed.