1 //------------------------------------------------------------------------------
5 // Copyright (C) 2001 Michael Lambert, All Rights Reserved
7 // Author: Michael Lambert, michaellambert@email.com
8 // Created: Thu 07/31/2001
10 // Remarks: Requires '/unsafe' compiler option. This class uses void*,
11 // ulong, and uint in overloaded constructors, conversion, and
12 // cast members in the public interface. Using pointers is not
13 // valid CLS and the methods in question have been marked with
14 // the CLSCompliant attribute that avoid compiler warnings.
16 //------------------------------------------------------------------------------
18 using System.Runtime.Serialization;
19 using System.Runtime.InteropServices;
26 StructLayout(LayoutKind.Auto),
29 public unsafe struct UIntPtr : ISerializable
31 public static readonly UIntPtr Zero = new UIntPtr(0);
32 private void* _pointer;
37 public UIntPtr(ulong value)
39 _pointer = (void*) value;
45 public UIntPtr(uint value)
47 _pointer = (void*)value;
53 public unsafe UIntPtr(void* value)
58 public override bool Equals(object obj)
62 UIntPtr obj2 = (UIntPtr)obj;
63 return this._pointer == obj2._pointer;
68 public override int GetHashCode()
76 public uint ToUInt32()
78 return (uint) _pointer;
84 public ulong ToUInt64()
86 return (ulong) _pointer;
92 public unsafe void* ToPointer()
96 public override string ToString()
98 return ((uint) _pointer).ToString();
101 // Interface ISerializable
102 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
105 throw new ArgumentNullException( "info" );
106 // if( context == null ) -- context is struct can't be null.
107 // throw new ArgumentNullException( "context" );
109 info.AddValue("pointer", (ulong)_pointer);
112 public static bool operator ==(UIntPtr value1, UIntPtr value2)
114 return value1._pointer == value2._pointer;
116 public static bool operator !=(UIntPtr value1, UIntPtr value2)
118 return value1._pointer != value2._pointer;
124 public static explicit operator ulong(UIntPtr value)
126 return (ulong)value._pointer;
132 public static explicit operator uint(UIntPtr value)
134 return (uint)value._pointer;
140 public static explicit operator UIntPtr(ulong value)
142 return new UIntPtr(value);
148 public unsafe static explicit operator UIntPtr(void* value)
150 return new UIntPtr(value);
156 public unsafe static explicit operator void*(UIntPtr value)
158 return value.ToPointer();
164 public static explicit operator UIntPtr(uint value)
166 return new UIntPtr(value);
169 public static int Size
173 return sizeof(void*);