From 9d83f279e180ccb095ae6baa075b9d1d82064b9c Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 8 Jan 2019 08:17:45 -0700 Subject: [PATCH] Added QueryInterface method for objects --- Core/Extensions/InteropExtensions.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index 5d782cd4..2533275d 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -195,6 +195,19 @@ namespace Vanara.Extensions /// A new pointer that reflects the addition of to . public static IntPtr Offset(this IntPtr pointer, long offset) => new IntPtr(pointer.ToInt64() + offset); + /// Queries the object for a COM interface and returns it, if found, in . + /// The object to query. + /// The interface identifier (IID) of the requested interface. + /// When this method returns, contains a reference to the returned interface. + /// An HRESULT that indicates the success or failure of the call. + public static int QueryInterface(object iUnk, in Guid iid, out object ppv) + { + var tmp = iid; + var hr = Marshal.QueryInterface(Marshal.GetIUnknownForObject(iUnk), ref tmp, out var ippv); + ppv = hr == 0 ? Marshal.GetObjectForIUnknown(ippv) : null; + return hr; + } + /// Marshals data from a managed object to an unmanaged block of memory that is allocated using . /// The type of the managed object. ///