marshaling variable arguments - __arglist or alternative

喜欢而已 提交于 2019-12-09 03:44:25
Erti-Chris Eelmaa

Here is my suggestion how to tackle this. Take a look at varargs.h which is part of VisualStudio. This gives you some insight what the va_list means. You can see: typedef char * va_list;. It's just a pointer.

Not only is __arglist undocumented, I don't think it works correctly on 64-bit processes.

You need to build the va_list dynamically on C# side. I believe that this is better solution than undocumented __arglist and it seems to be working nicely. For C#, you want to use params[], and on C++ receiving side, va_list. Every variadic function should have function starting with v..., such as vsprintf, that receives va_list, instead of fiddling with arguments in the stack.

Copy/paste this beauty to your solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

// Author: Chris Eelmaa

namespace ConsoleApplication1
{
    #region VariableCombiner

    class CombinedVariables : IDisposable
    {
        readonly IntPtr _ptr;
        readonly IList<IDisposable> _disposables;

        bool _disposed;

        public CombinedVariables(VariableArgument[] args)
        {
            _disposables = new List<IDisposable>();

            _ptr = Marshal.AllocHGlobal(args.Sum(arg => arg.GetSize()));
            var curPtr = _ptr;

            foreach (var arg in args)
            {
                _disposables.Add(arg.Write(curPtr));
                curPtr += arg.GetSize();
            }
        }

        public IntPtr GetPtr()
        {
            if(_disposed)
                throw new InvalidOperationException("Disposed already.");

            return _ptr;
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;

                foreach (var disposable in _disposables)
                    disposable.Dispose();

                Marshal.FreeHGlobal(_ptr);
            }
        }
    }

    #endregion

    #region VariableArgument

    abstract class VariableArgument
    {
        #region SentinelDispose

        protected  static readonly IDisposable SentinelDisposable =
              new SentinelDispose();

        class SentinelDispose : IDisposable
        {
            public void Dispose()
            {

            }
        }

        #endregion

        public abstract IDisposable Write(IntPtr buffer);

        public virtual int GetSize()
        {
            return IntPtr.Size;
        }

        public static implicit operator VariableArgument(int input)
        {
            return new VariableIntegerArgument(input);
        }

        public static implicit operator VariableArgument(string input)
        {
            return new VariableStringArgument(input);
        }

        public static implicit operator VariableArgument(double input)
        {
            return new VariableDoubleArgument(input);
        }
    }

    #endregion

    #region VariableIntegerArgument

    sealed class VariableIntegerArgument : VariableArgument
    {
        readonly int _value;

        public VariableIntegerArgument(int value)
        {
            _value = value;
        }

        public override IDisposable Write(IntPtr buffer)
        {
            Marshal.Copy(new[] { _value }, 0, buffer, 1);
            return SentinelDisposable;
        }
    }

    #endregion

    #region VariableDoubleArgument

    sealed class VariableDoubleArgument : VariableArgument
    {
        readonly double _value;

        public VariableDoubleArgument(double value)
        {
            _value = value;
        }

        public override int GetSize()
        {
            return 8;
        }

        public override IDisposable Write(IntPtr buffer)
        {
            Marshal.Copy(new[] { _value }, 0, buffer, 1);
            return SentinelDisposable;
        }
    }

    #endregion

    #region VariableStringArgument

    sealed class VariableStringArgument : VariableArgument
    {
        readonly string _value;

        public VariableStringArgument(string value)
        {
            _value = value;
        }

        public override IDisposable Write(IntPtr buffer)
        {
            var ptr = Marshal.StringToHGlobalAnsi(_value);

            Marshal.Copy(new[] {ptr}, 0, buffer, 1);

            return new StringArgumentDisposable(ptr);
        }

        #region StringArgumentDisposable

        class StringArgumentDisposable : IDisposable
        {
            IntPtr _ptr;

            public StringArgumentDisposable(IntPtr ptr)
            {
                _ptr = ptr;
            }

            public void Dispose()
            {
                if (_ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(_ptr);
                    _ptr = IntPtr.Zero;
                }
            }
        }

        #endregion
    }
    #endregion
}

and the example of usage:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
              AmazingSPrintf("I am %s, %d years old, %f meters tall!",
                "Chris",
                24,
                1.94));
        }

        static string AmazingSPrintf(string format, params VariableArgument[] args)
        {
            if (!args.Any()) 
                return format;

            using (var combinedVariables = new CombinedVariables(args))
            {
                var bufferCapacity = _vscprintf(format, combinedVariables.GetPtr());
                var stringBuilder = new StringBuilder(bufferCapacity + 1);

                vsprintf(stringBuilder, format, combinedVariables.GetPtr());

                return stringBuilder.ToString();
            }
        }

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int vsprintf(
            StringBuilder buffer,
            string format,
            IntPtr ptr);

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        static extern int _vscprintf(
            string format,
            IntPtr ptr);

    }
}

The CombinedVariables class is used to build va_list, and then you can pass it to your C++ method void SomeFunction(LPCWSTR pszFormat, va_list args).

You need to take care of the VariableStringArgument as it works with ANSI currently. You're probably looking for Marshal.StringToHGlobalUni.

Note that there is a small difference between va_list and .... printf uses ..., while vprintf uses va_list. A va_list is often a pointer to the first element of the .... __arglist is for ....

For va_list you can use the code of @Erti, or my code:

public class VaList : IDisposable
{
    protected readonly List<GCHandle> handles = new List<GCHandle>();

    public VaList(bool unicode, params object[] args)
    {
        if (args == null)
        {
            throw new ArgumentNullException("args");
        }

        // The first handle is for the bytes array
        handles.Add(default(GCHandle));

        int total = 0;
        var bytes = new PrimitiveToBytes[args.Length];

        for (int i = 0; i < args.Length; i++)
        {
            int size = Convert(unicode, args[i], ref bytes[i]);

            bytes[i].Size = size;
            total += size;
        }

        // Instead of a byte[] we use a IntPtr[], so copying elements
        // inside is faster (perhaps :-) )
        var buffer = new IntPtr[total / IntPtr.Size];
        handles[0] = GCHandle.Alloc(buffer, GCHandleType.Pinned);

        for (int i = 0, j = 0; i < args.Length; i++)
        {
            buffer[j++] = bytes[i].IntPtr;

            // long or double with IntPtr == 4
            if (bytes[i].Size > IntPtr.Size)
            {
                buffer[j++] = (IntPtr)bytes[i].Int32High;
            }
        }
    }

    // Overload this to handle other types
    protected virtual int Convert(bool unicode, object arg, ref PrimitiveToBytes primitiveToBytes)
    {
        int size;

        if (arg == null)
        {
            primitiveToBytes.IntPtr = IntPtr.Zero;
            size = IntPtr.Size;
        }
        else
        {
            Type type = arg.GetType();

            TypeCode typeHandle = Type.GetTypeCode(type);

            switch (typeHandle)
            {
                case TypeCode.Boolean:
                    // Boolean converted to Int32
                    primitiveToBytes.Int32 = (bool)arg ? 1 : 0;
                    size = IntPtr.Size;
                    break;

                case TypeCode.SByte:
                    primitiveToBytes.SByte = (sbyte)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.Byte:
                    primitiveToBytes.Byte = (byte)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.Int16:
                    primitiveToBytes.Int16 = (short)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.UInt16:
                    primitiveToBytes.UInt16 = (ushort)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.Int32:
                    primitiveToBytes.Int32 = (int)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.UInt32:
                    primitiveToBytes.UInt32 = (uint)arg;
                    size = IntPtr.Size;
                    break;

                case TypeCode.Int64:
                    primitiveToBytes.Int64 = (long)arg;
                    size = sizeof(long);
                    break;

                case TypeCode.UInt64:
                    primitiveToBytes.UInt64 = (ulong)arg;
                    size = sizeof(ulong);
                    break;

                case TypeCode.Single:
                    // Single converted to Double
                    primitiveToBytes.Double = (double)(float)arg;
                    size = sizeof(double);
                    break;

                case TypeCode.Double:
                    primitiveToBytes.Double = (double)arg;
                    size = sizeof(double);
                    break;

                case TypeCode.Char:
                    if (unicode)
                    {
                        primitiveToBytes.UInt16 = (char)arg;
                    }
                    else
                    {
                        byte[] bytes = Encoding.Default.GetBytes(new[] { (char)arg });

                        if (bytes.Length > 0)
                        {
                            primitiveToBytes.B0 = bytes[0];

                            if (bytes.Length > 1)
                            {
                                primitiveToBytes.B1 = bytes[1];

                                if (bytes.Length > 2)
                                {
                                    primitiveToBytes.B2 = bytes[2];

                                    if (bytes.Length > 3)
                                    {
                                        primitiveToBytes.B3 = bytes[3];
                                    }
                                }
                            }
                        }
                    }

                    size = IntPtr.Size;
                    break;

                case TypeCode.String:
                    {
                        string str = (string)arg;

                        GCHandle handle;

                        if (unicode)
                        {
                            handle = GCHandle.Alloc(str, GCHandleType.Pinned);
                        }
                        else
                        {
                            byte[] bytes = new byte[Encoding.Default.GetByteCount(str) + 1];
                            Encoding.Default.GetBytes(str, 0, str.Length, bytes, 0);
                            handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
                        }

                        handles.Add(handle);
                        primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
                        size = IntPtr.Size;
                    }

                    break;

                case TypeCode.Object:
                    if (type == typeof(IntPtr))
                    {
                        primitiveToBytes.IntPtr = (IntPtr)arg;
                        size = IntPtr.Size;
                    }
                    else if (type == typeof(UIntPtr))
                    {
                        primitiveToBytes.UIntPtr = (UIntPtr)arg;
                        size = UIntPtr.Size;
                    }
                    else if (!type.IsValueType)
                    {
                        GCHandle handle = GCHandle.Alloc(arg, GCHandleType.Pinned);
                        primitiveToBytes.IntPtr = handle.AddrOfPinnedObject();
                        size = IntPtr.Size;
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }

                    break;

                default:
                    throw new NotSupportedException();
            }
        }

        return size;
    }

    ~VaList()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        for (int i = 0; i < handles.Count; i++)
        {
            if (handles[i].IsAllocated)
            {
                handles[i].Free();
            }
        }

        handles.Clear();
    }

    public IntPtr AddrOfPinnedObject()
    {
        if (handles.Count == 0)
        {
            throw new ObjectDisposedException(GetType().Name);
        }

        return handles[0].AddrOfPinnedObject();
    }

    [StructLayout(LayoutKind.Explicit)]
    protected struct PrimitiveToBytes
    {
        [FieldOffset(0)]
        public byte B0;
        [FieldOffset(1)]
        public byte B1;
        [FieldOffset(2)]
        public byte B2;
        [FieldOffset(3)]
        public byte B3;
        [FieldOffset(4)]
        public byte B4;
        [FieldOffset(5)]
        public byte B5;
        [FieldOffset(6)]
        public byte B6;
        [FieldOffset(7)]
        public byte B7;

        [FieldOffset(4)]
        public int Int32High;

        [FieldOffset(0)]
        public byte Byte;
        [FieldOffset(0)]
        public sbyte SByte;

        [FieldOffset(0)]
        public short Int16;
        [FieldOffset(0)]
        public ushort UInt16;

        [FieldOffset(0)]
        public int Int32;

        [FieldOffset(0)]
        public uint UInt32;

        [FieldOffset(0)]
        public long Int64;
        [FieldOffset(0)]
        public ulong UInt64;

        [FieldOffset(0)]
        public float Single;
        [FieldOffset(0)]
        public double Double;

        [FieldOffset(0)]
        public IntPtr IntPtr;
        [FieldOffset(0)]
        public UIntPtr UIntPtr;

        [FieldOffset(8)]
        public int Size;
    }
}

Example of use:

[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int vprintf(string format, IntPtr ptr);

and

using (var list = new VaList(false, // Ansi encoding
    true, // bool test
    short.MinValue + 1, int.MinValue + 2, long.MinValue + 3, // signed
    ushort.MaxValue - 4, uint.MaxValue - 5, ulong.MaxValue - 6, // unsigned
    float.MaxValue, double.MaxValue, // float/double
    'A', "Foo", Encoding.Default.GetBytes("Bar\0"), null, // char/string
    IntPtr.Size == sizeof(int) ? (IntPtr)(int.MinValue + 7) : (IntPtr)(long.MinValue + 7), // signed ptr
    UIntPtr.Size == sizeof(uint) ? (UIntPtr)(uint.MaxValue - 8) : (UIntPtr)(ulong.MaxValue - 8))) // unsigned ptr
{
    vprintf("%d\n %hd\n %d\n %lld\n %hu\n %u\n %llu\n %f\n %f\n %c\n %s\n %s\n %s\n %p\n %p\n", list.AddrOfPinnedObject());
}

Note that this code is compatible only with Visual C++ for Intel x86/x64. ARM uses another format, and GCC still other formats.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!