C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
来源:互联网 发布:什么软件小说免费 编辑:程序博客网 时间:2024/06/08 16:19
C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
1. 如果函数只有传入参数,比如:
C/C++ Code Copy Code To Clipboard
- //C++中的输出函数
- int__declspec(dllexport) test(constint N)
- {
- return N+10;
- }
对应的C#代码为:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- publicstaticexternint test(int m);
- privatevoid button1_Click(object sender, EventArgs e)
- {
- textBox1.Text= test(10).ToString();
- }
2. 如果函数有传出参数,比如:
C/C++ Code Copy Code To Clipboard
- //C++
- void__declspec(dllexport) test(constint N, int& Z)
- {
- Z=N+10;
- }
对应的C#代码:
C# Code Copy Code To Clipboard
- [DllImport("test.dll", EntryPoint = "#1")]
- publicstaticexterndouble test(int m, refint n);
- privatevoid 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(constint N, constint 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")]
- publicstaticexterndouble test(int N, int[] n, refint Z);
- privatevoid button1_Click(object sender, EventArgs e)
- {
- int N = 0;
- int[] n;
- n = newint[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(constint M, constint 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")]
- publicstaticexternvoid test(int N, int[] n, [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] int[] Z);
- privatevoid button1_Click(object sender, EventArgs e)
- {
- int N = 1000;
- int[] n, Z;
- n = newint[N];Z = newint[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
0 0
- 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函数各种参数传递问题
- C#调用C++编写的DLL函数各种参数传递问题
- C#调用C++dll方法,char*类型之间的传递
- 一次python对dll的调用,传递string类型的参数作为调用函数的参数
- C#调用C函数(DLL)传递参数问题
- C#调用C函数(DLL)传递参数问题
- 开源UI
- Linux---命令
- Android GridView自定义Adapter
- 模拟Http登录
- android支付宝支付功能集成
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
- 安装Dubbo管理控制台
- Java线程安全和非线程安全
- Linux内部的时钟处理机制全面剖析
- Java之访问子类对象的实例变量
- git使用基本法则
- ImageLoader配置
- Hive性能优化
- 强大的vim配置文件