因为网上关于API控制系统音量的资料都是C++的,于是我先用C++写了个。这个程序能设置系统的当前音量。有时间用汇编再写一遍,主要里面的用到的结构定义很繁琐。一份好的参考资料:
http://202.38.64.13/personal/cs011/csli/vc_note/MIXER.htm
PS:无语掉。程序拿到Win7后说不行。在CSDN上查说,Vista之后,设置全局的音量的话用ISimpleAudioVolume::SetMasterVolume 。。。。
http://topic.csdn.net/u/20100930/18/f6ad08c7-43e6-4c23-a70f-3c8a3cd4f06c.html
无奈说下面是XP版控制音量:
Win7版:
参考资料:
http://msdn.microsoft.com/en-us/library/dd370892(v=VS.85).aspx
http://www.cnblogs.com/gingerjws/archive/2010/10/28/1863560.html
http://www.portaudio.com/docs/v19-doxydocs/endpointvolume_8h-source.html
.386
.model flat,stdcall
option casemap:none
include e:/masm32/include/windows.inc
include e:/masm32/include/kernel32.inc
includelib e:/masm32/lib/kernel32.lib
include e:/masm32/include/user32.inc
includelib e:/masm32/lib/user32.lib
include e:/masm32/include/ole32.inc
includelib e:/masm32/lib/ole32.lib
eRender equ 0
eConsole equ 0
.data?
lpEnumerator dd ?
lpDevice dd ?
lpAudioEndpointVolume dd ?
fLevel dd ?
bMute db ?
szBuffer db 32 dup (?)
.data
;CLSID_MMDeviceEnumerator = {BCDE0395-E52F-467C-8E3D-C4579291692E}
;IID_IMMDeviceEnumerator = {A95664D2-9614-4F35-A746-DE8DB63617E6}
;IID_IAudioEndpointVolume = {5CDF2C82-841E-4546-9722-0CF74078229A}
CLSID_MMDeviceEnumerator GUID <0BCDE0395h,0E52Fh,467Ch,<8Eh,3Dh,0C4h,57h,92h,91h,69h,2Eh>>
IID_IMMDeviceEnumerator GUID <0A95664D2h,9614h,4F35h,<0A7h,46h,0DEh,8Dh,0B6h,36h,17h,0E6h>>
IID_IAudioEndpointVolume GUID <5CDF2C82h,841Eh,4546h,<97h,22h,0Ch,0F7h,40h,78h,22h,9Ah>>
.code
_MsgBoxHex proc uses ebx edi esi,_dwInt
local @szBuffer[9]:byte
local @szFormat[5]:byte
push eax
mov @szFormat[0],'%'
mov @szFormat[1],'0'
mov @szFormat[2],'8'
mov @szFormat[3],'X'
mov @szFormat[4],0
invoke wsprintf,addr @szBuffer,addr @szFormat,_dwInt
invoke MessageBox,NULL,addr @szBuffer,addr @szFormat,MB_OK
pop eax
ret
_MsgBoxHex endp
start:
invoke CoInitialize,NULL
invoke CoCreateInstance,offset CLSID_MMDeviceEnumerator,NULL,/
CLSCTX_INPROC_SERVER,offset IID_IMMDeviceEnumerator,/
offset lpEnumerator
;Enumerator.GetDefaultAudioEndpoint方法
mov esi,lpEnumerator
mov esi,[esi]
push offset lpDevice
push eConsole
push eRender
push lpEnumerator
call dword ptr [esi+16]
;Device.Activate方法
mov esi,lpDevice
mov esi,[esi]
push offset lpAudioEndpointVolume
push NULL
push CLSCTX_ALL
push offset IID_IAudioEndpointVolume
push lpDevice
call dword ptr [esi+12]
;AudioEndpointVolume.GetMasterVolumeLevelScalar方法,获取音量大小
mov esi,lpAudioEndpointVolume
mov esi,[esi]
push offset fLevel
push lpAudioEndpointVolume
call dword ptr [esi+36]
;显示fLevelDB在内存中的浮点型数据
invoke _MsgBoxHex,fLevel
;此处将令fLevel为3F333333h,即0.7
mov fLevel,3F333333h
;AudioEndpointVolume.SetMasterVolumeLevelScalar方法,设置音量大小,设置范围0~1
mov esi,lpAudioEndpointVolume
mov esi,[esi]
push NULL
push fLevel
push lpAudioEndpointVolume
call dword ptr [esi+28]
;AudioEndpointVolume.GetMute方法,获取静音状态
;mov esi,lpAudioEndpointVolume
;mov esi,[esi]
;push offset bMute
;push lpAudioEndpointVolume
;call dword ptr [esi+60]
;AudioEndpointVolume.SetMute方法,更改静音状态
;mov esi,lpAudioEndpointVolume
;mov esi,[esi]
;push NULL
;not bMute
;movzx eax,bMute
;push eax
;push lpAudioEndpointVolume
;call dword ptr [esi+56]
;Release方法
mov esi,lpAudioEndpointVolume
mov esi,[esi]
push lpAudioEndpointVolume
call dword ptr [esi+8]
mov esi,lpDevice
mov esi,[esi]
push lpDevice
call dword ptr [esi+8]
mov esi,lpEnumerator
mov esi,[esi]
push lpEnumerator
call dword ptr [esi+8]
invoke CoUninitialize
invoke ExitProcess,NULL
end start