Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using Naudio to access audio device properties and receive device connected or removed notification.

i am using MMDeviceEnumerator's RegisterEndpointNotificationCallback(IMMNotificationClient) register for notifications.

The code compiles and runs but when is connect a jack device i recieve. the following errors.

System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.Marshal.AddRef(IntPtr pUnk)
at System.Management.SinkForEventQuery.Indicate(IntPtr pWbemClassObject)
InnerException:
FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error. The address of the error was at 0x6aab9017, on thread 0x1f10. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Below is the class i have created that implements Naudio's IMMNotificationClient Interface


C#
class NotificationClientImplementation : NAudio.CoreAudioApi.Interfaces.IMMNotificationClient
    {

public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId)
        {
            Console.WriteLine("OnDefaultDeviceChanged --> {0}", dataFlow.ToString());
        }

        public void OnDeviceAdded(string deviceId)
        {
            Console.WriteLine("OnDeviceAdded -->");
        }

        public void OnDeviceRemoved(string deviceId)
        {
            Console.WriteLine("OnDeviceRemoved -->");
        }

        public void OnDeviceStateChanged(string deviceId, DeviceState newState)
        {
            Console.WriteLine("OnDeviceStateChanged\n Device Id -->{0} : Device State {1}", deviceId, newState);
        }

        public NotificationClientImplementation()
        {
            if (System.Environment.OSVersion.Version.Major < 6)
            {
                throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
            }
        }

        public void OnPropertyValueChanged(string deviceId, PropertyKey propertyKey)
        {
            //  Console.WriteLine("OnPropertyValueChanged:  fmtid --> {0}  PID--> {1}", propertyKey.fmtid.ToString(), propertyKey.pid.ToString());
        }

    }

NAUDIO Enums ::::
public enum Role
    {
       Console,
       Multimedia,
       Communications,
    }
[Flags]
    public  enum DeviceState
    {
      Active = 0x00000001,
      Disabled = 0x00000002,
      NotPresent = 0x00000004,
      Unplugged = 0x00000008,
      All = 0x0000000F
    }

public enum DataFlow
    {
       Render,
       Capture,
       All
    };

NAudio IMMNotificationClient:::: 

    [Guid("7991EEC9-7E89-4D85-8390-6C703CEC60C0"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public partial interface IMMNotificationClient
    {
        void OnDeviceStateChanged(
            [MarshalAs(UnmanagedType.LPWStr)] string deviceId,
            [MarshalAs(UnmanagedType.U4)] DeviceState newState);

        void OnDeviceAdded(
            [MarshalAs(UnmanagedType.LPWStr)] string deviceId);

        void OnDeviceRemoved(
            [MarshalAs(UnmanagedType.LPWStr)] string deviceId);

void OnDefaultDeviceChanged(
            [MarshalAs(UnmanagedType.I4)] DataFlow dataFlow,
            [MarshalAs(UnmanagedType.I4)] Role deviceRole,
            [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId);

        void OnPropertyValueChanged(
            [MarshalAs(UnmanagedType.LPWStr)] string deviceId, PropertyKey propertyKey);

    }


Ours is a Winforms application and my first encounter with COM . Please Help.
Thanks.
Posted
Comments
Bernhard Hiller 11-Feb-14 9:20am    
NAudio is a .Net application, isn't it? So why do you use it via COM instead of its .Net libraries?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900