/// <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>SafeCoTaskMemHandle object to an native (unmanaged) memory block the size of T.</returns>
        public static SafeCoTaskMemHandle AllocCoTaskMem <T>(T value = default(T)) where T : struct
        {
            Debug.Assert(typeof(T).StructLayoutAttribute?.Value == LayoutKind.Sequential);
            var ret = new SafeCoTaskMemHandle(Marshal.SizeOf(typeof(T)));

            Marshal.StructureToPtr(value, ret.handle, false);
            return(ret);
        }
        /// <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>SafeCoTaskMemHandle object to an native (unmanaged) structure with a trail array of structures</returns>
        public static SafeCoTaskMemHandle AllocCoTaskMem <T>(IEnumerable <T> values, int count, int prefixBytes = 0) where T : struct
        {
            Debug.Assert(typeof(T).StructLayoutAttribute?.Value == LayoutKind.Sequential);
            var sz     = Marshal.SizeOf(typeof(T));
            var result = new SafeCoTaskMemHandle(prefixBytes + sz * count);
            var ptr    = new IntPtr(result.handle.ToInt64() + prefixBytes);

            foreach (var value in values)
            {
                result.StructureToPtr(value, ptr, false);
                ptr = new IntPtr(ptr.ToInt64() + sz);
            }
            return(result);
        }
        /// <summary>Allocates unmanaged memory to represent an array of pointers to strings that are then packed in memory behind the array.</summary>
        /// <param name="values">The list of strings.</param>
        /// <param name="charSet">The character set.</param>
        /// <param name="prefixBytes">Number of bytes preceding the trailing array of structures</param>
        /// <returns>SafeCoTaskMemHandle object to a native (unmanaged) array of string pointers with trailing strings.</returns>
        public static SafeCoTaskMemHandle AllocHGlobal(IEnumerable <string> values, CharSet charSet = CharSet.Auto, int prefixBytes = 0)
        {
            var coll   = values as IList <string> ?? new List <string>(values);
            var chSz   = charSet == CharSet.Unicode ? 2 : (charSet == CharSet.Auto ? Marshal.SystemDefaultCharSize : 1);
            var sz     = coll.Sum(s => ((s?.Length + 1) * chSz ?? 0) + IntPtr.Size);
            var result = new SafeCoTaskMemHandle(prefixBytes + sz);
            var ptr    = new IntPtr(result.handle.ToInt64() + prefixBytes);
            var enc    = chSz == 1 ? System.Text.Encoding.ASCII : System.Text.Encoding.Unicode;
            var sptr   = new IntPtr(ptr.ToInt64() + IntPtr.Size * coll.Count);
            var iptr   = ptr;

            foreach (var s in coll)
            {
                var bytes = enc.GetByteCount(s);
                Marshal.Copy(enc.GetBytes(s), 0, sptr, bytes);
                Marshal.WriteIntPtr(iptr, sptr);
                iptr = new IntPtr(iptr.ToInt64() + IntPtr.Size);
                sptr = new IntPtr(sptr.ToInt64() + bytes);
            }
            return(result);
        }
Beispiel #4
0
 public static extern HRESULT PropVariantToUInt64VectorAlloc([In] Ole32.PROPVARIANT propVar, out SafeCoTaskMemHandle pprgf, out uint pcElem);
Beispiel #5
0
 public static extern HRESULT PropVariantToBuffer([In] Ole32.PROPVARIANT propVar, [In, Out] SafeCoTaskMemHandle pv, uint cb);
Beispiel #6
0
 /// <summary>Initializes a new instance of the <see cref="SNB"/> class.</summary>
 /// <param name="p">The native pointer.</param>
 private SNB(IntPtr p)
 {
     ptr = new SafeCoTaskMemHandle(p, 0, true);
 }
Beispiel #7
0
 /// <summary>Initializes a new instance of the <see cref="SNB"/> class.</summary>
 /// <param name="names">The list of names to associate with this instance.</param>
 public SNB(IEnumerable <string> names)
 {
     ptr = names == null ? SafeCoTaskMemHandle.Null : SafeCoTaskMemHandle.CreateFromStringList(names, StringListPackMethod.Packed, CharSet.Unicode);
 }
Beispiel #8
0
 public static extern HRESULT PropVariantToFileTimeVectorAlloc([In] PROPVARIANT propVar, out SafeCoTaskMemHandle pprgf, out uint pcElem);