mscorlib(4.0.0.0) API with additions
IntPtr.cs
6 using System.Security;
7 
8 namespace System
9 {
11  [Serializable]
12  [ComVisible(true)]
13  [__DynamicallyInvokable]
14  public struct IntPtr : ISerializable
15  {
16  [SecurityCritical]
17  private unsafe void* m_value;
18 
20  public static readonly IntPtr Zero;
21 
25  [__DynamicallyInvokable]
26  public static int Size
27  {
28  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
29  [NonVersionable]
30  [__DynamicallyInvokable]
31  get
32  {
33  return 8;
34  }
35  }
36 
37  [SecuritySafeCritical]
38  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
39  internal unsafe bool IsNull()
40  {
41  return m_value == null;
42  }
43 
46  [SecuritySafeCritical]
47  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
48  [NonVersionable]
49  [__DynamicallyInvokable]
50  public unsafe IntPtr(int value)
51  {
52  m_value = (void*)value;
53  }
54 
58  [SecuritySafeCritical]
59  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
60  [NonVersionable]
61  [__DynamicallyInvokable]
62  public unsafe IntPtr(long value)
63  {
64  m_value = (void*)value;
65  }
66 
69  [SecurityCritical]
70  [CLSCompliant(false)]
71  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
72  [NonVersionable]
73  public unsafe IntPtr(void* value)
74  {
75  m_value = value;
76  }
77 
78  [SecurityCritical]
79  private unsafe IntPtr(SerializationInfo info, StreamingContext context)
80  {
81  long @int = info.GetInt64("value");
82  if (Size == 4 && (@int > int.MaxValue || @int < int.MinValue))
83  {
84  throw new ArgumentException(Environment.GetResourceString("Serialization_InvalidPtrValue"));
85  }
86  m_value = (void*)@int;
87  }
88 
94  [SecurityCritical]
96  {
97  if (info == null)
98  {
99  throw new ArgumentNullException("info");
100  }
101  info.AddValue("value", (long)m_value);
102  }
103 
108  [SecuritySafeCritical]
109  [__DynamicallyInvokable]
110  public unsafe override bool Equals(object obj)
111  {
112  if (obj is IntPtr)
113  {
114  return m_value == ((IntPtr)obj).m_value;
115  }
116  return false;
117  }
118 
121  [SecuritySafeCritical]
122  [__DynamicallyInvokable]
123  public unsafe override int GetHashCode()
124  {
125  return (int)m_value;
126  }
127 
131  [SecuritySafeCritical]
132  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
133  [NonVersionable]
134  [__DynamicallyInvokable]
135  public unsafe int ToInt32()
136  {
137  long num = (long)m_value;
138  return checked((int)num);
139  }
140 
143  [SecuritySafeCritical]
144  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
145  [NonVersionable]
146  [__DynamicallyInvokable]
147  public unsafe long ToInt64()
148  {
149  return (long)m_value;
150  }
151 
154  [SecuritySafeCritical]
155  [__DynamicallyInvokable]
156  public unsafe override string ToString()
157  {
158  return ((long*)(&m_value))->ToString(CultureInfo.InvariantCulture);
159  }
160 
164  [SecuritySafeCritical]
165  [__DynamicallyInvokable]
166  public unsafe string ToString(string format)
167  {
168  return ((long*)(&m_value))->ToString(format, CultureInfo.InvariantCulture);
169  }
170 
174  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
175  [NonVersionable]
176  public static explicit operator IntPtr(int value)
177  {
178  return new IntPtr(value);
179  }
180 
185  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
186  [NonVersionable]
187  public static explicit operator IntPtr(long value)
188  {
189  return new IntPtr(value);
190  }
191 
195  [SecurityCritical]
196  [CLSCompliant(false)]
197  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
198  [NonVersionable]
199  public unsafe static explicit operator IntPtr(void* value)
200  {
201  return new IntPtr(value);
202  }
203 
207  [SecuritySafeCritical]
208  [CLSCompliant(false)]
209  [NonVersionable]
210  public unsafe static explicit operator void*(IntPtr value)
211  {
212  return value.m_value;
213  }
214 
219  [SecuritySafeCritical]
220  [NonVersionable]
221  public unsafe static explicit operator int(IntPtr value)
222  {
223  long num = (long)value.m_value;
224  return checked((int)num);
225  }
226 
230  [SecuritySafeCritical]
231  [NonVersionable]
232  public unsafe static explicit operator long(IntPtr value)
233  {
234  return (long)value.m_value;
235  }
236 
242  [SecuritySafeCritical]
243  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
244  [NonVersionable]
245  [__DynamicallyInvokable]
246  public unsafe static bool operator ==(IntPtr value1, IntPtr value2)
247  {
248  return value1.m_value == value2.m_value;
249  }
250 
256  [SecuritySafeCritical]
257  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
258  [NonVersionable]
259  [__DynamicallyInvokable]
260  public unsafe static bool operator !=(IntPtr value1, IntPtr value2)
261  {
262  return value1.m_value != value2.m_value;
263  }
264 
269  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
270  [NonVersionable]
271  public static IntPtr Add(IntPtr pointer, int offset)
272  {
273  return pointer + offset;
274  }
275 
280  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
281  [NonVersionable]
282  public static IntPtr operator +(IntPtr pointer, int offset)
283  {
284  return new IntPtr(pointer.ToInt64() + offset);
285  }
286 
291  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
292  [NonVersionable]
293  public static IntPtr Subtract(IntPtr pointer, int offset)
294  {
295  return pointer - offset;
296  }
297 
302  [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
303  [NonVersionable]
304  public static IntPtr operator -(IntPtr pointer, int offset)
305  {
306  return new IntPtr(pointer.ToInt64() - offset);
307  }
308 
311  [SecuritySafeCritical]
312  [CLSCompliant(false)]
313  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
314  [NonVersionable]
315  public unsafe void* ToPointer()
316  {
317  return m_value;
318  }
319  }
320 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
unsafe IntPtr(long value)
Initializes a new instance of T:System.IntPtr using the specified 64-bit pointer.
Definition: IntPtr.cs:62
unsafe string ToString(string format)
Converts the numeric value of the current T:System.IntPtr object to its equivalent string representat...
Definition: IntPtr.cs:166
unsafe IntPtr(int value)
Initializes a new instance of T:System.IntPtr using the specified 32-bit pointer or handle.
Definition: IntPtr.cs:50
unsafe IntPtr(void *value)
Initializes a new instance of T:System.IntPtr using the specified pointer to an unspecified type.
Definition: IntPtr.cs:73
Definition: __Canon.cs:3
unsafe long ToInt64()
Converts the value of this instance to a 64-bit signed integer.
Definition: IntPtr.cs:147
static IntPtr operator+(IntPtr pointer, int offset)
Adds an offset to the value of a pointer.
Definition: IntPtr.cs:282
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
unsafe void * ToPointer()
Converts the value of this instance to a pointer to an unspecified type.
Definition: IntPtr.cs:315
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static unsafe bool operator==(IntPtr value1, IntPtr value2)
Determines whether two specified instances of T:System.IntPtr are equal.
Definition: IntPtr.cs:246
static IntPtr Subtract(IntPtr pointer, int offset)
Subtracts an offset from the value of a pointer.
Definition: IntPtr.cs:293
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
unsafe override string ToString()
Converts the numeric value of the current T:System.IntPtr object to its equivalent string representat...
Definition: IntPtr.cs:156
unsafe int ToInt32()
Converts the value of this instance to a 32-bit signed integer.
Definition: IntPtr.cs:135
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
static int Size
Gets the size of this instance.
Definition: IntPtr.cs:27
static IntPtr Add(IntPtr pointer, int offset)
Adds an offset to the value of a pointer.
Definition: IntPtr.cs:271
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Specifies that the class can be serialized.
unsafe override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
Definition: IntPtr.cs:110
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
static IntPtr operator -(IntPtr pointer, int offset)
Subtracts an offset from the value of a pointer.
Definition: IntPtr.cs:304
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
static unsafe bool operator !=(IntPtr value1, IntPtr value2)
Determines whether two specified instances of T:System.IntPtr are not equal.
Definition: IntPtr.cs:260
unsafe override int GetHashCode()
Returns the hash code for this instance.
Definition: IntPtr.cs:123