Skip to content

Instantly share code, notes, and snippets.

@rdp
Last active July 12, 2022 18:48
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rdp/8363580 to your computer and use it in GitHub Desktop.
Save rdp/8363580 to your computer and use it in GitHub Desktop.
// to compile (using mingw-w64)
// g++ this_filename.c -lole32
// outputs current system volume (out of 0-100) to stdout, ex: output "23"
// mostly gleaned from examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd370839(v=vs.85).aspx
// download a compiled version here:
// https://sourceforge.net/projects/mplayer-edl/files/adjust_get_current_system_volume_vista_plus.exe.exe (updated, can set it too now!)
#include <windows.h>
#include <commctrl.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <stdio.h>
#include <math.h> /* log */
#define EXIT_ON_ERROR(hr) \
if (FAILED(hr)) { printf("error %d occurred\n", -hr); goto Exit; }
#define SAFE_RELEASE(punk) \
if ((punk) != NULL) \
{ (punk)->Release(); (punk) = NULL; }
int main(int argc, char** argv) {
IAudioEndpointVolume *g_pEndptVol = NULL;
bool WindowsLH;
HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDevice *pDevice = NULL;
OSVERSIONINFO VersionInfo;
ZeroMemory(&VersionInfo, sizeof(OSVERSIONINFO));
VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&VersionInfo);
if (VersionInfo.dwMajorVersion > 5)
WindowsLH = true; // vista+
else
WindowsLH = false;
if (WindowsLH)
{
CoInitialize(NULL);
// Get enumerator for audio endpoint devices.
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
EXIT_ON_ERROR(hr)
// Get default audio-rendering device.
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
EXIT_ON_ERROR(hr)
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume),
CLSCTX_ALL, NULL, (void**)&g_pEndptVol);
EXIT_ON_ERROR(hr)
float currentVal;
if(argc == 2) {
if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "/?")==0 || strcmp(argv[1], "--help")==0 ) {
printf("sets/gest current master volume level [vista+]\nSyntax: either no args to display current value, or a number to set it, 0-100. It prints out the new or current value.");
goto Exit;
}
float got = (float) atof(argv[1])/100.0; // needs to be within 1.0 to 0.0
hr = g_pEndptVol->SetMasterVolumeLevelScalar(got, NULL);
EXIT_ON_ERROR(hr)
}
hr = g_pEndptVol->GetMasterVolumeLevelScalar(&currentVal);
EXIT_ON_ERROR(hr)
printf("%d", (int) round(100 * currentVal)); // 0.839999 to 84 :)
}
else {
printf("wrong v of windows, req. vista+ ! \n");
}
fflush(stdout); // just in case
Exit:
SAFE_RELEASE(pEnumerator)
SAFE_RELEASE(pDevice)
SAFE_RELEASE(g_pEndptVol)
CoUninitialize();
return 0;
}
@rdp
Copy link
Author

rdp commented Jan 13, 2014

note to self, how to compile it: g++ -IC:\downloads\i686-4.8.2-release-posix-sjlj-rt_v3-rev2\mingw32\i686-w64-mingw32\include go.c -lole32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment