Wednesday, November 19, 2008

How to P/Invoke VarArgs (variable arguments) in C#? ... or hidden junk in CLR

Recently I wrote a cheat sheet for pinvoking in .NET. Shortly after I got a question in comments about how to deal with variable arguments, when it's more, then one parameter. Also what to do if those arguments are heterogeneous?

Let's say, that we have following method in C:

int VarSum(int nargs, ...){
    va_list argp;
    va_start( argp, nargs );
    int sum = 0;
    for( int i = 0 ; i < nargs; i++ ) {
        int arg = va_arg( argp, int );
        sum += arg;
    }
    va_end( argp );

    return sum;
}

We can expose this method to C# as following:

[System.Runtime.InteropServices.DllImportAttribute("unmanaged.dll", EntryPoint = "VarSum")]
        public static extern int VarSum(int nargs,int arg1);

[System.Runtime.InteropServices.DllImportAttribute("unmanaged.dll", EntryPoint = "VarSum")]
        public static extern int VarSum(int nargs,int arg1,int arg2);

[System.Runtime.InteropServices.DllImportAttribute("unmanaged.dll", EntryPoint = "VarSum")]
        public static extern int VarSum(int nargs,int arg1,int arg2,int arg3);

etc...

And it will work. However, if you'll try to expose it as int array, marshaller will fail to understand how to align things

[System.Runtime.InteropServices.DllImportAttribute("unmanaged.dll", EntryPoint = "VarSum")]
        public static extern int VarSum(int nargs,int[] arg);

This in spite of the fact, that this method will work properly with another signature

int ArrSum(int* nargs) {
    int sum = 0;
    for( int i = 0 ; i < 2; i++ ) {
        sum += nargs[i];
    }
    return sum;
}

So what to do? The official answer is - you have nothing to do, rather then override all possibilities. This is very bad and absolutely not flexible. So, there is small class in C#, named ArgIterator. This one is similar to params object[], but knows to marshal into varargs. The problem is, that you have no way to add things inside. It's "kind-of-read-only".

Let's look into reflected version of ArgIterator. We'll see there something, named __arglist and __refvalue. OMG, isn't it good old stuff similar to "__declspec(dllexport) int _stdcall" etc.? It is! But can we use it in C#? We can! Just sign your method as Cdecl and you have working signature for "..."

[System.Runtime.InteropServices.DllImportAttribute("unmanaged.dll", EntryPoint = "VarSum",
            CallingConvention=System.Runtime.InteropServices.CallingConvention.Cdecl)]
        public static extern int VarSum(int nargs, __arglist);

Yes, looks strange, and absolutely not CLR compliant. However, this is the only way to expose varargs to CLR via P/Invoke. How to use it? Simple:

c = VarSum(2, __arglist(5, 10));

Have a nice day and be good people. Also, my question to Microsoft is why this stuff is not in MSDN and we, as developers, have no way to get rid of it.

Is not it very good practices to use non-compliant methods? Give us another way to do it!
Is not it very good practices to use variable arguments in unmanaged method signatures? So why you want dynamic types in C# 4?

Source code for this article

No comments: