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
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)
{
}
}
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.