mirror of https://github.com/dahall/Vanara.git
Added IsNullOrEmpty property to StrPtrXX structures.
parent
a52ea804ca
commit
231ea34f17
|
@ -5,6 +5,145 @@ using Vanara.Extensions;
|
||||||
|
|
||||||
namespace Vanara.InteropServices
|
namespace Vanara.InteropServices
|
||||||
{
|
{
|
||||||
|
/// <summary>The GuidPtr structure represents a LPGUID.</summary>
|
||||||
|
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
||||||
|
public struct GuidPtr
|
||||||
|
{
|
||||||
|
private IntPtr ptr;
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the <see cref="GuidPtr"/> struct by allocating memory with <see cref="Marshal.AllocCoTaskMem(int)"/>.</summary>
|
||||||
|
/// <param name="g">The guid value.</param>
|
||||||
|
public GuidPtr(Guid g) => ptr = g.MarshalToPtr(Marshal.AllocCoTaskMem, out _);
|
||||||
|
|
||||||
|
/// <summary>Gets a value indicating whether this instance is equivalent to null pointer or void*.</summary>
|
||||||
|
/// <value><c>true</c> if this instance is null; otherwise, <c>false</c>.</value>
|
||||||
|
public bool IsNull => ptr == IntPtr.Zero;
|
||||||
|
|
||||||
|
/// <summary>Gets the value of the Guid.</summary>
|
||||||
|
/// <value>The value pointed to by this pointer.</value>
|
||||||
|
public Guid? Value => IsNull ? null : ptr.ToStructure<Guid>();
|
||||||
|
|
||||||
|
/// <summary>Assigns a new Guid value to the pointer.</summary>
|
||||||
|
/// <param name="g">The guid value.</param>
|
||||||
|
public void Assign(Guid g)
|
||||||
|
{
|
||||||
|
if (IsNull)
|
||||||
|
ptr = g.MarshalToPtr(Marshal.AllocCoTaskMem, out _);
|
||||||
|
else
|
||||||
|
Marshal.StructureToPtr(g, ptr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Frees the unmanaged memory.</summary>
|
||||||
|
public void Free() { Marshal.FreeCoTaskMem(ptr); ptr = IntPtr.Zero; }
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="GuidPtr"/> to <see cref="System.Nullable{Guid}"/>.</summary>
|
||||||
|
/// <param name="g">The pointer to a Guid.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator Guid?(GuidPtr g) => g.Value;
|
||||||
|
|
||||||
|
/// <summary>Performs an explicit conversion from <see cref="GuidPtr"/> to <see cref="IntPtr"/>.</summary>
|
||||||
|
/// <param name="g">The pointer to a Guid.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static explicit operator IntPtr(GuidPtr g) => g.ptr;
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="GuidPtr"/>.</summary>
|
||||||
|
/// <param name="p">The pointer.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator GuidPtr(IntPtr p) => new() { ptr = p };
|
||||||
|
|
||||||
|
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
|
||||||
|
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
|
||||||
|
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||||
|
public override bool Equals(object obj) => obj switch
|
||||||
|
{
|
||||||
|
Guid g => !IsNull && g.Equals(Value.Value),
|
||||||
|
IntPtr p => ptr.Equals(p),
|
||||||
|
_ => base.Equals(obj),
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>Returns a hash code for this instance.</summary>
|
||||||
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||||
|
public override int GetHashCode() => ptr.GetHashCode();
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override string ToString() => Value?.ToString("B") ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>The StrPtr structure represents a LPWSTR.</summary>
|
||||||
|
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
||||||
|
public struct StrPtrAnsi
|
||||||
|
{
|
||||||
|
private IntPtr ptr;
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the <see cref="StrPtrAnsi"/> struct.</summary>
|
||||||
|
/// <param name="s">The string value.</param>
|
||||||
|
public StrPtrAnsi(string s) => ptr = StringHelper.AllocString(s, CharSet.Ansi);
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the <see cref="StrPtrAnsi"/> struct.</summary>
|
||||||
|
/// <param name="charLen">Number of characters to reserve in memory.</param>
|
||||||
|
public StrPtrAnsi(uint charLen) => ptr = StringHelper.AllocChars(charLen, CharSet.Ansi);
|
||||||
|
|
||||||
|
/// <summary>Gets a value indicating whether this instance is equivalent to null pointer or void*.</summary>
|
||||||
|
/// <value><c>true</c> if this instance is null; otherwise, <c>false</c>.</value>
|
||||||
|
public bool IsNull => ptr == IntPtr.Zero;
|
||||||
|
|
||||||
|
/// <summary>Indicates whether the specified string is <see langword="null"/> or an empty string ("").</summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if the value parameter is <see langword="null"/> or an empty string (""); otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public bool IsNullOrEmpty => ptr == IntPtr.Zero || Marshal.ReadByte(ptr) == 0;
|
||||||
|
|
||||||
|
/// <summary>Assigns a new string value to the pointer.</summary>
|
||||||
|
/// <param name="s">The string value.</param>
|
||||||
|
public void Assign(string s) => StringHelper.RefreshString(ref ptr, out var _, s, CharSet.Ansi);
|
||||||
|
|
||||||
|
/// <summary>Assigns a new string value to the pointer.</summary>
|
||||||
|
/// <param name="s">The string value.</param>
|
||||||
|
/// <param name="charsAllocated">The character count allocated.</param>
|
||||||
|
/// <returns><c>true</c> if new memory was allocated for the string; <c>false</c> if otherwise.</returns>
|
||||||
|
public bool Assign(string s, out uint charsAllocated) => StringHelper.RefreshString(ref ptr, out charsAllocated, s, CharSet.Ansi);
|
||||||
|
|
||||||
|
/// <summary>Assigns an integer to the pointer for uses such as LPSTR_TEXTCALLBACK.</summary>
|
||||||
|
/// <param name="value">The value to assign.</param>
|
||||||
|
public void AssignConstant(int value) { Free(); ptr = (IntPtr)value; }
|
||||||
|
|
||||||
|
/// <summary>Frees the unmanaged string memory.</summary>
|
||||||
|
public void Free() { StringHelper.FreeString(ptr, CharSet.Ansi); ptr = IntPtr.Zero; }
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="StrPtrAnsi"/> to <see cref="string"/>.</summary>
|
||||||
|
/// <param name="p">The <see cref="StrPtrAnsi"/> instance.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator string(StrPtrAnsi p) => p.IsNull ? null : p.ToString();
|
||||||
|
|
||||||
|
/// <summary>Performs an explicit conversion from <see cref="StrPtrAnsi"/> to <see cref="System.IntPtr"/>.</summary>
|
||||||
|
/// <param name="p">The <see cref="StrPtrAnsi"/> instance.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static explicit operator IntPtr(StrPtrAnsi p) => p.ptr;
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrAnsi"/>.</summary>
|
||||||
|
/// <param name="p">The pointer.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator StrPtrAnsi(IntPtr p) => new() { ptr = p };
|
||||||
|
|
||||||
|
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
|
||||||
|
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
|
||||||
|
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||||
|
public override bool Equals(object obj) => obj switch
|
||||||
|
{
|
||||||
|
string s => s.Equals(StringHelper.GetString(ptr, CharSet.Ansi)),
|
||||||
|
IntPtr p => ptr.Equals(p),
|
||||||
|
_ => base.Equals(obj),
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>Returns a hash code for this instance.</summary>
|
||||||
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||||
|
public override int GetHashCode() => ptr.GetHashCode();
|
||||||
|
|
||||||
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
||||||
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
||||||
|
public override string ToString() => StringHelper.GetString(ptr, CharSet.Ansi) ?? "null";
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>The StrPtr structure represents a LPTSTR.</summary>
|
/// <summary>The StrPtr structure represents a LPTSTR.</summary>
|
||||||
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
||||||
public struct StrPtrAuto
|
public struct StrPtrAuto
|
||||||
|
@ -44,7 +183,13 @@ namespace Vanara.InteropServices
|
||||||
/// <summary>Frees the unmanaged string memory.</summary>
|
/// <summary>Frees the unmanaged string memory.</summary>
|
||||||
public void Free() { StringHelper.FreeString(ptr); ptr = IntPtr.Zero; }
|
public void Free() { StringHelper.FreeString(ptr); ptr = IntPtr.Zero; }
|
||||||
|
|
||||||
/// <summary>Performs an implicit conversion from <see cref="StrPtrAuto"/> to <see cref="System.String"/>.</summary>
|
/// <summary>Indicates whether the specified string is <see langword="null"/> or an empty string ("").</summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if the value parameter is <see langword="null"/> or an empty string (""); otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public bool IsNullOrEmpty => ptr == IntPtr.Zero || StringHelper.GetString(ptr, CharSet.Auto, 1) == string.Empty;
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="StrPtrAuto"/> to <see cref="string"/>.</summary>
|
||||||
/// <param name="p">The <see cref="StrPtrAuto"/> instance.</param>
|
/// <param name="p">The <see cref="StrPtrAuto"/> instance.</param>
|
||||||
/// <returns>The result of the conversion.</returns>
|
/// <returns>The result of the conversion.</returns>
|
||||||
public static implicit operator string(StrPtrAuto p) => p.IsNull ? null : p.ToString();
|
public static implicit operator string(StrPtrAuto p) => p.IsNull ? null : p.ToString();
|
||||||
|
@ -57,30 +202,24 @@ namespace Vanara.InteropServices
|
||||||
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrAuto"/>.</summary>
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrAuto"/>.</summary>
|
||||||
/// <param name="p">The pointer.</param>
|
/// <param name="p">The pointer.</param>
|
||||||
/// <returns>The result of the conversion.</returns>
|
/// <returns>The result of the conversion.</returns>
|
||||||
public static implicit operator StrPtrAuto(IntPtr p) => new StrPtrAuto { ptr = p };
|
public static implicit operator StrPtrAuto(IntPtr p) => new() { ptr = p };
|
||||||
|
|
||||||
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
|
||||||
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
|
||||||
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||||
public override bool Equals(object obj)
|
public override bool Equals(object obj) => obj switch
|
||||||
{
|
{
|
||||||
switch (obj)
|
string s => s.Equals(StringHelper.GetString(ptr, CharSet.Auto)),
|
||||||
{
|
IntPtr p => ptr.Equals(p),
|
||||||
case string s:
|
_ => base.Equals(obj),
|
||||||
return s.Equals(StringHelper.GetString(ptr, CharSet.Auto));
|
};
|
||||||
case IntPtr p:
|
|
||||||
return ptr.Equals(p);
|
|
||||||
default:
|
|
||||||
return base.Equals(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns a hash code for this instance.</summary>
|
/// <summary>Returns a hash code for this instance.</summary>
|
||||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||||
public override int GetHashCode() => ptr.GetHashCode();
|
public override int GetHashCode() => ptr.GetHashCode();
|
||||||
|
|
||||||
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
||||||
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
||||||
public override string ToString() => StringHelper.GetString(ptr) ?? "null";
|
public override string ToString() => StringHelper.GetString(ptr) ?? "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +258,13 @@ namespace Vanara.InteropServices
|
||||||
/// <summary>Frees the unmanaged string memory.</summary>
|
/// <summary>Frees the unmanaged string memory.</summary>
|
||||||
public void Free() { StringHelper.FreeString(ptr, CharSet.Unicode); ptr = IntPtr.Zero; }
|
public void Free() { StringHelper.FreeString(ptr, CharSet.Unicode); ptr = IntPtr.Zero; }
|
||||||
|
|
||||||
/// <summary>Performs an implicit conversion from <see cref="StrPtrUni"/> to <see cref="System.String"/>.</summary>
|
/// <summary>Indicates whether the specified string is <see langword="null"/> or an empty string ("").</summary>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true"/> if the value parameter is <see langword="null"/> or an empty string (""); otherwise, <see langword="false"/>.
|
||||||
|
/// </returns>
|
||||||
|
public bool IsNullOrEmpty => ptr == IntPtr.Zero || Marshal.ReadInt16(ptr) == 0;
|
||||||
|
|
||||||
|
/// <summary>Performs an implicit conversion from <see cref="StrPtrUni"/> to <see cref="string"/>.</summary>
|
||||||
/// <param name="p">The <see cref="StrPtrUni"/> instance.</param>
|
/// <param name="p">The <see cref="StrPtrUni"/> instance.</param>
|
||||||
/// <returns>The result of the conversion.</returns>
|
/// <returns>The result of the conversion.</returns>
|
||||||
public static implicit operator string(StrPtrUni p) => p.IsNull ? null : p.ToString();
|
public static implicit operator string(StrPtrUni p) => p.IsNull ? null : p.ToString();
|
||||||
|
@ -132,192 +277,36 @@ namespace Vanara.InteropServices
|
||||||
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrUni"/>.</summary>
|
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrUni"/>.</summary>
|
||||||
/// <param name="p">The pointer.</param>
|
/// <param name="p">The pointer.</param>
|
||||||
/// <returns>The result of the conversion.</returns>
|
/// <returns>The result of the conversion.</returns>
|
||||||
public static implicit operator StrPtrUni(IntPtr p) => new StrPtrUni { ptr = p };
|
public static implicit operator StrPtrUni(IntPtr p) => new() { ptr = p };
|
||||||
|
|
||||||
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
/// <summary>Determines whether the specified <see cref="object"/>, is equal to this instance.</summary>
|
||||||
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
/// <param name="obj">The <see cref="object"/> to compare with this instance.</param>
|
||||||
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
/// <returns><c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||||
public override bool Equals(object obj)
|
public override bool Equals(object obj) => obj switch
|
||||||
{
|
{
|
||||||
switch (obj)
|
string s => s.Equals(StringHelper.GetString(ptr, CharSet.Unicode)),
|
||||||
{
|
IntPtr p => ptr.Equals(p),
|
||||||
case string s:
|
_ => base.Equals(obj),
|
||||||
return s.Equals(StringHelper.GetString(ptr, CharSet.Unicode));
|
};
|
||||||
case IntPtr p:
|
|
||||||
return ptr.Equals(p);
|
|
||||||
default:
|
|
||||||
return base.Equals(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns a hash code for this instance.</summary>
|
/// <summary>Returns a hash code for this instance.</summary>
|
||||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||||
public override int GetHashCode() => ptr.GetHashCode();
|
public override int GetHashCode() => ptr.GetHashCode();
|
||||||
|
|
||||||
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
|
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
|
||||||
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
|
/// <returns>A <see cref="string"/> that represents this instance.</returns>
|
||||||
public override string ToString() => StringHelper.GetString(ptr, CharSet.Unicode) ?? "null";
|
public override string ToString() => StringHelper.GetString(ptr, CharSet.Unicode) ?? "null";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The StrPtr structure represents a LPWSTR.</summary>
|
|
||||||
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
|
||||||
public struct StrPtrAnsi
|
|
||||||
{
|
|
||||||
private IntPtr ptr;
|
|
||||||
|
|
||||||
/// <summary>Initializes a new instance of the <see cref="StrPtrAnsi"/> struct.</summary>
|
|
||||||
/// <param name="s">The string value.</param>
|
|
||||||
public StrPtrAnsi(string s) => ptr = StringHelper.AllocString(s, CharSet.Ansi);
|
|
||||||
|
|
||||||
/// <summary>Initializes a new instance of the <see cref="StrPtrAnsi"/> struct.</summary>
|
|
||||||
/// <param name="charLen">Number of characters to reserve in memory.</param>
|
|
||||||
public StrPtrAnsi(uint charLen) => ptr = StringHelper.AllocChars(charLen, CharSet.Ansi);
|
|
||||||
|
|
||||||
/// <summary>Gets a value indicating whether this instance is equivalent to null pointer or void*.</summary>
|
|
||||||
/// <value><c>true</c> if this instance is null; otherwise, <c>false</c>.</value>
|
|
||||||
public bool IsNull => ptr == IntPtr.Zero;
|
|
||||||
|
|
||||||
/// <summary>Assigns a new string value to the pointer.</summary>
|
|
||||||
/// <param name="s">The string value.</param>
|
|
||||||
public void Assign(string s) => StringHelper.RefreshString(ref ptr, out var _, s, CharSet.Ansi);
|
|
||||||
|
|
||||||
/// <summary>Assigns a new string value to the pointer.</summary>
|
|
||||||
/// <param name="s">The string value.</param>
|
|
||||||
/// <param name="charsAllocated">The character count allocated.</param>
|
|
||||||
/// <returns><c>true</c> if new memory was allocated for the string; <c>false</c> if otherwise.</returns>
|
|
||||||
public bool Assign(string s, out uint charsAllocated) => StringHelper.RefreshString(ref ptr, out charsAllocated, s, CharSet.Ansi);
|
|
||||||
|
|
||||||
/// <summary>Assigns an integer to the pointer for uses such as LPSTR_TEXTCALLBACK.</summary>
|
|
||||||
/// <param name="value">The value to assign.</param>
|
|
||||||
public void AssignConstant(int value) { Free(); ptr = (IntPtr)value; }
|
|
||||||
|
|
||||||
/// <summary>Frees the unmanaged string memory.</summary>
|
|
||||||
public void Free() { StringHelper.FreeString(ptr, CharSet.Ansi); ptr = IntPtr.Zero; }
|
|
||||||
|
|
||||||
/// <summary>Performs an implicit conversion from <see cref="StrPtrAnsi"/> to <see cref="System.String"/>.</summary>
|
|
||||||
/// <param name="p">The <see cref="StrPtrAnsi"/> instance.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator string(StrPtrAnsi p) => p.IsNull ? null : p.ToString();
|
|
||||||
|
|
||||||
/// <summary>Performs an explicit conversion from <see cref="StrPtrAnsi"/> to <see cref="System.IntPtr"/>.</summary>
|
|
||||||
/// <param name="p">The <see cref="StrPtrAnsi"/> instance.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static explicit operator IntPtr(StrPtrAnsi p) => p.ptr;
|
|
||||||
|
|
||||||
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="StrPtrAnsi"/>.</summary>
|
|
||||||
/// <param name="p">The pointer.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator StrPtrAnsi(IntPtr p) => new StrPtrAnsi { ptr = p };
|
|
||||||
|
|
||||||
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
|
||||||
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
|
||||||
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns a hash code for this instance.</summary>
|
|
||||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
||||||
public override int GetHashCode() => ptr.GetHashCode();
|
|
||||||
|
|
||||||
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
|
|
||||||
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
|
|
||||||
public override string ToString() => StringHelper.GetString(ptr, CharSet.Ansi) ?? "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>The GuidPtr structure represents a LPGUID.</summary>
|
|
||||||
[StructLayout(LayoutKind.Sequential), DebuggerDisplay("{ptr}, {ToString()}")]
|
|
||||||
public struct GuidPtr
|
|
||||||
{
|
|
||||||
private IntPtr ptr;
|
|
||||||
|
|
||||||
/// <summary>Initializes a new instance of the <see cref="GuidPtr"/> struct by allocating memory with <see cref="Marshal.AllocCoTaskMem(int)"/>.</summary>
|
|
||||||
/// <param name="g">The guid value.</param>
|
|
||||||
public GuidPtr(Guid g) => ptr = g.MarshalToPtr(Marshal.AllocCoTaskMem, out _);
|
|
||||||
|
|
||||||
/// <summary>Gets a value indicating whether this instance is equivalent to null pointer or void*.</summary>
|
|
||||||
/// <value><c>true</c> if this instance is null; otherwise, <c>false</c>.</value>
|
|
||||||
public bool IsNull => ptr == IntPtr.Zero;
|
|
||||||
|
|
||||||
/// <summary>Gets the value of the Guid.</summary>
|
|
||||||
/// <value>The value pointed to by this pointer.</value>
|
|
||||||
public Guid? Value => IsNull ? (Guid?)null : ptr.ToStructure<Guid>();
|
|
||||||
|
|
||||||
/// <summary>Assigns a new Guid value to the pointer.</summary>
|
|
||||||
/// <param name="g">The guid value.</param>
|
|
||||||
public void Assign(Guid g)
|
|
||||||
{
|
|
||||||
if (IsNull)
|
|
||||||
ptr = g.MarshalToPtr(Marshal.AllocCoTaskMem, out _);
|
|
||||||
else
|
|
||||||
Marshal.StructureToPtr(g, ptr, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Frees the unmanaged memory.</summary>
|
|
||||||
public void Free() { Marshal.FreeCoTaskMem(ptr); ptr = IntPtr.Zero; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Performs an implicit conversion from <see cref="GuidPtr"/> to <see cref="System.Nullable{Guid}"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="g">The pointer to a Guid.</param>
|
|
||||||
/// <returns>
|
|
||||||
/// The result of the conversion.
|
|
||||||
/// </returns>
|
|
||||||
public static implicit operator Guid?(GuidPtr g) => g.Value;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Performs an explicit conversion from <see cref="GuidPtr"/> to <see cref="IntPtr"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="g">The pointer to a Guid.</param>
|
|
||||||
/// <returns>
|
|
||||||
/// The result of the conversion.
|
|
||||||
/// </returns>
|
|
||||||
public static explicit operator IntPtr(GuidPtr g) => g.ptr;
|
|
||||||
|
|
||||||
/// <summary>Performs an implicit conversion from <see cref="IntPtr"/> to <see cref="GuidPtr"/>.</summary>
|
|
||||||
/// <param name="p">The pointer.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator GuidPtr(IntPtr p) => new GuidPtr { ptr = p };
|
|
||||||
|
|
||||||
/// <summary>Determines whether the specified <see cref="System.Object"/>, is equal to this instance.</summary>
|
|
||||||
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
|
|
||||||
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.</returns>
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Returns a hash code for this instance.</summary>
|
|
||||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
||||||
public override int GetHashCode() => ptr.GetHashCode();
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override string ToString() => Value?.ToString("B") ?? "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <para>Represents a GUID point, or REFGUID, that will automatically dispose the memory to which it points at the end of scope.</para>
|
/// <para>Represents a GUID point, or REFGUID, that will automatically dispose the memory to which it points at the end of scope.</para>
|
||||||
/// <note>You must use the <see cref="Null"/> value, or the parameter-less constructor to pass the equivalent of <see langword="null"/>.</note>
|
/// <note>You must use the <see cref="Null"/> value, or the parameter-less constructor to pass the equivalent of <see langword="null"/>.</note>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SafeGuidPtr : SafeMemStruct<Guid, CoTaskMemoryMethods>
|
public class SafeGuidPtr : SafeMemStruct<Guid, CoTaskMemoryMethods>
|
||||||
{
|
{
|
||||||
|
/// <summary>The equivalent of a <see langword="null"/> pointer to a GUID value.</summary>
|
||||||
|
public static readonly SafeGuidPtr Null = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="SafeGuidPtr"/> class. This value is equivalent to a <see langword="null"/> pointer.
|
/// Initializes a new instance of the <see cref="SafeGuidPtr"/> class. This value is equivalent to a <see langword="null"/> pointer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -331,8 +320,5 @@ namespace Vanara.InteropServices
|
||||||
/// <param name="guid">The unique identifier.</param>
|
/// <param name="guid">The unique identifier.</param>
|
||||||
/// <returns>The resulting <see cref="SafeGuidPtr"/> instance from the conversion.</returns>
|
/// <returns>The resulting <see cref="SafeGuidPtr"/> instance from the conversion.</returns>
|
||||||
public static implicit operator SafeGuidPtr(Guid? guid) => guid.HasValue ? new SafeGuidPtr(guid.Value) : Null;
|
public static implicit operator SafeGuidPtr(Guid? guid) => guid.HasValue ? new SafeGuidPtr(guid.Value) : Null;
|
||||||
|
|
||||||
/// <summary>The equivalent of a <see langword="null"/> pointer to a GUID value.</summary>
|
|
||||||
public static readonly SafeGuidPtr Null = new SafeGuidPtr();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue