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;
27 public unsafe struct UIntPtr : ISerializable
29 public static readonly UIntPtr Zero = new UIntPtr(0u);
30 private void* _pointer;
35 public UIntPtr(ulong value)
37 _pointer = (void*) value;
43 public UIntPtr(uint value)
45 _pointer = (void*)value;
51 public unsafe UIntPtr(void* value)
56 public override bool Equals(object obj)
60 UIntPtr obj2 = (UIntPtr)obj;
61 return this._pointer == obj2._pointer;
66 public override int GetHashCode()
74 public uint ToUInt32()
76 return (uint) _pointer;
82 public ulong ToUInt64()
84 return (ulong) _pointer;
90 public unsafe void* ToPointer()
94 public override string ToString()
96 return ((uint) _pointer).ToString();
99 // Interface ISerializable
100 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
103 throw new ArgumentNullException( "info" );
104 // if( context == null ) -- context is struct can't be null.
105 // throw new ArgumentNullException( "context" );
107 info.AddValue("pointer", (ulong)_pointer);
110 public static bool operator ==(UIntPtr value1, UIntPtr value2)
112 return value1._pointer == value2._pointer;
114 public static bool operator !=(UIntPtr value1, UIntPtr value2)
116 return value1._pointer != value2._pointer;
122 public static explicit operator ulong(UIntPtr value)
124 return (ulong)value._pointer;
130 public static explicit operator uint(UIntPtr value)
132 return (uint)value._pointer;
138 public static explicit operator UIntPtr(ulong value)
140 return new UIntPtr(value);
146 public unsafe static explicit operator UIntPtr(void* value)
148 return new UIntPtr(value);
154 public unsafe static explicit operator void*(UIntPtr value)
156 return value.ToPointer();
162 public static explicit operator UIntPtr(uint value)
164 return new UIntPtr(value);
167 public static int Size
171 return sizeof(void*);