Fix ManagedHandle marshalling

This commit is contained in:
2022-12-31 15:30:58 +02:00
parent 8b80f73641
commit 2f507091b2
2 changed files with 12 additions and 0 deletions

View File

@@ -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<T>.marshallableFields != null)
{
IntPtr fieldPtr = nativePtr;
@@ -2302,6 +2308,8 @@ namespace FlaxEngine
return ManagedString.ToNative(Unsafe.As<string>(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<string>(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))

View File

@@ -55,6 +55,8 @@ namespace FlaxEngine
return ManagedString.ToNative(Unsafe.As<string>(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))