Search code examples
c#compact-frameworkpinvokevariadic

P/Invoking a function with a variadic signature


I have a C#.NET 2.0 CF application that imports a function from a native DLL with the following signature:

__declspec( dllexport ) void DLL_Foo( int count, ... );

My C# application P/Invokes that function as follows:

public sealed class MyObject
{
    public void Foo()
    {
        NativeMethods.DLL_Foo(2, __arglist("a","b")); 
    }

    internal static class NativeMethods
    {
        [DllImport("My.dll")]
        internal static extern void DLL_Foo(int count, __arglist);
    }
}

But, when I invoke MyObject.Foo, I get a System.MissingMethodException.

What do I need to change to make this work?

Thanks, PaulH


Edit: If I change the import definition to:

internal static extern void DLL_Foo(int count, [MarshalAs(UnmanagedType.LPWStr)]string a, [MarshalAs(UnmanagedType.LPWStr)]string b);

then, call:

NativeMethods.DLL_Foo(2, "a", "b"); 

It works with no problems, so it's something with my __arglist usage.


Solution

  • I am not sure (and I have never done it) if you can have params args in P/Invoke but you might give it a try.

    internal static extern void DLL_Foo(int count, params string[] args);