I'm trying to make this work from c#:
C header:
typedef void (LogFunc) (const char *format, va_list args);
bool Init(uint32 version, LogFunc *log)
C# implementation:
static class NativeMethods
{
[DllImport("My.dll", SetLastError = true)]
internal static extern bool Init(uint version, LogFunc log);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, SetLastError = true)]
internal delegate void LogFunc(string format, string[] args);
}
class Program
{
public static void Main(string[] args)
{
NativeMethods.Init(5, LogMessage);
Console.ReadLine();
}
private static void LogMessage(string format, string[] args)
{
Console.WriteLine("Format: {0}, args: {1}", format, DisplayArgs(args));
}
}
What happens here is that the call to NativeMethods.Init
calls back LogMessage
and passes data from unmanaged code as parameters. This works for most cases in which the arguments are strings. However, there is a call on which the format is:
Loaded plugin %s for version %d.
and the args contains only a string (the plugin name). They do not contain the version value, which makes sense since I used string[]
in the delegate declaration. Question is, how should I write the delegate to get both the string and the int?
I tried using object[] args
and got this exception:
An invalid VARIANT was detected during a conversion from an unmanaged VARIANT to a managed object. Passing invalid VARIANTs to the CLR can cause unexpected exceptions, corruption or data loss.
EDIT: I could change the delegate signature to this:
internal delegate void LogFunc(string format, IntPtr args);
I could parse the format and find out how many arguments to expect and of what type. E.g. for Loaded plugin %s for version %d. I would expect a string and an int. Is there a way to get these 2 out of that IntPtr?
.NET can (to some extent) marshal between
va_list
andArgIterator
. You can try this: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.Just in case it helps someone, here's a solution for marshaling the arguments. The delegate is declared as:
The
argsAddress
is the unmanaged memory address where the array starts (I think). Theformat
gives the size of the array. Knowing this I can create the managed array and fill it. Pseuso-code: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.
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
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:
You pass your callback as you did:
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.