什么是正确的PInvoke签名的函数,它变参?函数、正确、PInvoke

由网友(莂碰、莪侽亽)分享简介:还有一个原生的功能:int sqlite3_config(int, ...);我想的PInvoke这个功能。目前,我有这样的声明:I would like to PInvoke to this function. Currently, I have this declaration:[DllImport("sql...

还有一个原生的功能:

int sqlite3_config(int, ...);

我想的PInvoke这个功能。目前,我有这样的声明:

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

(结果和ConfigOption的形式是枚举枚举结果:INT {...}

其实我只关心这个函数的一个参数的版本,不需要其他的args。这是正确的?

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

我也很好奇,你将如何声明两个参数形式(也许需要2 IntPtrs?)。

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

推荐答案

您需要使用__arglist关键字(这是无证),巴特#有一个的好看一下博客。

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

示例

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

该是pinvoking可变参数方法没有标准的方法,大部分的解决方案将包装在几种方法,例如

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);
阅读全文

相关推荐

最新文章