morphx666 / CoreAudio

Windows CoreAudio wrapper for .NET
https://whenimbored.xfx.net/2011/01/core-audio-for-net/
MIT License
95 stars 20 forks source link

Protected memory exception when changing master volume #26

Open Vicharian opened 7 months ago

Vicharian commented 7 months ago

Had this exception when using the Nuget package.

Downloaded the code from GitHub and ran the Forms.Framework sample and got the same error. Am running it in Framework 4.8.1, but read through another issue that just changing the targeting framework should work. Should mention that changing the selected device is working just fine. Only the volume has this problem.

image

System.AccessViolationException HResult=0x80004003 Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Source= StackTrace:

Stack trace at CoreAudio.Interfaces.IAudioEndpointVolume.SetMasterVolumeLevelScalar(Single fLevel, Guid pguidEventContext) at CoreAudio.AudioEndpointVolume.set_MasterVolumeLevelScalar(Single value) in C:\Code\ExampleCode\CoreAudio-master\CoreAudio\AudioEndpointVolume.cs:line 60 at CoreAudioForms.Framework.Sample.FormMain.Master_Scroll(Object sender, EventArgs e) in C:\Code\ExampleCode\CoreAudio-master\samples\CoreAudioForms.Framework.Sample\FormMain.cs:line 35 at System.Windows.Forms.TrackBar.OnScroll(EventArgs e) at System.Windows.Forms.TrackBar.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WmVScroll(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WmMouseMove(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.TrackBar.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at CoreAudioForms.Framework.Sample.Program.Main() in C:\Code\ExampleCode\CoreAudio-master\samples\CoreAudioForms.Framework.Sample\Program.cs:line 13
morphx666 commented 7 months ago

I'm looking into this issue and will post back when I find a solution. For now, change the call to MMDeviceEnumerator passing an empty Guid as a parameter:

Like so: var devEnum = new MMDeviceEnumerator(Guid.Empty);

morphx666 commented 7 months ago

I was able to find the problem. Please take a pull from the master branch and let me know if this version works for you as well.