示例#1
0
 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));
 }
示例#2
0
 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);
 }
示例#3
0
        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);
        }