Beispiel #1
0
        internal static unsafe byte *StringToUTF8String(string str)
        {
            if (str == null)
            {
                return(null);

                fixed(char *charsPtr = str)
                {
                    int   length   = Encoding.UTF8.GetByteCount(str) + 1;
                    byte *bytesPtr = (byte *)PInvokeMarshal.CoTaskMemAlloc((System.UIntPtr)length);
                    int   bytes    = Encoding.UTF8.GetBytes(charsPtr, str.Length, bytesPtr, length);

                    Debug.Assert(bytes + 1 == length);
                    bytesPtr[length - 1] = 0;
                    return(bytesPtr);
                }
        }
Beispiel #2
0
        internal static unsafe byte *StringToAnsiBstrBuffer(string s)
        {
            if (s is null)
            {
                return((byte *)IntPtr.Zero);
            }

            int stringLength = s.Length;

            fixed(char *pStr = s)
            {
                int   nativeLength = PInvokeMarshal.GetByteCount(pStr, stringLength);
                byte *bstr         = (byte *)Marshal.AllocBSTRByteLen((uint)nativeLength);

                PInvokeMarshal.ConvertWideCharToMultiByte(pStr, stringLength, bstr, nativeLength, bestFit: false, throwOnUnmappableChar: false);
                return(bstr);
            }
        }
Beispiel #3
0
        internal static unsafe void StringToByValAnsiString(string str, byte *pNative, int charCount, bool bestFit, bool throwOnUnmappableChar)
        {
            if (str != null)
            {
                // Truncate the string if it is larger than specified by SizeConst
                int lenUnicode = str.Length;
                if (lenUnicode >= charCount)
                {
                    lenUnicode = charCount - 1;

                    fixed(char *pManaged = str)
                    {
                        PInvokeMarshal.StringToAnsiString(pManaged, lenUnicode, pNative, /*terminateWithNull=*/ true, bestFit, throwOnUnmappableChar);
                    }
            }
            else
            {
                (*pNative) = (byte)'\0';
            }
        }
Beispiel #4
0
        internal static unsafe char *StringToUnicodeBuffer(string str)
        {
            if (str == null)
            {
                return(null);
            }

            int stringLength = str.Length;

            char *buffer = (char *)PInvokeMarshal.CoTaskMemAlloc((UIntPtr)(sizeof(char) * (stringLength + 1))).ToPointer();

            fixed(char *pStr = str)
            {
                int size = stringLength * sizeof(char);

                Buffer.MemoryCopy(pStr, buffer, size, size);
                *(buffer + stringLength) = '\0';
            }

            return(buffer);
        }
Beispiel #5
0
 // Write the given byte array to the logical buffer in a thread-safe manner
 private void ThreadSafeWriteBytes(byte[] src)
 {
     lock (Instance)
     {
         IntPtr dst;
         int    requiredSize  = src.Length;
         int    availableSize = GetPhysicalBuffer(requiredSize, out dst);
         if (availableSize < requiredSize)
         {
             // if current physical buffer doesn't have enough space, try
             // and allocate a new one
             availableSize = GetPhysicalBuffer(requiredSize, out dst);
             if (availableSize < requiredSize)
             {
                 throw new OutOfMemoryException();
             }
         }
         PInvokeMarshal.CopyToNative(src, 0, dst, src.Length);
         UpdatePhysicalBufferUsedSize();    // make sure that used physical buffer size is updated
     }
 }
Beispiel #6
0
        internal static unsafe byte *StringToAnsi(String str)
        {
            if (str == null)
            {
                return(null);
            }

            // CORERT-TODO: Use same encoding as the rest of the interop
            var encoding = Encoding.UTF8;

            fixed(char *pStr = str)
            {
                int   stringLength = str.Length;
                int   bufferLength = encoding.GetByteCount(pStr, stringLength);
                byte *buffer       = (byte *)PInvokeMarshal.CoTaskMemAlloc((UIntPtr)(void *)(bufferLength + 1)).ToPointer();

                encoding.GetBytes(pStr, stringLength, buffer, bufferLength);
                *(buffer + bufferLength) = 0;
                return(buffer);
            }
        }
Beispiel #7
0
            /// <summary>
            /// Serializes the exception metadata and SerializedExceptionData
            /// </summary>
            public unsafe byte[] Serialize()
            {
                checked
                {
                    byte[] serializedData = new byte[sizeof(ExceptionMetadataStruct) + SerializedExceptionData.Length];
                    fixed(byte *pSerializedData = &serializedData[0])
                    {
                        ExceptionMetadataStruct *pMetadata = (ExceptionMetadataStruct *)pSerializedData;

                        pMetadata->ExceptionId      = ExceptionMetadata.ExceptionId;
                        pMetadata->InnerExceptionId = ExceptionMetadata.InnerExceptionId;
                        pMetadata->ThreadId         = ExceptionMetadata.ThreadId;
                        pMetadata->NestingLevel     = ExceptionMetadata.NestingLevel;
                        pMetadata->ExceptionCCWPtr  = ExceptionMetadata.ExceptionCCWPtr;

                        PInvokeMarshal.CopyToNative(SerializedExceptionData, 0, (IntPtr)(pSerializedData + sizeof(ExceptionMetadataStruct)), SerializedExceptionData.Length);
                    }

                    return(serializedData);
                }
            }
        internal unsafe IntPtr MarshalToBSTR()
        {
            int    length    = _decryptedLength;
            IntPtr ptr       = IntPtr.Zero;
            IntPtr result    = IntPtr.Zero;
            byte * bufferPtr = null;

            UnprotectMemory();
            try
            {
                _buffer.AcquirePointer(ref bufferPtr);
                int resultByteLength = (length + 1) * sizeof(char);

                ptr = PInvokeMarshal.AllocBSTR(length);

                Buffer.MemoryCopy(bufferPtr, (byte *)ptr, resultByteLength, length * sizeof(char));

                result = ptr;
            }
            finally
            {
                ProtectMemory();

                // If we failed for any reason, free the new buffer
                if (result == IntPtr.Zero && ptr != IntPtr.Zero)
                {
                    RuntimeImports.RhZeroMemory(ptr, (UIntPtr)(length * sizeof(char)));
                    PInvokeMarshal.FreeBSTR(ptr);
                }

                if (bufferPtr != null)
                {
                    _buffer.ReleasePointer();
                }
            }
            return(result);
        }
Beispiel #9
0
 public static unsafe IntPtr AllocCoTaskMem(int cb)
 {
     return(PInvokeMarshal.AllocCoTaskMem(cb));
 }
Beispiel #10
0
 public static void FreeCoTaskMem(IntPtr ptr)
 {
     PInvokeMarshal.FreeCoTaskMem(ptr);
 }
Beispiel #11
0
 public static void FreeHGlobal(IntPtr hglobal)
 {
     PInvokeMarshal.FreeHGlobal(hglobal);
 }
Beispiel #12
0
 internal static unsafe void StringToByValAnsiString(string str, byte *pNative, int charCount, bool bestFit, bool throwOnUnmappableChar)
 {
     // In CoreRT charCount = Min(SizeConst, str.Length). So we don't need to truncate again.
     PInvokeMarshal.StringToByValAnsiString(str, pNative, charCount, bestFit, throwOnUnmappableChar, truncate: false);
 }
Beispiel #13
0
 internal static unsafe byte *StringToAnsiString(string str, bool bestFit, bool throwOnUnmappableChar)
 {
     return(PInvokeMarshal.StringToAnsiString(str, bestFit, throwOnUnmappableChar));
 }
Beispiel #14
0
 /// <summary>
 /// Convert a single UNICODE wide char to a single ANSI byte.
 /// </summary>
 /// <param name="managedArray">single UNICODE wide char value</param>
 public static unsafe byte WideCharToAnsiChar(char managedValue, bool bestFit, bool throwOnUnmappableChar)
 {
     return(PInvokeMarshal.WideCharToAnsiChar(managedValue, bestFit, throwOnUnmappableChar));
 }
Beispiel #15
0
 public static unsafe void WideCharArrayToAnsiCharArray(char[] managedArray, byte *pNative, bool bestFit, bool throwOnUnmappableChar)
 {
     PInvokeMarshal.WideCharArrayToAnsiCharArray(managedArray, pNative, bestFit, throwOnUnmappableChar);
 }
Beispiel #16
0
 public static unsafe byte *AllocMemoryForAnsiStringBuilder(StringBuilder sb)
 {
     if (sb == null)
     {
         return(null);
     }
     return((byte *)CoTaskMemAllocAndZeroMemory(new IntPtr(checked ((sb.Capacity + 2) * PInvokeMarshal.GetSystemMaxDBCSCharSize()))));
 }
Beispiel #17
0
 /// <summary>
 /// Retrieve the corresponding P/invoke instance from the stub
 /// </summary>
 public static Delegate GetPInvokeDelegateForStub(IntPtr pStub, RuntimeTypeHandle delegateType)
 {
     return(PInvokeMarshal.GetPInvokeDelegateForStub(pStub, delegateType));
 }
Beispiel #18
0
 /// <summary>
 /// Returns the stub to the pinvoke marshalling stub
 /// </summary>
 public static IntPtr GetStubForPInvokeDelegate(Delegate del)
 {
     return(PInvokeMarshal.GetStubForPInvokeDelegate(del));
 }
Beispiel #19
0
 public static unsafe String PtrToStringAnsi(IntPtr ptr)
 {
     return(PInvokeMarshal.PtrToStringAnsi(ptr));
 }
Beispiel #20
0
 public static void Copy(IntPtr source, byte[] destination, int startIndex, int length)
 {
     PInvokeMarshal.CopyToManaged(source, destination, startIndex, length);
 }
Beispiel #21
0
 public static unsafe byte *AllocMemoryForAnsiCharArray(char[] chArray)
 {
     if (chArray == null)
     {
         return(null);
     }
     return((byte *)CoTaskMemAllocAndZeroMemory(new IntPtr(checked ((chArray.Length + 2) * PInvokeMarshal.GetSystemMaxDBCSCharSize()))));
 }
Beispiel #22
0
 internal unsafe static void CoTaskMemFree(void *p)
 {
     PInvokeMarshal.CoTaskMemFree((IntPtr)p);
 }
Beispiel #23
0
 /// <summary>
 /// Convert ANSI ByVal byte array to UNICODE wide char array, best fit
 /// </summary>
 /// <remarks>
 /// * This version works with array instead to string, it means that the len must be provided and there will be NO NULL to
 /// terminate the array.
 /// * The buffer to the UNICODE wide char array must be allocated by the caller.
 /// </remarks>
 /// <param name="pNative">Pointer to the ANSI byte array. Could NOT be null.</param>
 /// <param name="lenInBytes">Maximum buffer size.</param>
 /// <param name="managedArray">Wide char array that has already been allocated.</param>
 public static unsafe void AnsiCharArrayToWideCharArray(byte *pNative, char[] managedArray)
 {
     PInvokeMarshal.AnsiCharArrayToWideCharArray(pNative, managedArray);
 }
Beispiel #24
0
 public static IntPtr GetFunctionPointerForDelegate(Delegate del)
 {
     return(PInvokeMarshal.GetFunctionPointerForDelegate(del));
 }
Beispiel #25
0
 /// <summary>
 /// Convert a single ANSI byte value to a single UNICODE wide char value, best fit.
 /// </summary>
 /// <param name="nativeValue">Single ANSI byte value.</param>
 public static unsafe char AnsiCharToWideChar(byte nativeValue)
 {
     return(PInvokeMarshal.AnsiCharToWideChar(nativeValue));
 }
Beispiel #26
0
 public static Delegate GetDelegateForFunctionPointer(IntPtr ptr, RuntimeTypeHandle delegateType)
 {
     return(PInvokeMarshal.GetDelegateForFunctionPointer(ptr, delegateType));
 }
Beispiel #27
0
 public static unsafe string AnsiStringToString(byte *buffer)
 {
     return(PInvokeMarshal.AnsiStringToString(buffer));
 }
Beispiel #28
0
 /// <summary>
 /// Retrieves the function pointer for the current open static delegate that is being called
 /// </summary>
 public static IntPtr GetCurrentCalleeOpenStaticDelegateFunctionPointer()
 {
     return(PInvokeMarshal.GetCurrentCalleeOpenStaticDelegateFunctionPointer());
 }
Beispiel #29
0
 public static unsafe string ByValAnsiStringToString(byte *buffer, int length)
 {
     return(PInvokeMarshal.ByValAnsiStringToString(buffer, length));
 }
Beispiel #30
0
 /// <summary>
 /// Retrieves the current delegate that is being called
 /// </summary>
 public static T GetCurrentCalleeDelegate <T>() where T : class // constraint can't be System.Delegate
 {
     return(PInvokeMarshal.GetCurrentCalleeDelegate <T>());
 }