diff --git a/Core/InteropServices/StrPtr.cs b/Core/InteropServices/StrPtr.cs index 6996d839..ba54a0c3 100644 --- a/Core/InteropServices/StrPtr.cs +++ b/Core/InteropServices/StrPtr.cs @@ -58,6 +58,26 @@ namespace Vanara.InteropServices /// The result of the conversion. public static implicit operator StrPtrAuto(IntPtr p) => new StrPtrAuto { ptr = p }; + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + switch (obj) + { + case string s: + return s.Equals(StringHelper.GetString(ptr, CharSet.Auto)); + case IntPtr p: + return ptr.Equals(p); + default: + return base.Equals(obj); + } + } + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => ptr.GetHashCode(); + /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => StringHelper.GetString(ptr) ?? "null"; @@ -113,6 +133,26 @@ namespace Vanara.InteropServices /// The result of the conversion. public static implicit operator StrPtrUni(IntPtr p) => new StrPtrUni { ptr = p }; + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + switch (obj) + { + case string s: + return s.Equals(StringHelper.GetString(ptr, CharSet.Unicode)); + case IntPtr p: + return ptr.Equals(p); + default: + return base.Equals(obj); + } + } + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => ptr.GetHashCode(); + /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => StringHelper.GetString(ptr, CharSet.Unicode) ?? "null"; @@ -168,8 +208,106 @@ namespace Vanara.InteropServices /// The result of the conversion. public static implicit operator StrPtrAnsi(IntPtr p) => new StrPtrAnsi { ptr = p }; + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + switch (obj) + { + case string s: + return s.Equals(StringHelper.GetString(ptr, CharSet.Ansi)); + case IntPtr p: + return ptr.Equals(p); + default: + return base.Equals(obj); + } + } + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => ptr.GetHashCode(); + /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() => StringHelper.GetString(ptr, CharSet.Ansi) ?? "null"; } + + /// The GuidPtr structure represents a LPGUID. + [StructLayout(LayoutKind.Sequential)] + public struct GuidPtr + { + private IntPtr ptr; + + /// Initializes a new instance of the struct by allocating memory with . + /// The guid value. + public GuidPtr(Guid g) => ptr = g.StructureToPtr(Marshal.AllocCoTaskMem, out _); + + /// Gets a value indicating whether this instance is equivalent to null pointer or void*. + /// true if this instance is null; otherwise, false. + public bool IsNull => ptr == IntPtr.Zero; + + /// Gets the value of the Guid. + /// The value pointed to by this pointer. + public Guid? Value => IsNull ? (Guid?)null : ptr.ToStructure(); + + /// Assigns a new Guid value to the pointer. + /// The guid value. + public void Assign(Guid g) + { + if (IsNull) + ptr = g.StructureToPtr(Marshal.AllocCoTaskMem, out _); + else + Marshal.StructureToPtr(g, ptr, true); + } + + /// Frees the unmanaged memory. + public void Free() { Marshal.FreeCoTaskMem(ptr); ptr = IntPtr.Zero; } + + /// + /// Performs an implicit conversion from to . + /// + /// The pointer to a Guid. + /// + /// The result of the conversion. + /// + public static implicit operator Guid?(GuidPtr g) => g.Value; + + /// + /// Performs an explicit conversion from to . + /// + /// The pointer to a Guid. + /// + /// The result of the conversion. + /// + public static explicit operator IntPtr(GuidPtr g) => g.ptr; + + /// Performs an implicit conversion from to . + /// The pointer. + /// The result of the conversion. + public static implicit operator GuidPtr(IntPtr p) => new GuidPtr { ptr = p }; + + /// Determines whether the specified , is equal to this instance. + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object obj) + { + switch (obj) + { + case Guid g: + return IsNull ? false : g.Equals(Value.Value); + case IntPtr p: + return ptr.Equals(p); + default: + return base.Equals(obj); + } + } + + /// Returns a hash code for this instance. + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + public override int GetHashCode() => ptr.GetHashCode(); + + /// + public override string ToString() => Value?.ToString("B") ?? ""; + } } \ No newline at end of file