Some comments on StringToPtrUTF8.

This commit is contained in:
Eugene Wang 2021-04-27 07:45:16 -04:00
parent 714caf4935
commit 7a1cf2b5fe

View File

@ -20,8 +20,14 @@ namespace NTwain
/// <param name="value"></param> /// <param name="value"></param>
/// <param name="length">Actual number of bytes used to encode the string without the null.</param> /// <param name="length">Actual number of bytes used to encode the string without the null.</param>
/// <returns></returns> /// <returns></returns>
internal static unsafe IntPtr StringToPtrUTF8(TWAIN twain, string value, out int length) public static unsafe IntPtr StringToPtrUTF8(TWAIN twain, string value, out int length)
{ {
if (value == null)
{
length = 0;
return IntPtr.Zero;
}
var utf8 = Encoding.UTF8; var utf8 = Encoding.UTF8;
length = utf8.GetByteCount(value); length = utf8.GetByteCount(value);
@ -32,6 +38,7 @@ namespace NTwain
byte* bytes = (byte*)ptr; byte* bytes = (byte*)ptr;
try try
{ {
// fixed for managed pointer
fixed (char* firstChar = value) fixed (char* firstChar = value)
{ {
written = Encoding.UTF8.GetBytes(firstChar, value.Length, bytes, length); written = Encoding.UTF8.GetBytes(firstChar, value.Length, bytes, length);