C#调用C++编写的DLL函数, 以及各种类型的参数传递
来源:互联网 发布:深度优化dnf客户端方法 编辑:程序博客网 时间:2024/05/27 16:42
1. 如果函数只有传入参数,比如:
C/C++ Code Copy Code To Clipboard
- //C++中的输出函数
- int __declspec(dllexport) test(const int N)
- {
- return N+10;
- }
对应的C#代码为:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- public static extern int test(int m);
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Text= test(10).ToString();
- }
2. 如果函数有传出参数,比如:
C/C++ Code Copy Code To Clipboard
- //C++
- void __declspec(dllexport) test(const int N, int& Z)
- {
- Z=N+10;
- }
对应的C#代码:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- public static extern double test(int m, ref int n);
- private void button1_Click(object sender, EventArgs e)
- {
- int N = 0;
- test1(10, ref N);
- textBox1.Text= N.ToString();
- }
3. 带传入数组:
C/C++ Code Copy Code To Clipboard
- void __declspec(dllexport) test(const int N, const int n[], int& Z)
- {
- for (int i=0; i<N; i++)
- {
- Z+=n[i];
- }
- }
C#代码:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- public static extern double test(int N, int[] n, ref int Z);
- private void button1_Click(object sender, EventArgs e)
- {
- int N = 0;
- int[] n;
- n = new int[10];
- for (int i = 0; i < 10; i++)
- {
- n[i] = i;
- }
- test(n.Length, n, ref N);
- textBox1.Text= N.ToString();
- }
4. 带传出数组:
C++不能直接传出数组,只传出数组指针,
C/C++ Code Copy Code To Clipboard
- void __declspec(dllexport) test(const int M, const int n[], int *N)
- {
- for (int i=0; i<M; i++)
- {
- N[i]=n[i]+10;
- }
- }
对应的C#代码:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- public static extern void test(int N, int[] n, [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] int[] Z);
- private void button1_Click(object sender, EventArgs e)
- {
- int N = 1000;
- int[] n, Z;
- n = new int[N];Z = new int[N];
- for (int i = 0; i < N; i++)
- {
- n[i] = i;
- }
- test(n.Length, n, Z);
- for (int i=0; i<Z.Length; i++)
- {
- textBox1.AppendText(Z[i].ToString()+"n");
- }
- }
这里声明函数入口时,注意这句 [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] int[] Z
在C#中数组是直接使用的,而在C++中返回的是数组的指针,这句用来转化这两种不同的类型.
关于MarshalAs的参数用法以及数组的Marshaling,可以参见这篇转帖的文章: http://www.kycis.com/blog/read.php?21
5. 传出字符数组:
C++定义:
C/C++ Code Copy Code To Clipboard
- void __declspec(dllexport) test(int i, double &a, double &b, char t[5])
C#对应声明:
C# Code Copy Code To Clipboard
- [DllImport("dll.dll", EntryPoint = "test")]
- public static extern void test(int i, ref double a, ref double b, [Out, MarshalAs(UnmanagedType.LPArray)] char[] t);
- 。。。
- char[] t = new char[5];
- test(i, ref a, ref b, t);
字符数组的传递基本与4相似,只是mashalAs 时前面加上Out。
原文:http://www.cnblogs.com/rwzhou/p/5961095.html
另外有一篇很不错的博客,推荐:C#调用C++编写的DLL函数各种参数传递问题
阅读全文
0 0
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 不指定
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++dll方法,char*类型之间的传递
- 一次python对dll的调用,传递string类型的参数作为调用函数的参数
- C#调用C函数(DLL)传递参数问题
- C#调用C函数(DLL)传递参数问题
- 转一种连接方式
- Eclipse或者myeclipse的SVN提交过滤
- Linq中Orderby使用
- JavaScript基础知识及实例(一)
- 1624 取余最长路
- C#调用C++编写的DLL函数, 以及各种类型的参数传递
- [资料]ASP.NET C# 获取当前日期 时间 年 月 日 时 分 秒
- MVC 后台里的几个跳转
- MVC中ViewBag、ViewData和TempData 的区别与联系
- 添加Twitter Bootstrap 到MVC项目
- redis 查看配置信息
- 我为什么说 Python 是大数据全栈式开发语言
- [转载] MVC3自定义标签,给Html.ActionLink加上支持图片链接的功能
- [转载]Js小技巧||给input type=“password”的输入框赋默认值