Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / BCL / System / StubHelpers.cs / 5 / StubHelpers.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== namespace System.StubHelpers { using System.Text; using Microsoft.Win32; using System.Security; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; #if WIN64 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class AnsiCharMarshaler { static internal byte[] DoAnsiConversion(string str, bool fBestFit, bool fThrowOnUnmappableChar) { byte[] bytes = null; Encoding enc = null; bool fLegacyPath = false; if (fThrowOnUnmappableChar) { if (fBestFit) { fLegacyPath = true; } else { enc = Encoding.GetEncoding(0, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); } } else { if (fBestFit) { enc = Encoding.Default; } else { enc = Encoding.GetEncoding(0, EncoderFallback.ReplacementFallback, DecoderFallback.ReplacementFallback); } } if (fLegacyPath) { bytes = str.ConvertToAnsi_BestFit_Throw(Marshal.SystemMaxDBCSCharSize); } else { try { bytes = enc.GetBytes(str); } catch (EncoderFallbackException fallbackException) { throw new ArgumentException(Environment.GetResourceString("Interop_Marshal_Unmappable_Char"), fallbackException); } } return bytes; } static internal byte ConvertToNative(char managedChar, bool fBestFit, bool fThrowOnUnmappableChar) { byte[] bytes = DoAnsiConversion(managedChar.ToString(), fBestFit, fThrowOnUnmappableChar); return bytes[0]; } static internal char ConvertToManaged(byte nativeChar) { byte[] bytes = new byte[1] { nativeChar }; string str = Encoding.Default.GetString(bytes); return str[0]; } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class CSTRMarshaler { static internal unsafe IntPtr ConvertToNative(int flags, string strManaged) { if (null == strManaged) { return IntPtr.Zero; } else { int length = strManaged.Length; StubHelpers.CheckStringLength(length); byte[] bytes = null; int nb = 0; bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8)); nb = bytes.Length; // + 1 for the null character from the user. + 1 for the null character we put in. int cbNativeBuffer = (length + 2) * Marshal.SystemMaxDBCSCharSize; // In the (fBestFit && fThrowOnUnmappableChar) case, we'll actually call ConvertToAnsi_BestFit_Throw() // to do the conversion. This method allocates the buffer according to the worst case scenario, just // like what we do here. Thus, the length of the returned array could be bigger than our estimate. // So we need to take the maximum of the two values here. cbNativeBuffer = Math.Max(cbNativeBuffer, nb + 2); byte* pbNativeBuffer = (byte*)Win32Native.CoTaskMemAlloc(cbNativeBuffer); BCLDebug.Assert((nb + 2) <= cbNativeBuffer, "Underestimated buffer size"); Buffer.memcpy(bytes, 0, pbNativeBuffer, 0, nb); pbNativeBuffer[nb] = 0x00; pbNativeBuffer[nb+1] = 0x00; return (IntPtr)pbNativeBuffer; } } static internal unsafe string ConvertToManaged(IntPtr cstr) { if (IntPtr.Zero == cstr) return null; else return new String((sbyte*)cstr); } static internal void ClearNative(IntPtr pNative) { Win32Native.CoTaskMemFree(pNative); } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class BSTRMarshaler { static internal IntPtr ConvertToNative(string strManaged) { if (null == strManaged) return IntPtr.Zero; else return Win32Native.SysAllocStringLen(strManaged, strManaged.Length); } static internal unsafe string ConvertToManaged(IntPtr bstr) { if (IntPtr.Zero == bstr) return null; else return new String((char*)bstr, 0, Win32Native.SysStringLen(bstr)); } static internal void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.SysFreeString(pNative); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class VBByValStrMarshaler { static internal unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch) { if (null == strManaged) { return IntPtr.Zero; } byte* pNative; cch = strManaged.Length; StubHelpers.CheckStringLength(cch); int nbytes = sizeof(uint) + (cch+1)*2; pNative = (byte*)Win32Native.CoTaskMemAlloc(nbytes); int* pLength = (int*)pNative; pNative = pNative + sizeof(uint); if (0 == cch) { *pNative = 0; *pLength = 0; } else { byte[] bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit, fThrowOnUnmappableChar); int nbytesused = bytes.Length; Buffer.memcpy(bytes, 0, pNative, 0, nbytesused); pNative[nbytesused] = 0; *pLength = nbytesused; } return new IntPtr(pNative); } static internal unsafe string ConvertToManaged(IntPtr pNative, int cch) { if (IntPtr.Zero == pNative) { return null; } return new String((sbyte*)pNative, 0, cch); } static internal unsafe void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class AnsiBSTRMarshaler { static internal unsafe IntPtr ConvertToNative(int flags, string strManaged) { if (null == strManaged) { return IntPtr.Zero; } int length = strManaged.Length; StubHelpers.CheckStringLength(length); byte[] bytes = null; int nb = 0; if (length > 0) { bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8)); nb = bytes.Length; } // + 1 for the null character from the user. + 1 for the null character we put in. int cbNativeBuffer = (length + 2) * Marshal.SystemMaxDBCSCharSize + sizeof(int); // In the (fBestFit && fThrowOnUnmappableChar) case, we'll actually call ConvertToAnsi_BestFit_Throw() // to do the conversion. This method allocates the buffer according to the worst case scenario, just // like what we do here. Thus, the length of the returned array could be bigger than our estimate. // So we need to take the maximum of the two values here. cbNativeBuffer = Math.Max(cbNativeBuffer, nb + 2 + sizeof(int)); byte* pbNativeBuffer = ((byte*)Win32Native.CoTaskMemAlloc(cbNativeBuffer)) + sizeof(int); BCLDebug.Assert((nb + 2) <= cbNativeBuffer, "Underestimated buffer size"); Buffer.memcpy(bytes, 0, pbNativeBuffer, 0, nb); pbNativeBuffer[nb] = 0x00; pbNativeBuffer[nb+1] = 0x00; ((int*)pbNativeBuffer)[-1] = nb; return (IntPtr)pbNativeBuffer; } static internal unsafe string ConvertToManaged(IntPtr bstr) { if (IntPtr.Zero == bstr) return null; else return new String((sbyte*)bstr); } static internal unsafe void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class WSTRBufferMarshaler { static internal IntPtr ConvertToNative(string strManaged) { BCLDebug.Assert(false, "NYI"); return IntPtr.Zero; } static internal unsafe string ConvertToManaged(IntPtr bstr) { BCLDebug.Assert(false, "NYI"); return null; } static internal void ClearNative(IntPtr pNative) { BCLDebug.Assert(false, "NYI"); } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class ObjectMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToNative(object objSrc, IntPtr pDstVariant); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object ConvertToManaged(IntPtr pSrcVariant); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pVariant); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class ValueClassMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToManaged(IntPtr dst, IntPtr src, IntPtr pMT); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr dst, IntPtr pMT); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class DateMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern double ConvertToNative(DateTime managedDate); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern DateTime ConvertToManaged(double nativeDate); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class InterfaceMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr ConvertToNative(object objSrc, IntPtr itfMT, IntPtr classMT, int flags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object ConvertToManaged(IntPtr pUnk, IntPtr itfMT, IntPtr classMT, int flags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pUnk); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdNativeArrayMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, int sizeParam, IntPtr pMT, int additive, int multiplier, int dwFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNativeContents(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdSafeArrayMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, IntPtr pMT, int iRank, int dwFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome, object pOriginalManaged); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdRefCustomMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, IntPtr pCMHelper); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [StructLayout(LayoutKind.Sequential)] internal struct NativeVariant { ushort vt; ushort wReserved1; ushort wReserved2; ushort wReserved3; long data1; long data2; } // // This struct maps the the EE's Frame struct in vm\Frames.h // [StructLayout(LayoutKind.Sequential)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal unsafe struct EEFrame { internal void* __VFN_table; internal void* m_Next; internal const long OFFSETOF__Thread__m_pFrame = 0x10L; // // pass in an explicit 'this' pointer so that we don't have to // use 'fixed' to take the address of 'this' // internal static void Push(void* pThis, void* pThread) { EEFrame* pThisFrame = (EEFrame*)pThis; #if _DEBUG StubHelpers.Verify__EEFrame__Push(pThisFrame, pThread, OFFSETOF__Thread__m_pFrame); #endif // _DEBUG void** ppFrame = (void**)(((byte*)pThread) + OFFSETOF__Thread__m_pFrame); pThisFrame->m_Next = *ppFrame; *ppFrame = pThis; } // // uses an explicit 'this' pointer to be consistent with Push above // internal static void Pop(void* pThis, void* pThread) { EEFrame* pThisFrame = (EEFrame*)pThis; #if _DEBUG StubHelpers.Verify__EEFrame__Pop(pThisFrame, pThread, OFFSETOF__Thread__m_pFrame); #endif // _DEBUG void** ppFrame = (void**)(((byte*)pThread) + OFFSETOF__Thread__m_pFrame); *ppFrame = pThisFrame->m_Next; } } [StructLayoutAttribute(LayoutKind.Sequential)] internal struct CY { internal long int64; } [StructLayout(LayoutKind.Sequential)] internal struct DECIMAL { internal short wReserved; internal short signscale; internal int Hi32; internal long Lo64; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] [SuppressUnmanagedCodeSecurityAttribute()] internal static class StubHelpers { internal const String OLEAUT32 = "oleaut32.dll"; [DllImport(OLEAUT32)] internal extern static int VarCyFromDec(ref DECIMAL pdecIn, ref CY cyOut); [DllImport(OLEAUT32)] internal extern static int VarDecFromCy(CY cyIn, ref DECIMAL decOut); #if _DEBUG [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void Verify__EEFrame__Pop(EEFrame* pFrame, void* pThread, long managed_OFFSETOF__Thread__m_pFrame); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void Verify__EEFrame__Push(EEFrame* pFrame, void* pThread, long managed_OFFSETOF__Thread__m_pFrame); #endif // _DEBUG //-------------------------------------------------------- // PInvoke stub helpers //------------------------------------------------------- #if DEBUG [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern UIntPtr GetProcessGSCookie(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void FailFast(); #endif [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DoNDirectCall(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DoCLRToCOMCall(object thisPtr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void SetLastError(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginStandalone(IntPtr pFrame, IntPtr pNMD, int dwStubFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginStandaloneCleanup(IntPtr pFrame, IntPtr pNMD, int dwStubFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginCLRToCOMStandalone(IntPtr pFrame, IntPtr pCPCMD, int dwStubFlags, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginCLRToCOMStandaloneCleanup(IntPtr pFrame, IntPtr pCPCMD, int dwStubFlags, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ThrowDeferredException(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ThrowInteropParamException(int resID, int paramIdx); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateCleanupList(IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DestroyCleanupList(IntPtr pFrameMem, bool fExceptionThrown); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object GetHRExceptionObject(int hr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object GetCOMHRExceptionObject(int hr, IntPtr pFrame, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr CreateCustomMarshalerHelper(IntPtr pMD, int paramToken, IntPtr hndManagedType); //------------------------------------------------------- // AsAnyAMarshaler, AsAnyWMarshaler //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr MarshalAsAny(ref object pManagedHome, int dwFlags, IntPtr pvMarshaler, IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void UnmarshalAsAny(ref object pManagedHome, IntPtr pvMarshaler); //-------------------------------------------------------- // SafeHandle Helper //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr SafeHandleC2NHelper(object pThis, IntPtr pCleanupWorkList); //-------------------------------------------------------- // Helper for the MDA ----OnRCWCleanup //-------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void StubRegisterRCW(object pThis, IntPtr pThread, IntPtr pCleanupWorkList); //------------------------------------------------------- // Profiler helpers //-------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr ProfilerBeginTransitionCallback(IntPtr pSecretParam, IntPtr pThread, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ProfilerEndTransitionCallback(IntPtr pMD, IntPtr pThread); //------------------------------------------------------- // Debugger helpers //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DebuggerTraceCall(IntPtr pSecretParam); //----------------------------------------------------- // misc //------------------------------------------------------ static internal void CheckStringLength(int length) { if (length > 0x7ffffff0) { throw new MarshalDirectiveException(Environment.GetResourceString("Marshaler_StringTooLong")); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern int strlen(sbyte* ptr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern Decimal DecimalCanonicalizeInternal(Decimal dec); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void FmtClassUpdateNativeInternal(object obj, byte* pNative, IntPtr pOptionalCleanupList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void FmtClassUpdateCLRInternal(object obj, byte* pNative); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void LayoutDestroyNativeInternal(byte* pNative, IntPtr pMT); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object AllocateInternal(IntPtr typeHandle); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr MarshalToUnmanagedVaListInternal(IntPtr pArgIterator); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void MarshalToManagedVaListInternal(IntPtr va_list, IntPtr pArgIterator); } #endif } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== namespace System.StubHelpers { using System.Text; using Microsoft.Win32; using System.Security; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; #if WIN64 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class AnsiCharMarshaler { static internal byte[] DoAnsiConversion(string str, bool fBestFit, bool fThrowOnUnmappableChar) { byte[] bytes = null; Encoding enc = null; bool fLegacyPath = false; if (fThrowOnUnmappableChar) { if (fBestFit) { fLegacyPath = true; } else { enc = Encoding.GetEncoding(0, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback); } } else { if (fBestFit) { enc = Encoding.Default; } else { enc = Encoding.GetEncoding(0, EncoderFallback.ReplacementFallback, DecoderFallback.ReplacementFallback); } } if (fLegacyPath) { bytes = str.ConvertToAnsi_BestFit_Throw(Marshal.SystemMaxDBCSCharSize); } else { try { bytes = enc.GetBytes(str); } catch (EncoderFallbackException fallbackException) { throw new ArgumentException(Environment.GetResourceString("Interop_Marshal_Unmappable_Char"), fallbackException); } } return bytes; } static internal byte ConvertToNative(char managedChar, bool fBestFit, bool fThrowOnUnmappableChar) { byte[] bytes = DoAnsiConversion(managedChar.ToString(), fBestFit, fThrowOnUnmappableChar); return bytes[0]; } static internal char ConvertToManaged(byte nativeChar) { byte[] bytes = new byte[1] { nativeChar }; string str = Encoding.Default.GetString(bytes); return str[0]; } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class CSTRMarshaler { static internal unsafe IntPtr ConvertToNative(int flags, string strManaged) { if (null == strManaged) { return IntPtr.Zero; } else { int length = strManaged.Length; StubHelpers.CheckStringLength(length); byte[] bytes = null; int nb = 0; bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8)); nb = bytes.Length; // + 1 for the null character from the user. + 1 for the null character we put in. int cbNativeBuffer = (length + 2) * Marshal.SystemMaxDBCSCharSize; // In the (fBestFit && fThrowOnUnmappableChar) case, we'll actually call ConvertToAnsi_BestFit_Throw() // to do the conversion. This method allocates the buffer according to the worst case scenario, just // like what we do here. Thus, the length of the returned array could be bigger than our estimate. // So we need to take the maximum of the two values here. cbNativeBuffer = Math.Max(cbNativeBuffer, nb + 2); byte* pbNativeBuffer = (byte*)Win32Native.CoTaskMemAlloc(cbNativeBuffer); BCLDebug.Assert((nb + 2) <= cbNativeBuffer, "Underestimated buffer size"); Buffer.memcpy(bytes, 0, pbNativeBuffer, 0, nb); pbNativeBuffer[nb] = 0x00; pbNativeBuffer[nb+1] = 0x00; return (IntPtr)pbNativeBuffer; } } static internal unsafe string ConvertToManaged(IntPtr cstr) { if (IntPtr.Zero == cstr) return null; else return new String((sbyte*)cstr); } static internal void ClearNative(IntPtr pNative) { Win32Native.CoTaskMemFree(pNative); } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class BSTRMarshaler { static internal IntPtr ConvertToNative(string strManaged) { if (null == strManaged) return IntPtr.Zero; else return Win32Native.SysAllocStringLen(strManaged, strManaged.Length); } static internal unsafe string ConvertToManaged(IntPtr bstr) { if (IntPtr.Zero == bstr) return null; else return new String((char*)bstr, 0, Win32Native.SysStringLen(bstr)); } static internal void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.SysFreeString(pNative); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class VBByValStrMarshaler { static internal unsafe IntPtr ConvertToNative(string strManaged, bool fBestFit, bool fThrowOnUnmappableChar, ref int cch) { if (null == strManaged) { return IntPtr.Zero; } byte* pNative; cch = strManaged.Length; StubHelpers.CheckStringLength(cch); int nbytes = sizeof(uint) + (cch+1)*2; pNative = (byte*)Win32Native.CoTaskMemAlloc(nbytes); int* pLength = (int*)pNative; pNative = pNative + sizeof(uint); if (0 == cch) { *pNative = 0; *pLength = 0; } else { byte[] bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, fBestFit, fThrowOnUnmappableChar); int nbytesused = bytes.Length; Buffer.memcpy(bytes, 0, pNative, 0, nbytesused); pNative[nbytesused] = 0; *pLength = nbytesused; } return new IntPtr(pNative); } static internal unsafe string ConvertToManaged(IntPtr pNative, int cch) { if (IntPtr.Zero == pNative) { return null; } return new String((sbyte*)pNative, 0, cch); } static internal unsafe void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class AnsiBSTRMarshaler { static internal unsafe IntPtr ConvertToNative(int flags, string strManaged) { if (null == strManaged) { return IntPtr.Zero; } int length = strManaged.Length; StubHelpers.CheckStringLength(length); byte[] bytes = null; int nb = 0; if (length > 0) { bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8)); nb = bytes.Length; } // + 1 for the null character from the user. + 1 for the null character we put in. int cbNativeBuffer = (length + 2) * Marshal.SystemMaxDBCSCharSize + sizeof(int); // In the (fBestFit && fThrowOnUnmappableChar) case, we'll actually call ConvertToAnsi_BestFit_Throw() // to do the conversion. This method allocates the buffer according to the worst case scenario, just // like what we do here. Thus, the length of the returned array could be bigger than our estimate. // So we need to take the maximum of the two values here. cbNativeBuffer = Math.Max(cbNativeBuffer, nb + 2 + sizeof(int)); byte* pbNativeBuffer = ((byte*)Win32Native.CoTaskMemAlloc(cbNativeBuffer)) + sizeof(int); BCLDebug.Assert((nb + 2) <= cbNativeBuffer, "Underestimated buffer size"); Buffer.memcpy(bytes, 0, pbNativeBuffer, 0, nb); pbNativeBuffer[nb] = 0x00; pbNativeBuffer[nb+1] = 0x00; ((int*)pbNativeBuffer)[-1] = nb; return (IntPtr)pbNativeBuffer; } static internal unsafe string ConvertToManaged(IntPtr bstr) { if (IntPtr.Zero == bstr) return null; else return new String((sbyte*)bstr); } static internal unsafe void ClearNative(IntPtr pNative) { if (IntPtr.Zero != pNative) { Win32Native.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); } } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class WSTRBufferMarshaler { static internal IntPtr ConvertToNative(string strManaged) { BCLDebug.Assert(false, "NYI"); return IntPtr.Zero; } static internal unsafe string ConvertToManaged(IntPtr bstr) { BCLDebug.Assert(false, "NYI"); return null; } static internal void ClearNative(IntPtr pNative) { BCLDebug.Assert(false, "NYI"); } } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class ObjectMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToNative(object objSrc, IntPtr pDstVariant); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object ConvertToManaged(IntPtr pSrcVariant); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pVariant); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class ValueClassMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToNative(IntPtr dst, IntPtr src, IntPtr pMT, IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertToManaged(IntPtr dst, IntPtr src, IntPtr pMT); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr dst, IntPtr pMT); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class DateMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern double ConvertToNative(DateTime managedDate); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern DateTime ConvertToManaged(double nativeDate); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class InterfaceMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr ConvertToNative(object objSrc, IntPtr itfMT, IntPtr classMT, int flags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object ConvertToManaged(IntPtr pUnk, IntPtr itfMT, IntPtr classMT, int flags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pUnk); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdNativeArrayMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, int sizeParam, IntPtr pMT, int additive, int multiplier, int dwFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNativeContents(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdSafeArrayMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, IntPtr pMT, int iRank, int dwFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome, object pOriginalManaged); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertSpaceToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal static class MngdRefCustomMarshaler { [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateMarshaler(IntPtr pMarshalState, IntPtr pCMHelper); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ConvertContentsToManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearNative(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ClearManaged(IntPtr pMarshalState, ref object pManagedHome, IntPtr pNativeHome); } [StructLayout(LayoutKind.Sequential)] internal struct NativeVariant { ushort vt; ushort wReserved1; ushort wReserved2; ushort wReserved3; long data1; long data2; } // // This struct maps the the EE's Frame struct in vm\Frames.h // [StructLayout(LayoutKind.Sequential)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] internal unsafe struct EEFrame { internal void* __VFN_table; internal void* m_Next; internal const long OFFSETOF__Thread__m_pFrame = 0x10L; // // pass in an explicit 'this' pointer so that we don't have to // use 'fixed' to take the address of 'this' // internal static void Push(void* pThis, void* pThread) { EEFrame* pThisFrame = (EEFrame*)pThis; #if _DEBUG StubHelpers.Verify__EEFrame__Push(pThisFrame, pThread, OFFSETOF__Thread__m_pFrame); #endif // _DEBUG void** ppFrame = (void**)(((byte*)pThread) + OFFSETOF__Thread__m_pFrame); pThisFrame->m_Next = *ppFrame; *ppFrame = pThis; } // // uses an explicit 'this' pointer to be consistent with Push above // internal static void Pop(void* pThis, void* pThread) { EEFrame* pThisFrame = (EEFrame*)pThis; #if _DEBUG StubHelpers.Verify__EEFrame__Pop(pThisFrame, pThread, OFFSETOF__Thread__m_pFrame); #endif // _DEBUG void** ppFrame = (void**)(((byte*)pThread) + OFFSETOF__Thread__m_pFrame); *ppFrame = pThisFrame->m_Next; } } [StructLayoutAttribute(LayoutKind.Sequential)] internal struct CY { internal long int64; } [StructLayout(LayoutKind.Sequential)] internal struct DECIMAL { internal short wReserved; internal short signscale; internal int Hi32; internal long Lo64; } [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] [SuppressUnmanagedCodeSecurityAttribute()] internal static class StubHelpers { internal const String OLEAUT32 = "oleaut32.dll"; [DllImport(OLEAUT32)] internal extern static int VarCyFromDec(ref DECIMAL pdecIn, ref CY cyOut); [DllImport(OLEAUT32)] internal extern static int VarDecFromCy(CY cyIn, ref DECIMAL decOut); #if _DEBUG [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void Verify__EEFrame__Pop(EEFrame* pFrame, void* pThread, long managed_OFFSETOF__Thread__m_pFrame); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void Verify__EEFrame__Push(EEFrame* pFrame, void* pThread, long managed_OFFSETOF__Thread__m_pFrame); #endif // _DEBUG //-------------------------------------------------------- // PInvoke stub helpers //------------------------------------------------------- #if DEBUG [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern UIntPtr GetProcessGSCookie(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void FailFast(); #endif [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DoNDirectCall(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DoCLRToCOMCall(object thisPtr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void SetLastError(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginStandalone(IntPtr pFrame, IntPtr pNMD, int dwStubFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginStandaloneCleanup(IntPtr pFrame, IntPtr pNMD, int dwStubFlags); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginCLRToCOMStandalone(IntPtr pFrame, IntPtr pCPCMD, int dwStubFlags, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr BeginCLRToCOMStandaloneCleanup(IntPtr pFrame, IntPtr pCPCMD, int dwStubFlags, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ThrowDeferredException(); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ThrowInteropParamException(int resID, int paramIdx); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void CreateCleanupList(IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DestroyCleanupList(IntPtr pFrameMem, bool fExceptionThrown); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object GetHRExceptionObject(int hr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object GetCOMHRExceptionObject(int hr, IntPtr pFrame, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr CreateCustomMarshalerHelper(IntPtr pMD, int paramToken, IntPtr hndManagedType); //------------------------------------------------------- // AsAnyAMarshaler, AsAnyWMarshaler //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr MarshalAsAny(ref object pManagedHome, int dwFlags, IntPtr pvMarshaler, IntPtr pCleanupWorkList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void UnmarshalAsAny(ref object pManagedHome, IntPtr pvMarshaler); //-------------------------------------------------------- // SafeHandle Helper //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr SafeHandleC2NHelper(object pThis, IntPtr pCleanupWorkList); //-------------------------------------------------------- // Helper for the MDA ----OnRCWCleanup //-------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void StubRegisterRCW(object pThis, IntPtr pThread, IntPtr pCleanupWorkList); //------------------------------------------------------- // Profiler helpers //-------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr ProfilerBeginTransitionCallback(IntPtr pSecretParam, IntPtr pThread, object pThis); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void ProfilerEndTransitionCallback(IntPtr pMD, IntPtr pThread); //------------------------------------------------------- // Debugger helpers //------------------------------------------------------- [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void DebuggerTraceCall(IntPtr pSecretParam); //----------------------------------------------------- // misc //------------------------------------------------------ static internal void CheckStringLength(int length) { if (length > 0x7ffffff0) { throw new MarshalDirectiveException(Environment.GetResourceString("Marshaler_StringTooLong")); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern int strlen(sbyte* ptr); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern Decimal DecimalCanonicalizeInternal(Decimal dec); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void FmtClassUpdateNativeInternal(object obj, byte* pNative, IntPtr pOptionalCleanupList); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void FmtClassUpdateCLRInternal(object obj, byte* pNative); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal unsafe extern void LayoutDestroyNativeInternal(byte* pNative, IntPtr pMT); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern object AllocateInternal(IntPtr typeHandle); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern IntPtr MarshalToUnmanagedVaListInternal(IntPtr pArgIterator); [MethodImplAttribute(MethodImplOptions.InternalCall)] static internal extern void MarshalToManagedVaListInternal(IntPtr va_list, IntPtr pArgIterator); } #endif } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MatrixValueSerializer.cs
- InternalConfigRoot.cs
- ClientProxyGenerator.cs
- TableTextElementCollectionInternal.cs
- TextRenderingModeValidation.cs
- Configuration.cs
- Registry.cs
- HashAlgorithm.cs
- CustomErrorCollection.cs
- PointF.cs
- IndexedEnumerable.cs
- HttpInputStream.cs
- XsdBuildProvider.cs
- CellConstantDomain.cs
- DataTableReader.cs
- SqlDataSourceEnumerator.cs
- Image.cs
- _ListenerAsyncResult.cs
- login.cs
- DbConnectionPoolOptions.cs
- CompilationUnit.cs
- InvalidProgramException.cs
- AllMembershipCondition.cs
- DefaultValueConverter.cs
- OdbcConnectionString.cs
- SamlConditions.cs
- DictionaryItemsCollection.cs
- MeshGeometry3D.cs
- X509IssuerSerialKeyIdentifierClause.cs
- ConstructorNeedsTagAttribute.cs
- ItemsControlAutomationPeer.cs
- ExpressionBinding.cs
- UserControlFileEditor.cs
- EdgeModeValidation.cs
- RoutedEventHandlerInfo.cs
- HtmlContainerControl.cs
- ByteAnimation.cs
- VBCodeProvider.cs
- Queue.cs
- DbMetaDataCollectionNames.cs
- WebResourceUtil.cs
- Type.cs
- XmlDeclaration.cs
- CngAlgorithmGroup.cs
- ElementHostAutomationPeer.cs
- SchemaNames.cs
- XPathParser.cs
- FieldReference.cs
- FigureParaClient.cs
- CompositeDataBoundControl.cs
- HwndHost.cs
- PanelDesigner.cs
- EventPropertyMap.cs
- SiteMapNodeItem.cs
- ResXResourceSet.cs
- PersonalizationProvider.cs
- DataGridCommandEventArgs.cs
- KeyInstance.cs
- RowParagraph.cs
- ProxyWebPartConnectionCollection.cs
- HttpCapabilitiesEvaluator.cs
- UpdateCompiler.cs
- ModuleBuilder.cs
- autovalidator.cs
- LookupBindingPropertiesAttribute.cs
- ExceptionHandlerDesigner.cs
- CodeComment.cs
- PropertyTabAttribute.cs
- XhtmlTextWriter.cs
- RegexNode.cs
- xmlglyphRunInfo.cs
- TracePayload.cs
- LightweightEntityWrapper.cs
- ImageListUtils.cs
- TaskResultSetter.cs
- PriorityChain.cs
- Bitmap.cs
- NamedObject.cs
- OptionUsage.cs
- PolyQuadraticBezierSegment.cs
- Selector.cs
- IdentitySection.cs
- GridViewRowCollection.cs
- SHA1.cs
- PersonalizationStateInfoCollection.cs
- CodeSnippetTypeMember.cs
- CodeTypeParameterCollection.cs
- ProviderException.cs
- FtpRequestCacheValidator.cs
- Wildcard.cs
- PassportAuthenticationModule.cs
- DefaultPrintController.cs
- AssociatedControlConverter.cs
- CanExecuteRoutedEventArgs.cs
- XsdBuildProvider.cs
- EFTableProvider.cs
- ResourceContainer.cs
- FrameworkReadOnlyPropertyMetadata.cs
- Property.cs
- ProfileInfo.cs