public static T ActivateInterface <T>(this MMDeviceAPI.IMMDevice device) where T : class { var guid = new System.Guid(typeof(T).GetCustomAttribute <System.Runtime.InteropServices.GuidAttribute>(false).Value); // Normally we're supposed to pass NULL as pActivationParams but C# won't let us pass null to a ref struct argument. Passing an empty PROPVARIANT seems to work fine. var activationParams = new MMDeviceAPI.tag_inner_PROPVARIANT(); device.Activate(ref guid, (uint)CLSCTX.CLSCTX_ALL, ref activationParams, out var interfacePtr); object iface; try { iface = System.Runtime.InteropServices.Marshal.GetTypedObjectForIUnknown(interfacePtr, typeof(T)); } finally { System.Runtime.InteropServices.Marshal.Release(interfacePtr); } return(iface as T); }
public static MMDeviceAPI.IPropertyStore GetPropertyStore(this MMDeviceAPI.IMMDevice device, uint sgtmAccess) { device.OpenPropertyStore(MMDeviceAPIHelpers.STGM_READ, out var propertyStore); return(propertyStore); }
public static string Id(this MMDeviceAPI.IMMDevice device) { device.GetId(out var id); return(id); }