Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
VBByValStrMarshaler.cs
Go to the documentation of this file.
2
3namespace System.StubHelpers;
4
5internal static class VBByValStrMarshaler
6{
7 internal unsafe static IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch)
8 {
9 if (strManaged == null)
10 {
11 return IntPtr.Zero;
12 }
13 cch = strManaged.Length;
14 int cb = checked(4 + (cch + 1) * Marshal.SystemMaxDBCSCharSize);
15 byte* ptr = (byte*)(void*)Marshal.AllocCoTaskMem(cb);
16 int* ptr2 = (int*)ptr;
17 ptr += 4;
18 if (cch == 0)
19 {
20 *ptr = 0;
21 *ptr2 = 0;
22 }
23 else
24 {
25 int cbLength;
26 byte[] array = AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit, fThrowOnUnmappableChar, out cbLength);
27 Buffer.Memmove(ref *ptr, ref MemoryMarshal.GetArrayDataReference(array), (nuint)cbLength);
28 ptr[cbLength] = 0;
29 *ptr2 = cbLength;
30 }
31 return new IntPtr(ptr);
32 }
33
34 internal unsafe static string ConvertToManaged(IntPtr pNative, int cch)
35 {
36 if (IntPtr.Zero == pNative)
37 {
38 return null;
39 }
40 return new string((sbyte*)(void*)pNative, 0, cch);
41 }
42
43 internal static void ClearNative(IntPtr pNative)
44 {
45 if (IntPtr.Zero != pNative)
46 {
47 Marshal.FreeCoTaskMem((IntPtr)((long)pNative - 4));
48 }
49 }
50}
static void Memmove(ref byte dest, ref byte src, nuint len)
Definition Buffer.cs:215
static void FreeCoTaskMem(IntPtr ptr)
Definition Marshal.cs:1712
static IntPtr AllocCoTaskMem(int cb)
Definition Marshal.cs:1702
static readonly int SystemMaxDBCSCharSize
Definition Marshal.cs:19
static unsafe ref byte GetArrayDataReference(Array array)
static unsafe byte[] DoAnsiConversion(string str, bool fBestFit, bool fThrowOnUnmappableChar, out int cbLength)
static void ClearNative(IntPtr pNative)
static unsafe string ConvertToManaged(IntPtr pNative, int cch)
static unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch)
static readonly IntPtr Zero
Definition IntPtr.cs:18