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),
30 public unsafe struct UIntPtr : ISerializable
32 public static readonly UIntPtr Zero = new UIntPtr(0);
33 private void* _pointer;
38 public UIntPtr(ulong value)
40 _pointer = (void*) value;
46 public UIntPtr(uint value)
48 _pointer = (void*)value;
54 public unsafe UIntPtr(void* value)
59 public override bool Equals(object obj)
63 UIntPtr obj2 = (UIntPtr)obj;
64 return this._pointer == obj2._pointer;
69 public override int GetHashCode()
77 public uint ToUInt32()
79 return (uint) _pointer;
85 public ulong ToUInt64()
87 return (ulong) _pointer;
93 public unsafe void* ToPointer()
97 public override string ToString()
99 return ((uint) _pointer).ToString();
102 // Interface ISerializable
103 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
106 throw new ArgumentNullException( "info" );
107 // if( context == null ) -- context is struct can't be null.
108 // throw new ArgumentNullException( "context" );
110 info.AddValue("pointer", (ulong)_pointer);
113 public static bool operator ==(UIntPtr value1, UIntPtr value2)
115 return value1._pointer == value2._pointer;
117 public static bool operator !=(UIntPtr value1, UIntPtr value2)
119 return value1._pointer != value2._pointer;
125 public static explicit operator ulong(UIntPtr value)
127 return (ulong)value._pointer;
133 public static explicit operator uint(UIntPtr value)
135 return (uint)value._pointer;
141 public static explicit operator UIntPtr(ulong value)
143 return new UIntPtr(value);
149 public unsafe static explicit operator UIntPtr(void* value)
151 return new UIntPtr(value);
157 public unsafe static explicit operator void*(UIntPtr value)
159 return value.ToPointer();
165 public static explicit operator UIntPtr(uint value)
167 return new UIntPtr(value);
170 public static int Size
174 return sizeof(void*);