1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
#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; }
using namespace std;
int main()