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

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

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

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!