2 // System.Runtime.InteropServices/GCHandle.cs
5 // Ajay kumar Dwivedi (adwiv@yahoo.com) ??
6 // Paolo Molaro (lupus@ximian.com)
10 using System.Runtime.CompilerServices;
11 using System.Runtime.InteropServices;
13 namespace System.Runtime.InteropServices
16 /// Summary description for GCHandle.
18 public struct GCHandle
23 private GCHandle(IntPtr h)
29 private GCHandle(object obj)
30 : this(obj, GCHandleType.Normal)
33 private GCHandle(object value, GCHandleType type)
35 handle = GetTargetHandle (value, 0, type);
40 public bool IsAllocated
52 return GetTarget (handle);
56 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
61 public IntPtr AddrOfPinnedObject()
63 IntPtr res = GetAddrOfPinnedObject(handle);
64 if (res == IntPtr.Zero)
65 throw new InvalidOperationException("The handle is not of Pinned type");
69 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
71 return new GCHandle (value);
74 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
76 return new GCHandle (value,type);
85 public static explicit operator IntPtr (GCHandle value)
87 return (IntPtr) value.handle;
90 public static explicit operator GCHandle(IntPtr value)
92 return new GCHandle (value);
95 [MethodImplAttribute(MethodImplOptions.InternalCall)]
96 private extern static object GetTarget(int handle);
98 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
101 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102 private extern static void FreeHandle(int handle);
104 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105 private extern static IntPtr GetAddrOfPinnedObject(int handle);