2 // System.Runtime.InteropServices/GCHandle.cs
5 // Ajay kumar Dwivedi (adwiv@yahoo.com) ??
6 // Paolo Molaro (lupus@ximian.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
36 namespace System.Runtime.InteropServices
39 /// Summary description for GCHandle.
41 public struct GCHandle
46 private GCHandle(IntPtr h)
52 private GCHandle(object obj)
53 : this(obj, GCHandleType.Normal)
56 private GCHandle(object value, GCHandleType type)
58 handle = GetTargetHandle (value, 0, type);
63 public bool IsAllocated
75 return GetTarget (handle);
79 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
84 public IntPtr AddrOfPinnedObject()
86 IntPtr res = GetAddrOfPinnedObject(handle);
87 if (res == IntPtr.Zero)
88 throw new InvalidOperationException("The handle is not of Pinned type");
89 if (res == (IntPtr)(-1))
90 throw new ArgumentException ("Object contains non-primitive or non-blittable data.");
94 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
96 return new GCHandle (value);
99 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
101 return new GCHandle (value,type);
110 public static explicit operator IntPtr (GCHandle value)
112 return (IntPtr) value.handle;
115 public static explicit operator GCHandle(IntPtr value)
117 if (value == IntPtr.Zero)
118 throw new ArgumentException ("GCHandle value cannot be zero");
119 if (!CheckCurrentDomain ((int)value))
120 throw new ArgumentException ("GCHandle value belongs to a different domain");
121 return new GCHandle (value);
124 [MethodImplAttribute(MethodImplOptions.InternalCall)]
125 private extern static bool CheckCurrentDomain (int handle);
127 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128 private extern static object GetTarget(int handle);
130 [MethodImplAttribute(MethodImplOptions.InternalCall)]
131 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
133 [MethodImplAttribute(MethodImplOptions.InternalCall)]
134 private extern static void FreeHandle(int handle);
136 [MethodImplAttribute(MethodImplOptions.InternalCall)]
137 private extern static IntPtr GetAddrOfPinnedObject(int handle);
140 public override bool Equals(object o)
142 if (o == null || !(o is GCHandle))
145 return (handle == ((GCHandle)o).handle);
148 public override int GetHashCode()
150 return handle.GetHashCode ();
153 public static GCHandle FromIntPtr (IntPtr value)
155 return (GCHandle)value;
158 public static IntPtr ToIntPtr (GCHandle value)
160 return (IntPtr)value;