// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.Runtime.InteropServices; using System.Threading; namespace SharpDX { /// /// A COM Interface Callback /// internal abstract class ComObjectShadow : CppObjectShadow { public static Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); protected int QueryInterfaceImpl(ref Guid guid, out IntPtr output) { var shadow = (ComObjectShadow)((ShadowContainer)Callback.Shadow).FindShadow(guid); if (shadow != null) { ((IUnknown)Callback).AddReference(); output = shadow.NativePointer; return Result.Ok.Code; } output = IntPtr.Zero; return Result.NoInterface.Code; } protected virtual int AddRefImpl() { return ((IUnknown)Callback).AddReference(); } protected virtual int ReleaseImpl() { return ((IUnknown)Callback).Release(); } internal class ComObjectVtbl : CppObjectVtbl { public ComObjectVtbl(int numberOfCallbackMethods) : base(numberOfCallbackMethods + 3) { unsafe { AddMethod(new QueryInterfaceDelegate(QueryInterfaceImpl)); AddMethod(new AddRefDelegate(AddRefImpl)); AddMethod(new ReleaseDelegate(ReleaseImpl)); } } [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int QueryInterfaceDelegate(IntPtr thisObject, IntPtr guid, out IntPtr output); [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int AddRefDelegate(IntPtr thisObject); [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate int ReleaseDelegate(IntPtr thisObject); protected static int QueryInterfaceImpl(IntPtr thisObject, IntPtr guid, out IntPtr output) { var shadow = ToShadow(thisObject); if (shadow == null) { output = IntPtr.Zero; return Result.NoInterface.Code; } unsafe { return shadow.QueryInterfaceImpl(ref *((Guid*)guid), out output); } } protected static int AddRefImpl(IntPtr thisObject) { var shadow = ToShadow(thisObject); // The shadow could be null if it is released explicitly // But we are callbacked by a C++ that want to release it. if (shadow == null) return 0; return shadow.AddRefImpl(); } protected static int ReleaseImpl(IntPtr thisObject) { var shadow = ToShadow(thisObject); // The shadow could be null if it is released explicitly // But we are callbacked by a C++ that want to release it. if (shadow == null) return 0; return shadow.ReleaseImpl(); } } } }