public static object ObjectForWrapper(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return(null);
            }
            ManagedValue val = FromHandle(ptr);

            return(val == null ? null : val.Instance);
        }
        static ManagedValue FromHandle(IntPtr ptr)
        {
            GCHandle     gch = (GCHandle)ptr;
            ManagedValue val = gch.Target as ManagedValue;

            if (val == null)
            {
                throw new Exception("Unexpected GCHandle received.");
            }
            return(val);
        }
        public static void ReleaseWrapper(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return;
            }

            ManagedValue val = FromHandle(ptr);

            val.Unref();
        }
 static void Free(IntPtr ptr)
 {
     try {
         if (ptr == IntPtr.Zero)
         {
             return;
         }
         ManagedValue val = FromHandle(ptr);
         val.Unref();
     } catch (Exception e) {
         ExceptionManager.RaiseUnhandledException(e, false);
     }
 }
        static IntPtr Copy(IntPtr ptr)
        {
            try {
                if (ptr == IntPtr.Zero)
                {
                    return(ptr);
                }
                ManagedValue val = FromHandle(ptr);
                val.Ref();
                return(ptr);
            } catch (Exception e) {
                ExceptionManager.RaiseUnhandledException(e, false);
            }

            return(IntPtr.Zero);
        }