Marshal va_list in C# delegate

后端 未结 4 554
孤城傲影
孤城傲影 2021-01-13 08:04

I\'m trying to make this work from c#:

C header:

typedef void (LogFunc) (const char *format, va_list args);
         


        
相关标签:
4条回答
  • 2021-01-13 08:41

    I understand there's also an "__arglist" keyword available in C#:

    • http://www.dotnetinterop.com/faq/?q=Vararg

    • http://bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx

    0 讨论(0)
  • 2021-01-13 08:44

    Just in case it helps someone, here's a solution for marshaling the arguments. The delegate is declared as:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl, SetLastError = true)] // Cdecl is a must
    internal delegate void LogFunc(string format, IntPtr argsAddress);
    

    The argsAddress is the unmanaged memory address where the array starts (I think). The format gives the size of the array. Knowing this I can create the managed array and fill it. Pseuso-code:

    size <- get size from format
    if size = 0 then return
    
    array <- new IntPtr[size]
    Marshal.Copy(argsAddress, array, 0, size);
    args <- new string[size]
    
    for i = 0 to size-1 do
       placeholder <- get the i-th placeholder from format // e.g. "%s"
       switch (placeholder)
           case "%s": args[i] <- Marshal.PtrToStringAnsi(array[i])
           case "%d": args[i] <- array[i].ToString() // i can't explain why the array contains the value, but it does
           default: throw exception("todo: handle {placeholder}")
    

    To tell the truth, I'm not sure how this works. It just seems to get the right data. I'm not claiming it is correct though.

    0 讨论(0)
  • 2021-01-13 08:45

    Another approach is to pass the va_list back to native code, something like calling vprintf in .net. I had the same issue, and I wanted it cross platform. So I wrote a sample project to demonstrate how it could work on several platforms.

    See https://github.com/jeremyVignelles/va-list-interop-demo

    The basic idea is :

    You declare your callback delegate:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    internal delegate void LogFunc(string format, IntPtr args);
    

    You pass your callback as you did:

    NativeMethods.Init(5, LogMessage);
    

    In the callback, you handle the specific cases of the different platforms. You need to understand how it works on each platform. From my testing and understanding, you can pass the IntPtr as-is to the vprintf* family of functions on Windows (x86,x64) and Linux x86, but on Linux x64, you will need to copy a structure for that to work.

    See my demo for more explanations.

    EDIT : We posted an issue on .net runtime's repository a while back, you can see it here https://github.com/dotnet/runtime/issues/9316 . Unfortunately, it didn't went far because we lacked a formal proposal.

    0 讨论(0)
  • 2021-01-13 08:46

    .NET can (to some extent) marshal between va_list and ArgIterator. You can try this:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl, SetLastError = true)]
    internal delegate void LogFunc(string format, ArgIterator args);
    

    I am not sure how the arguments are going to be passed (strings as pointers, probably). You may have some luck with ArgIterator.GetNextArgType. Eventually, you will probably have to parse the placeholders in the format string to get the argument types.

    0 讨论(0)
提交回复
热议问题