Visual Basic - How to get volume level in Windows Vista/7


  • How can I get the master volume level in Visual Basic? Must work on Vista and 7.


    Sunday, April 18, 2010 3:27 PM


All replies

  • Try this code:

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices

    Public Class Form1

        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.TrackBar1 = New System.Windows.Forms.TrackBar()
            Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
            CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.TrackBar1.Location = New System.Drawing.Point(12, 12)
            Me.TrackBar1.Name = "TrackBar1"
            Me.TrackBar1.Orientation = System.Windows.Forms.Orientation.Vertical
            Me.TrackBar1.Size = New System.Drawing.Size(45, 240)
            Me.TrackBar1.TabIndex = 0
            Me.ClientSize = New System.Drawing.Size(108, 264)
            Me.Name = "Form1"
            CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()

        End Sub

        <DllImport("winmm.dll")> _
        Public Shared Function waveOutGetVolume(ByVal hwo As IntPtr, ByRef dwVolume As UInteger) As Integer
        End Function

        <DllImport("winmm.dll")> _
        Public Shared Function waveOutSetVolume(ByVal hwo As IntPtr, ByVal dwVolume As UInteger) As Integer
        End Function

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            On Error Resume Next
            Timer1.Enabled = True
        End Sub

        Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
            On Error Resume Next
            Dim NewVolume As Integer = ((UShort.MaxValue / 10) * TrackBar1.Value)
             Dim NewVolumeAllChannels As UInteger = ((CUInt(NewVolume) And &HFFFF) Or (CUInt(NewVolume) << 16))
             waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels)
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            On Error Resume Next
            Dim CurrVol As UInteger = 0
            waveOutGetVolume(IntPtr.Zero, CurrVol)
            Dim CalcVol As UShort = CUShort((CurrVol And &HFFFF))
            TrackBar1.Value = CalcVol / (UShort.MaxValue / 10)
        End Sub
    End Class
    by naldo

    Tuesday, April 20, 2010 7:10 AM
  • Hello grantman16,
    Thanks for your post.
    (1) If you would like to get master volume, I suggest you to check out the following threads, I hope they are helpful.
    (2) Here is also a thread where the author asks the similar question, but it is about C#. If you understand C#, it could be very useful. Or you may try to make a converation.
    Convert C# to VB.NET:

    For more information, you can see:
    (1)GetMasterVolume method retrieves the client volume level for the audio session.
    (2)SetMasterVolume: The SetMasterVolume method sets the master volume level for the audio session
    I hope they are helpful.
    Best regards,

    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Friday, April 23, 2010 2:48 AM
  • Thanks for your posts, but unfortunately, I can't get anything to work.

    Naldopr, I've tried using "waveOutGetVolume", but it always returns the same value no matter what I set my volume to.

    Liliane, your first two links are for setting the master volume, not getting it. The other links are for C++/C#, and I haven't been able to get them to work in VB.

    I'm thinking the "GetMasterVolume" function ( ) would work, but I don't know how to access "ISimpleAudioVolume" from VB. Does anyone know how?


    Friday, April 23, 2010 10:20 PM
  • @grantman16 - Here is a website that will convert C# code to VB.NET code.  There are other sites out there that will do the same thing.  I've never had a problem with this one.



    SEARCH ... then ask
    Sunday, April 25, 2010 10:52 PM