#nullable enable using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Security; namespace Vanara.InteropServices; /// Safely handles an unmanaged memory allocated Unicode string. public class SafeCoTaskMemString : SafeMemString { /// Initializes a new instance of the class. /// The string value. /// The character set. public SafeCoTaskMemString(string? s, CharSet charSet = CharSet.Unicode) : base(s, charSet) { } /// Initializes a new instance of the class. /// The string value. /// The size of the buffer in characters. /// The character set. public SafeCoTaskMemString(string? s, int capacity, CharSet charSet = CharSet.Unicode) : base(s, capacity, charSet) { } /// Initializes a new instance of the class. /// The string value. /// The character set. public SafeCoTaskMemString(SecureString s, CharSet charSet = CharSet.Unicode) : base(s, charSet) { } /// Initializes a new instance of the class. /// The size of the buffer in characters, including the null character terminator. /// The character set. public SafeCoTaskMemString(int capacity, CharSet charSet = CharSet.Unicode) : base(capacity, charSet) { } /// Prevents a default instance of the class from being created. private SafeCoTaskMemString() : base() { } /// 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] 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 comparison. /// A null value. public static SafeCoTaskMemString Null => new(IntPtr.Zero, CharSet.Unicode, false); }