1

I've been trying to get the current system volume using C# / Windows API. I'm on Windows 8.1 though I would like the solution to also work on Windows 7.

This:

[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

does not work, with the system volume after Windows XP.

I have tried this:

    [DllImport("Audioses.dll", EntryPoint = "GetMasterVolume", SetLastError = true)]
    static extern int GetMasterVolume(out float pfLevelDB);

    public void getVolume()
    {
        float f = 0.0F;
        int i = GetMasterVolume(out f);

        MessageBox.Show(f.ToString());
    }

However the application never gets to the MessageBox.Show(...), though running line-by-line shows that it gets to GetMasterVolume(out f) then fails. I think something must be wrong with my declaration or usage.

Output shows: System.EntryPointNotFoundException

GetMasterVolumeLevel: http://msdn.microsoft.com/en-us/library/windows/desktop/dd316533(v=vs.85).aspx

Lucas Goossen
  • 527
  • 7
  • 15
csm10495
  • 569
  • 6
  • 12
  • related: http://stackoverflow.com/questions/4629816/get-master-volume-in-windows-xp-vista-seven-the-one-increased-through-keyboard – Blorgbeard Oct 15 '14 at 19:55
  • `DllImport` does not apply to `GetMasterVolume`, you just need a proper use of this API... – Roman R. Oct 15 '14 at 20:23

1 Answers1

0

EntryPointNotFound indicates that the declaration is indeed mistaken, and your implementation cannot find the method you're requesting within the specified dll. The biggest I see here is that the ISimpleAudioVolume is an interface, and thus the desired method is an instance, not a static, method, and requires more work to grab an instance of the appropriate type and return the desired volume levels. Note that MSDN and on the interface itself mentions needing to start an audio session to operate through this interface.

David
  • 10,458
  • 1
  • 28
  • 40