diff --git a/Core/InteropServices/SafeCoTaskMemString.cs b/Core/InteropServices/SafeCoTaskMemString.cs
index 60ff97fb..8c0c0504 100644
--- a/Core/InteropServices/SafeCoTaskMemString.cs
+++ b/Core/InteropServices/SafeCoTaskMemString.cs
@@ -31,9 +31,9 @@ namespace Vanara.InteropServices
}
/// Initializes a new instance of the class.
- /// The size of the buffer in characters, including the null character terminator.
+ /// The size of the buffer in characters, including the null character terminator.
/// The character set.
- public SafeCoTaskMemString(int charLen, CharSet charSet = CharSet.Unicode) : base(charLen, charSet)
+ public SafeCoTaskMemString(int capacity, CharSet charSet = CharSet.Unicode) : base(capacity, charSet)
{
}
@@ -41,12 +41,14 @@ namespace Vanara.InteropServices
/// The PTR.
/// The character set.
/// true to reliably release the handle during finalization; false to prevent it.
+ /// The number of bytes allocated to .
[ExcludeFromCodeCoverage]
- private SafeCoTaskMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true) : base(ptr, charSet, ownsHandle)
+ private SafeCoTaskMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) :
+ base(ptr, charSet, ownsHandle, allocatedBytes)
{
}
- /// Represents a null value. Used primarily for comparrison.
+ /// Represents a null value. Used primarily for comparison.
/// A null value.
public static SafeCoTaskMemString Null => new SafeCoTaskMemString(IntPtr.Zero, CharSet.Unicode, false);
}
diff --git a/Core/InteropServices/SafeMemString.cs b/Core/InteropServices/SafeMemString.cs
index 23b2ffaa..f0de3831 100644
--- a/Core/InteropServices/SafeMemString.cs
+++ b/Core/InteropServices/SafeMemString.cs
@@ -11,8 +11,6 @@ namespace Vanara.InteropServices
///
public abstract class SafeMemString : SafeMemoryHandle where TMem : IMemoryMethods, new()
{
- private readonly CharSet chSet = CharSet.Unicode;
-
/// Initializes a new instance of the class.
/// The string value.
/// The character set.
@@ -43,26 +41,37 @@ namespace Vanara.InteropServices
/// The character set.
protected SafeMemString(int charLen, CharSet charSet = CharSet.Unicode) : base(charLen * StringHelper.GetCharSize(charSet))
{
- chSet = charSet;
+ CharSet = charSet;
}
/// Prevents a default instance of the class from being created.
[ExcludeFromCodeCoverage]
protected SafeMemString() : base(0) { }
- /// Initializes a new instance of the class.
+ ///
+ /// Initializes a new instance of the class.
+ ///
/// The PTR.
/// The character set.
/// true to reliably release the handle during finalization; false to prevent it.
+ /// The number of bytes allocated to .
[ExcludeFromCodeCoverage]
- protected SafeMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true) : base(ptr, 0, ownsHandle)
+ protected SafeMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) : base(ptr, allocatedBytes, ownsHandle)
{
- chSet = charSet;
+ CharSet = charSet;
}
/// Gets the number of allocated characters or 0 if the size is unknown (for example if it is holding a .
/// The number of allocated characters.
- public int Capacity => Size / StringHelper.GetCharSize(chSet);
+ public int Capacity
+ {
+ get => Size / StringHelper.GetCharSize(CharSet);
+ set => Size = value * StringHelper.GetCharSize(CharSet);
+ }
+
+ /// Gets the character set of the assigned string.
+ /// The character set.
+ public CharSet CharSet { get; private set; } = CharSet.Unicode;
/// Gets the number of characters in the current object.
public int Length => ToString().Length;
@@ -73,7 +82,7 @@ namespace Vanara.InteropServices
/// An representing the value of the handle field. If the handle has been marked invalid with
/// , this method still returns the original handle value, which can be a stale value.
///
- public static explicit operator IntPtr(SafeMemString s) => s.DangerousGetHandle();
+ public static implicit operator IntPtr(SafeMemString s) => s.DangerousGetHandle();
/// Returns the string value held by a .
/// The instance.
@@ -82,8 +91,14 @@ namespace Vanara.InteropServices
///
public static implicit operator string(SafeMemString s) => s?.ToString();
+ /// Performs an explicit conversion from to .
+ /// The instance.
+ /// The result of the conversion.
+ /// Cannot convert an ANSI string to a Char pointer.
+ public static unsafe explicit operator char*(SafeMemString s) => s.CharSet == CharSet.Unicode ? (char*)(void*)s.handle : throw new InvalidCastException("Cannot convert an ANSI string to a Char pointer.");
+
/// Returns the string value held by this instance.
/// A value held by this instance or null if the handle is invalid.
- public override string ToString() => IsInvalid ? null : StringHelper.GetString(handle, chSet, Size);
+ public override string ToString() => IsInvalid ? null : StringHelper.GetString(handle, CharSet, Size);
}
}
\ No newline at end of file