Exemple #1
0
        /// <summary>
        /// Returns a <typeparamref name="T"/> array marshalled from the given <paramref name="memory"/>
        /// <see cref="UnmanagedMemoryHandle"/> handle.
        /// </summary>
        /// <typeparam name="T">Type of managed array you wish to convert to.</typeparam>
        /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> memory that contains array to marshal.</param>
        /// <returns>Marshalled array.</returns>
        public T[] ToArray <T>(UnmanagedMemoryHandle memory)
            where T : struct
        {
            Type type = typeof(T);

            if (type == typeof(byte))
            {
                return(ToByteArray(memory) as T[]);
            }
            if (type == typeof(short))
            {
                return(ToShortArray(memory) as T[]);
            }
            if (type == typeof(int))
            {
                return(ToIntArray(memory) as T[]);
            }
            if (type == typeof(float))
            {
                return(ToFloatArray(memory) as T[]);
            }
            if (type == typeof(double))
            {
                return(ToDoubleArray(memory) as T[]);
            }
            if (type == typeof(long))
            {
                return(ToLongArray(memory) as T[]);
            }

            throw new NotSupportedException(string.Format("No marshalling support for array of type {0}.", type));
        }
Exemple #2
0
        /// <summary>
        /// Marshal <see cref="UnmanagedMemoryHandle"/> to managed <see cref="System.Single"/> array.
        /// </summary>
        /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> containing pointer to native array.</param>
        /// <returns>Managed <see cref="System.Single"/> array.</returns>
        private static float[] ToFloatArray(UnmanagedMemoryHandle memory)
        {
            int length = CalculateArrayLength <float>(memory);
            var array  = new float[length];

            Marshal.Copy(memory, array, 0, length);
            return(array);
        }
Exemple #3
0
        /// <summary>
        /// Marshal a <see cref="UnmanagedMemoryHandle"/> to managed <see cref="System.Byte"/> array.
        /// </summary>
        /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> containing pointer to native array.</param>
        /// <returns>Managed <see cref="System.Byte"/> array.</returns>
        private static byte[] ToByteArray(UnmanagedMemoryHandle memory)
        {
            int length = CalculateArrayLength <byte>(memory);
            var array  = new byte[length];

            Marshal.Copy(memory, array, 0, length);
            return(array);
        }
Exemple #4
0
        /// <summary>
        /// Marshal <see cref="UnmanagedMemoryHandle"/> to managed <see cref="System.Int64"/> array.
        /// </summary>
        /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> containing pointer to native array.</param>
        /// <returns>Managed <see cref="System.Int64"/> array.</returns>
        private static long[] ToLongArray(UnmanagedMemoryHandle memory)
        {
            int length = CalculateArrayLength <long>(memory);
            var array  = new long[length];

            Marshal.Copy(memory, array, 0, length);
            return(array);
        }
        /// <summary>
        /// Explicitly disposes of the <paramref name="memory"/> object and deallocates its unmanaged memory.
        /// </summary>
        /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> to deallocate.</param>
        public void Deallocate(UnmanagedMemoryHandle memory)
        {
            if (memory == null)
            {
                return;
            }

            memory.Dispose();
        }
Exemple #6
0
 /// <summary>
 /// Determine length required for managed array based on <typeparamref name="T"/> and <paramref name="memory"/> size.
 /// </summary>
 /// <typeparam name="T">Underlying array type.</typeparam>
 /// <param name="memory"><see cref="UnmanagedMemoryHandle"/> to location of native array.</param>
 /// <returns>Length of marshalled array.</returns>
 private static int CalculateArrayLength <T>(UnmanagedMemoryHandle memory)
 {
     return(memory.Size / Marshal.SizeOf(typeof(T)));
 }
 /// <summary>
 /// Marshal a <see cref="UnmanagedMemoryHandle"/> object to a <typeparamref name="T"/> array.
 /// </summary>
 /// <typeparam name="T">Type of array to marshal from unmanaged memory.</typeparam>
 /// <param name="memory">Reference to <see cref="UnmanagedMemoryHandle"/>.</param>
 /// <returns>Copy of marshalled array now in managed memory.</returns>
 public T[] MemoryHandleToArray <T>(UnmanagedMemoryHandle memory)
     where T : struct
 {
     return(m_ArrayMarshaller.ToArray <T>(memory));
 }
 /// <summary>
 /// Marshal an <see cref="UnmanagedMemoryHandle"/> object to a <typeparamref name="T"/> structure.
 /// </summary>
 /// <typeparam name="T">Type of structure to marshal from unmanaged memory.</typeparam>
 /// <param name="memory">Reference to <see cref="UnmanagedMemoryHandle"/>.</param>
 /// <returns>Marshalled structure stored in managed memory.</returns>
 public T MemoryHandleTo <T>(UnmanagedMemoryHandle memory)
     where T : struct
 {
     return((T)Marshal.PtrToStructure(memory, typeof(T)));
 }
 /// <summary>
 /// Marshal a <see cref="UnmanagedMemoryHandle"/> object to an ANSI string.
 /// </summary>
 /// <param name="memory">Reference to <see cref="UnmanagedMemoryHandle"/>.</param>
 /// <returns>ANSI string conversion from unmanaged memory.</returns>
 public string MemoryHandleToString(UnmanagedMemoryHandle memory)
 {
     return(Marshal.PtrToStringAnsi(memory.Handle));
 }