diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs index 9efbcaabe..4f84cff2e 100644 --- a/Source/Engine/Engine/NativeInterop.cs +++ b/Source/Engine/Engine/NativeInterop.cs @@ -400,12 +400,16 @@ namespace FlaxEngine public bool IsAllocated => handle != IntPtr.Zero; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator ManagedHandle(IntPtr value) => FromIntPtr(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ManagedHandle FromIntPtr(IntPtr value) => new ManagedHandle(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator IntPtr(ManagedHandle value) => ToIntPtr(value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IntPtr ToIntPtr(ManagedHandle value) => value.handle; public override int GetHashCode() => handle.GetHashCode(); @@ -1583,6 +1587,8 @@ namespace FlaxEngine if (type == typeof(IntPtr)) managedValue = (T)(object)nativePtr; + else if (type == typeof(ManagedHandle)) + managedValue = (T)(object)ManagedHandle.FromIntPtr(nativePtr); else if (MarshalHelper.marshallableFields != null) { IntPtr fieldPtr = nativePtr; @@ -2302,6 +2308,8 @@ namespace FlaxEngine return ManagedString.ToNative(Unsafe.As(returnObject)); else if (methodHolder.method.ReturnType == typeof(IntPtr)) return (IntPtr)returnObject; + else if (methodHolder.method.ReturnType == typeof(ManagedHandle)) + return ManagedHandle.ToIntPtr((ManagedHandle)(object)returnObject); else if (methodHolder.method.ReturnType == typeof(bool)) return (bool)returnObject ? boolTruePtr : boolFalsePtr; else if (methodHolder.method.ReturnType == typeof(Type)) @@ -2902,6 +2910,8 @@ namespace FlaxEngine return ManagedString.ToNative(Unsafe.As(returnObject)); else if (method.ReturnType == typeof(IntPtr)) return (IntPtr)returnObject; + else if (method.ReturnType == typeof(ManagedHandle)) + return ManagedHandle.ToIntPtr((ManagedHandle)returnObject); else if (method.ReturnType == typeof(bool)) return (bool)returnObject ? boolTruePtr : boolFalsePtr; else if (method.ReturnType == typeof(Type)) diff --git a/Source/Engine/Engine/NativeInterop_Invoker.cs b/Source/Engine/Engine/NativeInterop_Invoker.cs index 38386d31a..21f9770e9 100644 --- a/Source/Engine/Engine/NativeInterop_Invoker.cs +++ b/Source/Engine/Engine/NativeInterop_Invoker.cs @@ -55,6 +55,8 @@ namespace FlaxEngine return ManagedString.ToNative(Unsafe.As(returnValue)); else if (typeof(TRet) == typeof(IntPtr)) return (IntPtr)(object)returnValue; + else if (typeof(TRet) == typeof(ManagedHandle)) + return ManagedHandle.ToIntPtr((ManagedHandle)(object)returnValue); else if (typeof(TRet) == typeof(bool)) return (bool)(object)returnValue ? boolTruePtr : boolFalsePtr; else if (typeof(TRet) == typeof(Type))