public static T GetRcwForInterfacePtr <T>(IntPtr ptr, bool addRef) where T : class, IUnknown { if (ptr == IntPtr.Zero) { return(null); } return(InterfaceRuntimeInfo <T> .CreateRcw(ptr, addRef)); }
public static T QueryInterface <T>(IUnknown obj) where T : class, IUnknown { if (!(obj is T)) { var guid = InterfaceRuntimeInfo <T> .ComGuid; var ptr = obj.QueryNativeInterface(ref guid); return(InterfaceRuntimeInfo <T> .CreateRcw(ptr, true)); } return((T)obj); }
public static T QueryInterfaceOrNull <T>(IUnknown obj) where T : class, IUnknown { var casted = obj as T; if (casted == null) { const int E_NOINTERFACE = unchecked ((int)0x80004002); try { var guid = InterfaceRuntimeInfo <T> .ComGuid; var ptr = obj.QueryNativeInterface(ref guid); return(InterfaceRuntimeInfo <T> .CreateRcw(ptr, true)); } catch (COMException ex) when(ex.HResult == E_NOINTERFACE) { return(null); } } return(casted); }