What is the correct PInvoke signature for a function that accepts var args?

There is a built-in function:

int sqlite3_config(int, ...); 

I would like PInvoke for this feature. I currently have this ad:

 [DllImport("sqlite3", EntryPoint = "sqlite3_config")] public static extern Result Config (ConfigOption option); 

(Result and ConfigOption are enumerations of the form enum Result : int { ... } .)

In fact, I am only interested in the one-parameter version of this function and no other arguments are needed. Is it correct?

I am also curious how you declare two forms of the argument (maybe it will take 2 IntPtrs?).

+7
interop pinvoke
source share
1 answer

You need to use the __arglist keyword (which is undocumented), Bart # had a nice blog about it.

Example

 class Program { [DllImport("user32.dll")] static extern int wsprintf([Out] StringBuilder lpOut, string lpFmt, __arglist); static void Main(String[] args) { var sb = new StringBuilder(); wsprintf(sb, "%s %s %s", __arglist("1", "2", "3")); Console.Write(sb.ToString()); } } 

This is not a standard way to dump vararg methods; most solutions will wrap it in several ways, for example.

 [DllImport("MyDll", CallingConvention=CallingConvention.Cdecl)] static extern var MyVarArgMethods1(String fmt, String arg1); [DllImport("MyDll", CallingConvention=CallingConvention.Cdecl)] static extern var MyVarArgMethods2(String fmt, String arg1, String arg2); 
+8
source share

All Articles