开发者

What is the proper PInvoke signature for a function that takes var args?

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?).


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜