首页 > Windows开发 > 详细

C#调用dll中的可变参数函数

时间:2021-05-16 18:54:50      阅读:15      评论:0      收藏:0      [点我收藏+]

参考

教程:在同一个调试会话中调试 C# 和 C++
C# 中的可变参数方法(VarArgs)

背景

C/C++编写的函数可能用可变参数,在C++/C#混合编程时,如何调用在 dll 中的这样的函数呢?
可以通过使用 __arglist 这个不常见的关键字来进行可变参数函数的导入。

例子

C/C++ 中的函数形式

void axlog(unsigned int log_type, const char *format, ...);

C# 中的调用方法

方法1:使用__arglist

使用可变参数列表。

[DllImport("AxTraceDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void axlog(uint log_type, string message, __arglist);
...
axlog(4, "Hello %s.", __arglist("world"));

方法2:不使用__arglist

这样就不能使用可变参数列表了。

[DllImport("AxTraceDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void axlog(uint log_type, string message);
...
axlog(4, "Hello world.");

总结

  • 使用 __arglist 关键字:带可变参数列表
  • 不使用 __arglist 关键字:不带可变参数列表
  • 调用约定必须为 CallingConvention.Cdecl

C#调用dll中的可变参数函数

原文:https://www.cnblogs.com/octoberkey/p/14774059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!