Exemple #1
0
        private static T IntPtrToCallback <T>(IntPtr handle, bool unpinHandle) where T : class
        {
            if (PInvokeUtil.IsNull(handle))
            {
                return(null);
            }

            var gcHandle = GCHandle.FromIntPtr(handle);

            try
            {
                return((T)gcHandle.Target);
            }
            catch (System.InvalidCastException e)
            {
                Debug.LogError("GC Handle pointed to unexpected type: " + gcHandle.Target.ToString() +
                               ". Expected " + typeof(T));
                throw e;
            }
            finally
            {
                if (unpinHandle)
                {
                    gcHandle.Free();
                }
            }
        }
Exemple #2
0
 internal static void UnpinCallbackHandle(IntPtr handle)
 {
     if (PInvokeUtil.IsNotNull(handle))
     {
         var gcHandle = GCHandle.FromIntPtr(handle);
         gcHandle.Free();
     }
 }
Exemple #3
0
 private void Cleanup()
 {
     if (!PInvokeUtil.IsNull(mSelfPointer))
     {
         ReleaseHandle(mSelfPointer);
         mSelfPointer = new HandleRef(this, IntPtr.Zero);
     }
 }
Exemple #4
0
        public virtual bool Equals(InteropObject other)
        {
            if (null == other)
            {
                return(false);
            }

            if (PInvokeUtil.IsNull(this.mSelfPointer))
            {
                return(PInvokeUtil.IsNull(other.mSelfPointer));
            }

            return(mSelfPointer.Handle == other.mSelfPointer.Handle && this.mIsDisposed == other.mIsDisposed);
        }
Exemple #5
0
        /// <summary>
        /// Creates a managed binding object for the unmanaged object referenced by the given pointer.
        /// If such binding object exists, this method returns it instead of creating a new one.
        /// Therefore using this method guarantees that the same managed binder is always returned
        /// for the same native pointer.
        /// </summary>
        /// <returns>The binder for the given pointer, or null if the pointer is invalid.</returns>
        /// <param name="pointer">Pointer.</param>
        /// <param name="constructor">Constructor.</param>
        public static T FromPointer(IntPtr pointer, Func <IntPtr, T> constructor)
        {
            if (PInvokeUtil.IsNull(pointer))
            {
                return(default(T));
            }

            // Check if an binder exists for this IntPtr and return it.
            T binder = FindExistingBinder(pointer);

            if (binder != default(T))
            {
                return(binder);
            }

            // Otherwise create a new binder, add it to the binders map and return it.
            T newBinder = constructor(pointer);

            RegisterNewBinder(pointer, newBinder);
            return(newBinder);
        }
Exemple #6
0
 public InteropObject(IntPtr pointer)
 {
     mSelfPointer = PInvokeUtil.CheckNonNull(new HandleRef(this, pointer));
     AttachHandle(mSelfPointer);
 }
Exemple #7
0
 protected bool IsDisposed()
 {
     return(PInvokeUtil.IsNull(mSelfPointer));
 }
Exemple #8
0
 public bool IsDisposed()
 {
     return(PInvokeUtil.IsNull(mSelfPointer));
 }