示例#1
0
        public static IntPtr ClassArrayToPtr <ClassType, InteropStructType>(
            ClassType[] inputTypes,
            Func <ClassType, DisposableCollection, InteropStructType> converter,
            DisposableCollection disposableCollection,
            out SizeT arrayCount)
        {
            if (inputTypes == null)
            {
                arrayCount = new SizeT(0);
                return(IntPtr.Zero);
            }

            bool             isEnum       = typeof(InteropStructType).IsEnum;
            Int32            sizeOfStruct = Marshal.SizeOf(isEnum ? Enum.GetUnderlyingType(typeof(InteropStructType)) : typeof(InteropStructType));
            DisposableBuffer buffer       = disposableCollection.Add(new DisposableBuffer(checked (sizeOfStruct * inputTypes.Length)));

            IntPtr currentPtr = buffer.IntPtr;

            foreach (ClassType inputType in inputTypes)
            {
                object structure = isEnum ? Convert.ChangeType(converter(inputType, disposableCollection), Enum.GetUnderlyingType(typeof(InteropStructType))) : (object)converter(inputType, disposableCollection);
                Marshal.StructureToPtr(structure, currentPtr, fDeleteOld: false);
                currentPtr = currentPtr.OffsetPF(sizeOfStruct);
            }

            arrayCount = new SizeT(inputTypes.Length);
            return(buffer.IntPtr);
        }
示例#2
0
 /// <summary>
 /// Constructor for marshaling a UTF8 char* _to_ managed code.  Requires an existing DisposableCollection to add a buffer to.
 /// </summary>
 public UTF8StringPtr(string str, DisposableCollection disposableCollection)
 {
     if (str == null)
     {
         this.pointer = IntPtr.Zero;
     }
     else
     {
         byte[]           utf8Bytes = Converters.StringToNullTerminatedUTF8ByteArray(str);
         DisposableBuffer buffer    = new DisposableBuffer(utf8Bytes.Length);
         Marshal.Copy(source: utf8Bytes, startIndex: 0, destination: buffer.IntPtr, length: utf8Bytes.Length);
         disposableCollection.Add(buffer);
         this.pointer = buffer.IntPtr;
     }
 }