Marshals strings to and from unmanaged code.
Inheritance: ICustomMarshaler
Ejemplo n.º 1
0
        /// <summary>
        /// Marshals the given pointer to a string.
        /// </summary>
        /// <param name="pNativeData">The pointer to the data to marshal to a string.</param>
        /// <param name="p_intSize">The length of the array to marshal.</param>
        /// <returns>The marshaled string.</returns>
        public string[] MarshalNativeToManaged(IntPtr pNativeData, Int32 p_intSize)
        {
            if (pNativeData == IntPtr.Zero)
            {
                return(null);
            }

            string[] strStrings = new string[p_intSize];
            for (Int32 i = 0; i < p_intSize; i++)
            {
                IntPtr      ptrString = Marshal.ReadIntPtr(pNativeData, i * IntPtr.Size);
                List <byte> lstString = StringMarshaler.GetStringBytes(ptrString, m_encEncoding);
                strStrings[i] = m_encEncoding.GetString(lstString.ToArray(), 0, lstString.Count);
            }
            return(strStrings);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Marshals the given string to a pointer.
 /// </summary>
 /// <param name="ManagedObj">The string to marshal.</param>
 /// <returns>A pointer to the marshalled string.</returns>
 public IntPtr MarshalManagedToNative(string[] ManagedObj)
 {
     string[] strStrings = (string[])ManagedObj;
     if (strStrings == null)
     {
         m_ptrStringArray = IntPtr.Zero;
     }
     else
     {
         m_intArraySize = strStrings.Length;
         IntPtr[] ptrStrings = new IntPtr[m_intArraySize];
         for (Int32 i = 0; i < m_intArraySize; i++)
         {
             ptrStrings[i] = StringMarshaler.GetNullTerminatedStringPointer(strStrings[i], m_encEncoding);
         }
         m_ptrStringArray = Marshal.AllocHGlobal(m_intArraySize * IntPtr.Size);
         Marshal.Copy(ptrStrings, 0, m_ptrStringArray, m_intArraySize);
     }
     return(m_ptrStringArray);
 }