views:

92

answers:

1

There is a native function:

int sqlite3_config(int, ...);

I would like to PInvoke to this function. Currently, I have this declaration:

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

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

I am actually only interested in the single parameter version of this function and don't need the other args. Is this correct?

I am also curious as to how you would declare the two argument form (perhaps it would take 2 IntPtrs?).

+4  A: 

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());
    }       
}

The is no standard way of pinvoking vararg methods, most solutions will wrap it in several methods e.g.

[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);
Shay Erlichmen
I read about this before. Why isn't it documented?
ChaosPandion
probably miss the deadline for the ECMA submission and now MS will not commit to that keyword until its part of the sepc.
Shay Erlichmen
That makes sense. I also can't help but wonder why they use `__arglist` rather than `arglist`.
ChaosPandion
I updated wsprintf on pinvoke.net to use __arglisthttp://pinvoke.net/default.aspx/user32/wsprintf.html
kenny
I have to remember to do that when I find errors. Gotta give back to the community somehow.
ChaosPandion
@ChaosPandion 10x for the example. +1 on one of your past answers...
Shay Erlichmen
Thanks, I don't normally do this but I figured it couldn't hurt.
ChaosPandion