2019-06-25 19:10:57 -04:00
|
|
|
|
using System;
|
2017-11-27 12:18:01 -05:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security;
|
|
|
|
|
|
|
|
|
|
namespace Vanara.InteropServices
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Safely handles an unmanaged memory allocated Unicode string.</summary>
|
2019-06-25 19:10:57 -04:00
|
|
|
|
public class SafeCoTaskMemString : SafeMemString<CoTaskMemoryMethods>
|
2017-11-27 12:18:01 -05:00
|
|
|
|
{
|
2019-06-25 19:10:57 -04:00
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
|
|
|
|
/// <param name="s">The string value.</param>
|
|
|
|
|
/// <param name="charSet">The character set.</param>
|
|
|
|
|
public SafeCoTaskMemString(string s, CharSet charSet = CharSet.Unicode) : base(s, charSet)
|
|
|
|
|
{
|
|
|
|
|
}
|
2017-11-27 12:18:01 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
|
|
|
|
/// <param name="s">The string value.</param>
|
2019-06-25 19:10:57 -04:00
|
|
|
|
/// <param name="capacity">The size of the buffer in characters.</param>
|
2017-11-27 12:18:01 -05:00
|
|
|
|
/// <param name="charSet">The character set.</param>
|
2019-06-25 19:10:57 -04:00
|
|
|
|
public SafeCoTaskMemString(string s, int capacity, CharSet charSet = CharSet.Unicode) : base(s, capacity, charSet)
|
2017-11-27 12:18:01 -05:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
|
|
|
|
/// <param name="s">The string value.</param>
|
|
|
|
|
/// <param name="charSet">The character set.</param>
|
2019-06-25 19:10:57 -04:00
|
|
|
|
public SafeCoTaskMemString(SecureString s, CharSet charSet = CharSet.Unicode) : base(s, charSet)
|
2017-11-27 12:18:01 -05:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
2019-08-23 13:52:14 -04:00
|
|
|
|
/// <param name="capacity">The size of the buffer in characters, including the null character terminator.</param>
|
2017-11-27 12:18:01 -05:00
|
|
|
|
/// <param name="charSet">The character set.</param>
|
2019-08-23 13:52:14 -04:00
|
|
|
|
public SafeCoTaskMemString(int capacity, CharSet charSet = CharSet.Unicode) : base(capacity, charSet)
|
2017-11-27 12:18:01 -05:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="SafeCoTaskMemString"/> class.</summary>
|
|
|
|
|
/// <param name="ptr">The PTR.</param>
|
|
|
|
|
/// <param name="charSet">The character set.</param>
|
2018-01-15 13:57:06 -05:00
|
|
|
|
/// <param name="ownsHandle"><c>true</c> to reliably release the handle during finalization; <c>false</c> to prevent it.</param>
|
2019-08-23 13:52:14 -04:00
|
|
|
|
/// <param name="allocatedBytes">The number of bytes allocated to <paramref name="ptr"/>.</param>
|
2017-11-27 12:18:01 -05:00
|
|
|
|
[ExcludeFromCodeCoverage]
|
2019-08-23 13:52:14 -04:00
|
|
|
|
private SafeCoTaskMemString(IntPtr ptr, CharSet charSet = CharSet.Unicode, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) :
|
|
|
|
|
base(ptr, charSet, ownsHandle, allocatedBytes)
|
2017-11-27 12:18:01 -05:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 13:52:14 -04:00
|
|
|
|
/// <summary>Represents a <c>null</c> value. Used primarily for comparison.</summary>
|
2018-05-29 19:45:53 -04:00
|
|
|
|
/// <value>A null value.</value>
|
|
|
|
|
public static SafeCoTaskMemString Null => new SafeCoTaskMemString(IntPtr.Zero, CharSet.Unicode, false);
|
2017-11-27 12:18:01 -05:00
|
|
|
|
}
|
|
|
|
|
}
|