/// <summary> /// Allocates from unmanaged memory to represent a structure with a variable length array at the end and marshal these structure /// elements. It is the callers responsibility to marshal what precedes the trailing array into the unmanaged memory. ONLY /// structures with attribute StructLayout of LayoutKind.Sequential are supported. /// </summary> /// <typeparam name="T">Type of the trailing array of structures</typeparam> /// <param name="values">Collection of structure objects</param> /// <param name="count"> /// Number of items in <paramref name="values"/>. Setting this value to -1 will cause the method to get the count by iterating /// through <paramref name="values"/>. /// </param> /// <param name="prefixBytes">Number of bytes preceding the trailing array of structures</param> /// <returns><see cref="SafeCoTaskMemHandle"/> object to an native (unmanaged) structure with a trail array of structures</returns> public static SafeCoTaskMemHandle CreateFromList <T>(IEnumerable <T> values, int count = -1, int prefixBytes = 0) => new SafeCoTaskMemHandle(InteropExtensions.MarshalToPtr(values, mm.AllocMem, out int s, prefixBytes), s);
/// <summary> /// Allocates from unmanaged memory to represent a structure with a variable length array at the end and marshal these structure /// elements. It is the callers responsibility to marshal what precedes the trailing array into the unmanaged memory. ONLY structures /// with attribute StructLayout of LayoutKind.Sequential are supported. /// </summary> /// <typeparam name="T">Type of the trailing array of structures</typeparam> /// <param name="values">Collection of structure objects</param> /// <param name="count">Number of items in <paramref name="values"/>.</param> /// <param name="prefixBytes">Number of bytes preceding the trailing array of structures</param> /// <returns><see cref="SafeLocalHandle"/> object to an native (unmanaged) structure with a trail array of structures</returns> public static SafeLocalHandle CreateFromList <T>(IEnumerable <T> values, int count = -1, int prefixBytes = 0) => new SafeLocalHandle(InteropExtensions.MarshalToPtr(values, new LocalMemoryMethods().AllocMem, out var s, prefixBytes), s);
/// <summary>Allocates from unmanaged memory sufficient memory to hold an object of type T.</summary> /// <typeparam name="T">Native type</typeparam> /// <param name="value">The value.</param> /// <returns><see cref="SafeCoTaskMemHandle"/> object to an native (unmanaged) memory block the size of T.</returns> public static SafeCoTaskMemHandle CreateFromStructure <T>(in T value = default) => new SafeCoTaskMemHandle(InteropExtensions.MarshalToPtr(value, mm.AllocMem, out int s), s);