' Translated to Liberty BASIC by Richard Russell from the code here, 13-Oct-2016:
' http://www.codeproject.com/Tips/233484/Change-Master-Volume-in-Visual-Cplusplus
result = ChangeVolume(0.5, 1)
end
function ChangeVolume(nVolume, bScalar)
open "ole32.dll" for dll as #ole32
open "oleaut32.dll" for dll as #oleaut32
calldll #ole32, "CoInitialize", 0 as long, r as void
IID.MMDeviceEnumerator$ = UUID$("{A95664D2-9614-4F35-A746-DE8DB63617E6}")
CLSID.MMDeviceEnumerator$ = UUID$("{BCDE0395-E52F-467C-8E3D-C4579291692E}")
IID.IAudioEndpointVolume$ = UUID$("{5CDF2C82-841E-4546-9722-0CF74078229A}")
CLSCTX.INPROC.SERVER = 1
eRender = 0
eConsole = 0
Release = 2
Activate = 3
GetDefaultAudioEndpoint = 4
SetMasterVolumeLevel = 6
SetMasterVolumeLevelScalar = 7
GetMasterVolumeLevel = 8
GetMasterVolumeLevelScalar = 9
struct IMM, deviceEnumerator as ulong
calldll #ole32, "CoCreateInstance", CLSID.MMDeviceEnumerator$ as ptr, _
0 as long, CLSCTX.INPROC.SERVER as long, IID.MMDeviceEnumerator$ as ptr, _
IMM as struct, hr as long
' hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
struct default, Device as ulong
struct parms, dataFlow as long, role as long, ppDevice as ptr
parms.dataFlow.struct = eRender
parms.role.struct = eConsole
parms.ppDevice.struct = default.struct
hr = CallMethod(IMM.deviceEnumerator.struct, GetDefaultAudioEndpoint, parms.struct)
default.struct = parms.ppDevice.struct
' hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
' CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
struct Iaudio, endpointVolume as ulong
struct parms, iid as ptr, dwClsCtx as long, _
pActivationParams as ulong, ppInterface as ptr
parms.iid.struct = IID.IAudioEndpointVolume$
parms.dwClsCtx.struct = CLSCTX.INPROC.SERVER
parms.ppInterface.struct = Iaudio.struct
hr = CallMethod(default.Device.struct, Activate, parms.struct)
Iaudio.struct = parms.ppInterface.struct
' defaultDevice->Release();
hr = CallMethod(default.Device.struct, Release, "")
default.Device.struct = 0
' endpointVolume->GetMasterVolumeLevel(ĪtVolume);
struct Volume, float as long
struct parms, pfLevel as ptr
parms.pfLevel.struct = Volume.struct
hr = CallMethod(Iaudio.endpointVolume.struct, GetMasterVolumeLevel, parms.struct)
Volume.struct = parms.pfLevel.struct
' Convert float to double:
struct current, Volume as double
float = Volume.float.struct
calldll #oleaut32, "VarR8FromR4", float as long, current as struct, r as void
print "Current volume (dB) = "; current.Volume.struct
' hr = endpointVolume->GetMasterVolumeLevelScalar(ĪtVolume);
hr = CallMethod(Iaudio.endpointVolume.struct, GetMasterVolumeLevelScalar, parms.struct)
Volume.struct = parms.pfLevel.struct
' Convert float to double:
float = Volume.float.struct
calldll #oleaut32, "VarR8FromR4", float as long, current as struct, r as void
print "Current volume (scalar) = "; current.Volume.struct
' Set new volume:
struct parms, fLevel as ptr, EventContext as long
calldll #oleaut32, "VarR4FromR8", nVolume as double, parms as struct, r as void
if bScalar = 0 then
hr = CallMethod(Iaudio.endpointVolume.struct, SetMasterVolumeLevel, parms.struct)
else
hr = CallMethod(Iaudio.endpointVolume.struct, SetMasterVolumeLevelScalar, parms.struct)
end if
' endpointVolume->Release();
hr = CallMethod(Iaudio.endpointVolume.struct, Release, "")
Iaudio.endpointVolume.struct = 0
calldll #ole32, "CoUninitialize", r as void
close #oleaut32
close #ole32
end function
function UUID$(iid$)
l = len(iid$) + 1
wide$ = space$(2 * l) + chr$(0)
calldll #kernel32, "MultiByteToWideChar", 0 as long, 0 as long, _
iid$ as ptr, -1 as long, wide$ as ptr, l as long, r as long
UUID$ = space$(16) + chr$(0)
calldll #ole32, "CLSIDFromString", wide$ as ptr, UUID$ as ptr, r as long
end function
function CallMethod(object, method, parm$)
code$ = chr$(139)+"D$"+chr$(4)+chr$(139)+"T$"+chr$(8)+chr$(139)+"L$" _
+ chr$(16)+"VW"+chr$(139)+"t$"+chr$(20)+chr$(43)+chr$(225)+chr$(139) _
+ chr$(252)+chr$(243)+chr$(164)+chr$(80)+chr$(139)+chr$(0)+chr$(255) _
+ chr$(20)+chr$(144)+chr$(95)+chr$(94)+chr$(194)+chr$(16)+chr$(0)
p$ = parm$
n = len(p$)
calldll #user32, "CallWindowProcA", code$ as ptr, object as long,_
method as long, p$ as ptr, n as long, CallMethod as long
end function
DesiredVolume=10'volume as a percentage from 0 to 100, as Windows shows it in the master volume control
ResetTime=.1'time in minutes to wait after volume has been increased
[loop1]
scan
calldll #kernel32, "Sleep", 10 as long, re as void
CurrentVolume=GetVolume()
if CurrentVolume > DesiredVolume then
StartTime=GetTickCount()
goto [loop2]
end if
goto [loop1]
[loop2]
scan
calldll #kernel32, "Sleep", 10 as long, re as void
CurrentTime=GetTickCount()
if CurrentTime-StartTime > ResetTime*60*1000 then
call SetVolume DesiredVolume,1
goto [loop1]
end if
goto [loop2]
function GetVolume()
open "ole32.dll" for dll as #ole32
open "oleaut32.dll" for dll as #oleaut32
calldll #ole32, "CoInitialize", 0 as long, r as void
IID.MMDeviceEnumerator$ = UUID$("{A95664D2-9614-4F35-A746-DE8DB63617E6}")
CLSID.MMDeviceEnumerator$ = UUID$("{BCDE0395-E52F-467C-8E3D-C4579291692E}")
IID.IAudioEndpointVolume$ = UUID$("{5CDF2C82-841E-4546-9722-0CF74078229A}")
CLSCTX.INPROC.SERVER = 1
eRender = 0
eConsole = 0
Release = 2
Activate = 3
GetDefaultAudioEndpoint = 4
GetMasterVolumeLevel = 8
GetMasterVolumeLevelScalar = 9
struct IMM, deviceEnumerator as ulong
calldll #ole32, "CoCreateInstance", CLSID.MMDeviceEnumerator$ as ptr, _
0 as long, CLSCTX.INPROC.SERVER as long, IID.MMDeviceEnumerator$ as ptr, _
IMM as struct, hr as long
' hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
struct default, Device as ulong
struct parms, dataFlow as long, role as long, ppDevice as ptr
parms.dataFlow.struct = eRender
parms.role.struct = eConsole
parms.ppDevice.struct = default.struct
hr = CallMethod(IMM.deviceEnumerator.struct, GetDefaultAudioEndpoint, parms.struct)
default.struct = parms.ppDevice.struct
' hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
' CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
struct Iaudio, endpointVolume as ulong
struct parms, iid as ptr, dwClsCtx as long, _
pActivationParams as ulong, ppInterface as ptr
parms.iid.struct = IID.IAudioEndpointVolume$
parms.dwClsCtx.struct = CLSCTX.INPROC.SERVER
parms.ppInterface.struct = Iaudio.struct
hr = CallMethod(default.Device.struct, Activate, parms.struct)
Iaudio.struct = parms.ppInterface.struct
' defaultDevice->Release();
hr = CallMethod(default.Device.struct, Release, "")
default.Device.struct = 0
' endpointVolume->GetMasterVolumeLevel(ĪtVolume);
struct Volume, float as long
struct parms, pfLevel as ptr
parms.pfLevel.struct = Volume.struct
hr = CallMethod(Iaudio.endpointVolume.struct, GetMasterVolumeLevel, parms.struct)
Volume.struct = parms.pfLevel.struct
' Convert float to double:
struct current, Volume as double
float = Volume.float.struct
calldll #oleaut32, "VarR8FromR4", float as long, current as struct, r as void
' hr = endpointVolume->GetMasterVolumeLevelScalar(ĪtVolume);
hr = CallMethod(Iaudio.endpointVolume.struct, GetMasterVolumeLevelScalar, parms.struct)
Volume.struct = parms.pfLevel.struct
' Convert float to double:
float = Volume.float.struct
calldll #oleaut32, "VarR8FromR4", float as long, current as struct, r as void
GetVolume=int(current.Volume.struct*100+1)
' endpointVolume->Release();
hr = CallMethod(Iaudio.endpointVolume.struct, Release, "")
Iaudio.endpointVolume.struct = 0
calldll #ole32, "CoUninitialize", r as void
close #oleaut32
close #ole32
end function
sub SetVolume nVolume, bScalar
nVolume=nVolume/100
open "ole32.dll" for dll as #ole32
open "oleaut32.dll" for dll as #oleaut32
calldll #ole32, "CoInitialize", 0 as long, r as void
IID.MMDeviceEnumerator$ = UUID$("{A95664D2-9614-4F35-A746-DE8DB63617E6}")
CLSID.MMDeviceEnumerator$ = UUID$("{BCDE0395-E52F-467C-8E3D-C4579291692E}")
IID.IAudioEndpointVolume$ = UUID$("{5CDF2C82-841E-4546-9722-0CF74078229A}")
CLSCTX.INPROC.SERVER = 1
eRender = 0
eConsole = 0
Release = 2
Activate = 3
GetDefaultAudioEndpoint = 4
SetMasterVolumeLevelScalar = 7
struct IMM, deviceEnumerator as ulong
calldll #ole32, "CoCreateInstance", CLSID.MMDeviceEnumerator$ as ptr, _
0 as long, CLSCTX.INPROC.SERVER as long, IID.MMDeviceEnumerator$ as ptr, _
IMM as struct, hr as long
' hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
struct default, Device as ulong
struct parms, dataFlow as long, role as long, ppDevice as ptr
parms.dataFlow.struct = eRender
parms.role.struct = eConsole
parms.ppDevice.struct = default.struct
hr = CallMethod(IMM.deviceEnumerator.struct, GetDefaultAudioEndpoint, parms.struct)
default.struct = parms.ppDevice.struct
' hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
' CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
struct Iaudio, endpointVolume as ulong
struct parms, iid as ptr, dwClsCtx as long, _
pActivationParams as ulong, ppInterface as ptr
parms.iid.struct = IID.IAudioEndpointVolume$
parms.dwClsCtx.struct = CLSCTX.INPROC.SERVER
parms.ppInterface.struct = Iaudio.struct
hr = CallMethod(default.Device.struct, Activate, parms.struct)
Iaudio.struct = parms.ppInterface.struct
' defaultDevice->Release();
hr = CallMethod(default.Device.struct, Release, "")
default.Device.struct = 0
' Set new volume:
struct Volume, float as long
struct parms, fLevel as ptr, EventContext as long
calldll #oleaut32, "VarR4FromR8", nVolume as double, parms as struct, r as void
hr = CallMethod(Iaudio.endpointVolume.struct, SetMasterVolumeLevelScalar, parms.struct)
' endpointVolume->Release();
hr = CallMethod(Iaudio.endpointVolume.struct, Release, "")
Iaudio.endpointVolume.struct = 0
calldll #ole32, "CoUninitialize", r as void
close #oleaut32
close #ole32
end sub
function UUID$(iid$)
l = len(iid$) + 1
wide$ = space$(2 * l) + chr$(0)
calldll #kernel32, "MultiByteToWideChar", 0 as long, 0 as long, _
iid$ as ptr, -1 as long, wide$ as ptr, l as long, r as long
UUID$ = space$(16) + chr$(0)
calldll #ole32, "CLSIDFromString", wide$ as ptr, UUID$ as ptr, r as long
end function
function CallMethod(object, method, parm$)
code$ = chr$(139)+"D$"+chr$(4)+chr$(139)+"T$"+chr$(8)+chr$(139)+"L$" _
+ chr$(16)+"VW"+chr$(139)+"t$"+chr$(20)+chr$(43)+chr$(225)+chr$(139) _
+ chr$(252)+chr$(243)+chr$(164)+chr$(80)+chr$(139)+chr$(0)+chr$(255) _
+ chr$(20)+chr$(144)+chr$(95)+chr$(94)+chr$(194)+chr$(16)+chr$(0)
p$ = parm$
n = len(p$)
calldll #user32, "CallWindowProcA", code$ as ptr, object as long,_
method as long, p$ as ptr, n as long, CallMethod as long
end function
function GetTickCount()
calldll #kernel32, "GetTickCount",_
GetTickCount as ulong
end function