internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntPtr pNativeBuffer) { if (strManaged == null) { return(IntPtr.Zero); } System.StubHelpers.StubHelpers.CheckStringLength(strManaged.Length); byte *numPtr = (byte *)(void *)pNativeBuffer; int cbLength; if ((IntPtr)numPtr != IntPtr.Zero || Marshal.SystemMaxDBCSCharSize == 1) { int num = (strManaged.Length + 1) * Marshal.SystemMaxDBCSCharSize; if ((IntPtr)numPtr == IntPtr.Zero) { numPtr = (byte *)(void *)Marshal.AllocCoTaskMem(num + 1); } cbLength = strManaged.ConvertToAnsi(numPtr, num + 1, (uint)(flags & (int)byte.MaxValue) > 0U, (uint)(flags >> 8) > 0U); } else { byte[] src = AnsiCharMarshaler.DoAnsiConversion(strManaged, (uint)(flags & (int)byte.MaxValue) > 0U, (uint)(flags >> 8) > 0U, out cbLength); numPtr = (byte *)(void *)Marshal.AllocCoTaskMem(cbLength + 2); Buffer.Memcpy(numPtr, 0, src, 0, cbLength); } numPtr[cbLength] = (byte)0; numPtr[cbLength + 1] = (byte)0; return((IntPtr)((void *)numPtr)); }
internal static unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch) { if (strManaged == null) { return(IntPtr.Zero); } cch = strManaged.Length; System.StubHelpers.StubHelpers.CheckStringLength(cch); int cb = 4 + ((cch + 1) * Marshal.SystemMaxDBCSCharSize); byte *pDest = (byte *)Marshal.AllocCoTaskMem(cb); int * numPtr2 = (int *)pDest; pDest += 4; if (cch == 0) { pDest[0] = 0; numPtr2[0] = 0; } else { int num2; Buffer.memcpy(AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit, fThrowOnUnmappableChar, out num2), 0, pDest, 0, num2); pDest[num2] = 0; numPtr2[0] = num2; } return(new IntPtr((void *)pDest)); }
internal static unsafe IntPtr ConvertToNative(int flags, string strManaged) { if (null == strManaged) { return(IntPtr.Zero); } byte[]? bytes = null; int nb = 0; if (strManaged.Length > 0) { bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8), out nb); } uint length = (uint)nb; IntPtr bstr = Marshal.AllocBSTRByteLen(length); if (bytes != null) { Buffer.Memmove(ref *(byte *)bstr, ref MemoryMarshal.GetArrayDataReference(bytes), length); } return(bstr); }
private unsafe IntPtr ConvertStringBuilderToNative(StringBuilder pManagedHome, int dwFlags) { IntPtr intPtr; if (AsAnyMarshaler.IsAnsi(dwFlags)) { StubHelpers.CheckStringLength(pManagedHome.Capacity); int num = pManagedHome.Capacity * Marshal.SystemMaxDBCSCharSize + 4; intPtr = Marshal.AllocCoTaskMem(num); byte *ptr = (byte *)((void *)intPtr); *(ptr + num - 3) = 0; *(ptr + num - 2) = 0; *(ptr + num - 1) = 0; if (AsAnyMarshaler.IsIn(dwFlags)) { int num2; byte[] src = AnsiCharMarshaler.DoAnsiConversion(pManagedHome.ToString(), AsAnyMarshaler.IsBestFit(dwFlags), AsAnyMarshaler.IsThrowOn(dwFlags), out num2); Buffer.Memcpy(ptr, 0, src, 0, num2); ptr[num2] = 0; } if (AsAnyMarshaler.IsOut(dwFlags)) { this.backPropAction = AsAnyMarshaler.BackPropAction.StringBuilderAnsi; } } else { int num3 = pManagedHome.Capacity * 2 + 4; intPtr = Marshal.AllocCoTaskMem(num3); byte *ptr2 = (byte *)((void *)intPtr); *(ptr2 + num3 - 1) = 0; *(ptr2 + num3 - 2) = 0; if (AsAnyMarshaler.IsIn(dwFlags)) { int num4 = pManagedHome.Length * 2; pManagedHome.InternalCopy(intPtr, num4); ptr2[num4] = 0; (ptr2 + num4)[1] = 0; } if (AsAnyMarshaler.IsOut(dwFlags)) { this.backPropAction = AsAnyMarshaler.BackPropAction.StringBuilderUnicode; } } return(intPtr); }
private unsafe IntPtr ConvertStringBuilderToNative(StringBuilder pManagedHome, int dwFlags) { IntPtr dest; if (AsAnyMarshaler.IsAnsi(dwFlags)) { System.StubHelpers.StubHelpers.CheckStringLength(pManagedHome.Capacity); int cb = pManagedHome.Capacity * Marshal.SystemMaxDBCSCharSize + 4; dest = Marshal.AllocCoTaskMem(cb); byte *pDest = (byte *)(void *)dest; *(pDest + cb - 3) = (byte)0; *(pDest + cb - 2) = (byte)0; *(pDest + cb - 1) = (byte)0; if (AsAnyMarshaler.IsIn(dwFlags)) { int cbLength; byte[] src = AnsiCharMarshaler.DoAnsiConversion(pManagedHome.ToString(), AsAnyMarshaler.IsBestFit(dwFlags), AsAnyMarshaler.IsThrowOn(dwFlags), out cbLength); Buffer.Memcpy(pDest, 0, src, 0, cbLength); pDest[cbLength] = (byte)0; } if (AsAnyMarshaler.IsOut(dwFlags)) { this.backPropAction = AsAnyMarshaler.BackPropAction.StringBuilderAnsi; } } else { int cb = pManagedHome.Capacity * 2 + 4; dest = Marshal.AllocCoTaskMem(cb); byte *numPtr = (byte *)(void *)dest; *(numPtr + cb - 1) = (byte)0; *(numPtr + cb - 2) = (byte)0; if (AsAnyMarshaler.IsIn(dwFlags)) { int len = pManagedHome.Length * 2; pManagedHome.InternalCopy(dest, len); (numPtr + len)[0] = (byte)0; (numPtr + len)[1] = (byte)0; } if (AsAnyMarshaler.IsOut(dwFlags)) { this.backPropAction = AsAnyMarshaler.BackPropAction.StringBuilderUnicode; } } return(dest); }
private unsafe IntPtr ConvertStringBuilderToNative(StringBuilder pManagedHome, int dwFlags) { IntPtr ptr; if (IsAnsi(dwFlags)) { System.StubHelpers.StubHelpers.CheckStringLength(pManagedHome.Capacity); int num = (pManagedHome.Capacity * Marshal.SystemMaxDBCSCharSize) + 4; ptr = Marshal.AllocCoTaskMem(num); byte *pDest = (byte *)ptr; *((pDest + num) - 3) = 0; *((pDest + num) - 2) = 0; *((pDest + num) - 1) = 0; if (IsIn(dwFlags)) { int num2; Buffer.memcpy(AnsiCharMarshaler.DoAnsiConversion(pManagedHome.ToString(), IsBestFit(dwFlags), IsThrowOn(dwFlags), out num2), 0, pDest, 0, num2); pDest[num2] = 0; } if (IsOut(dwFlags)) { this.backPropAction = BackPropAction.StringBuilderAnsi; } return(ptr); } int cb = (pManagedHome.Capacity * 2) + 4; ptr = Marshal.AllocCoTaskMem(cb); byte *numPtr2 = (byte *)ptr; *((numPtr2 + cb) - 1) = 0; *((numPtr2 + cb) - 2) = 0; if (IsIn(dwFlags)) { int len = pManagedHome.Length * 2; pManagedHome.InternalCopy(ptr, len); numPtr2[len] = 0; (numPtr2 + len)[1] = 0; } if (IsOut(dwFlags)) { this.backPropAction = BackPropAction.StringBuilderUnicode; } return(ptr); }
internal static IntPtr ConvertToNative(int flags, string strManaged) { if (strManaged == null) { return(IntPtr.Zero); } int length = strManaged.Length; StubHelpers.CheckStringLength(length); byte[] str = null; int len = 0; if (length > 0) { str = AnsiCharMarshaler.DoAnsiConversion(strManaged, (flags & 255) != 0, flags >> 8 != 0, out len); } return(Win32Native.SysAllocStringByteLen(str, (uint)len)); }
internal static IntPtr ConvertToNative(int flags, string strManaged) { if (strManaged == null) { return(IntPtr.Zero); } int length = strManaged.Length; System.StubHelpers.StubHelpers.CheckStringLength(length); byte[] str = null; int cbLength = 0; if (length > 0) { str = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xff), 0 != (flags >> 8), out cbLength); } return(Win32Native.SysAllocStringByteLen(str, (uint)cbLength)); }
internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntPtr pNativeBuffer) { int num; if (strManaged == null) { return(IntPtr.Zero); } System.StubHelpers.StubHelpers.CheckStringLength(strManaged.Length); byte[] src = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xff), 0 != (flags >> 8), out num); byte * pDest = (byte *)pNativeBuffer; if (pDest == null) { pDest = (byte *)Marshal.AllocCoTaskMem(num + 2); } Buffer.memcpy(src, 0, pDest, 0, num); pDest[num] = 0; pDest[num + 1] = 0; return((IntPtr)pDest); }
internal static unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch) { if (null == strManaged) { return(IntPtr.Zero); } byte *pNative; cch = strManaged.Length; // length field at negative offset + (# of characters incl. the terminator) * max ANSI char size int nbytes = checked (sizeof(uint) + ((cch + 1) * Marshal.SystemMaxDBCSCharSize)); pNative = (byte *)Marshal.AllocCoTaskMem(nbytes); int *pLength = (int *)pNative; pNative += sizeof(uint); if (0 == cch) { *pNative = 0; *pLength = 0; } else { byte[] bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit, fThrowOnUnmappableChar, out int nbytesused); Debug.Assert(nbytesused >= 0 && nbytesused < nbytes, "Insufficient buffer allocated in VBByValStrMarshaler.ConvertToNative"); Buffer.Memmove(ref *pNative, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nbytesused); pNative[nbytesused] = 0; *pLength = nbytesused; } return(new IntPtr(pNative)); }
internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntPtr pNativeBuffer) { if (null == strManaged) { return(IntPtr.Zero); } int nb; byte *pbNativeBuffer = (byte *)pNativeBuffer; if (pbNativeBuffer != null || Marshal.SystemMaxDBCSCharSize == 1) { // If we are marshaling into a stack buffer or we can accurately estimate the size of the required heap // space, we will use a "1-pass" mode where we convert the string directly into the unmanaged buffer. // + 1 for the null character from the user. + 1 for the null character we put in. nb = checked ((strManaged.Length + 1) * Marshal.SystemMaxDBCSCharSize + 1); bool didAlloc = false; // Use the pre-allocated buffer (allocated by localloc IL instruction) if not NULL, // otherwise fallback to AllocCoTaskMem if (pbNativeBuffer == null) { pbNativeBuffer = (byte *)Marshal.AllocCoTaskMem(nb); didAlloc = true; } try { nb = Marshal.StringToAnsiString(strManaged, pbNativeBuffer, nb, bestFit: 0 != (flags & 0xFF), throwOnUnmappableChar: 0 != (flags >> 8)); } catch (Exception) when(didAlloc) { Marshal.FreeCoTaskMem((IntPtr)pbNativeBuffer); throw; } } else { if (strManaged.Length == 0) { nb = 0; pbNativeBuffer = (byte *)Marshal.AllocCoTaskMem(2); } else { // Otherwise we use a slower "2-pass" mode where we first marshal the string into an intermediate buffer // (managed byte array) and then allocate exactly the right amount of unmanaged memory. This is to avoid // wasting memory on systems with multibyte character sets where the buffer we end up with is often much // smaller than the upper bound for the given managed string. byte[] bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit: 0 != (flags & 0xFF), fThrowOnUnmappableChar: 0 != (flags >> 8), out nb); // + 1 for the null character from the user. + 1 for the null character we put in. pbNativeBuffer = (byte *)Marshal.AllocCoTaskMem(nb + 2); Buffer.Memmove(ref *pbNativeBuffer, ref MemoryMarshal.GetArrayDataReference(bytes), (nuint)nb); } } pbNativeBuffer[nb] = 0x00; pbNativeBuffer[nb + 1] = 0x00; return((IntPtr)pbNativeBuffer); }