C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)
C#调用C++编写的DLL函数, 以及各种类型的参数传递 ![]()
1. 如果函数只有传入参数,比如:
- //C++中的输出函数
- int __declspec(dllexport) test(const int N)
- {
- return N+10;
- }
对应的C#代码为:
- [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++
- void __declspec(dllexport) test(const int N, int& Z)
- {
- Z=N+10;
- }
对应的C#代码:
- [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. 带传入数组:
- void __declspec(dllexport) test(const int N, const int n[], int& Z)
- {
- for (int i=0; i<N; i++)
- {
- Z+=n[i];
- }
- }
C#代码:
- [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++不能直接传出数组,只传出数组指针,
- 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#代码:
- [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++定义:
- void __declspec(dllexport) test(int i, double &a, double &b, char t[5])
C#对应声明:
- [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。
C#调用C++编写的DLL函数, 以及各种类型的参数传递 (转载)的更多相关文章
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 z
1. 如果函数只有传入参数,比如: C/C++ Code Copy Code To Clipboard //C++中的输出函数 int__declspec(dllexport) test(consti ...
- C#动态调用C++编写的DLL函数
C#动态调用C++编写的DLL函数 动态加载DLL需要使用Windows API函数:LoadLibrary.GetProcAddress以及FreeLibrary.我们可以使用DllImport在C ...
- 使用clr 调用C#编写的dll中的方法的全解释
使用clr 调用C#编写的dll中的方法的全解释1.数据库初始化:将下面这段代码直接在运行就可以初始化数据库了exec sp_configure 'show advanced options', '1 ...
- C# 调用delphi编写的dll
技术实现 如何逐步实现动态库的加载,类型的匹配,动态链接库函数导出的定义,参考下面宏定义即可: #define LIBEXPORT_API extern "C" __declspe ...
- Delphi7调用DelphiXE编写的DLL问题
http://bbs.csdn.net/topics/380045353 用DelphiXE在WIN2008下编写一个访问WebServices的DLL ws.dll,只有一个输出函数,如下: fun ...
- [JNA系列]Java调用Delphi编写的Dll之JNA使用
介绍 给大家介绍一个最新的访问本机代码的 Java 框架 —JNA . JNA(Java Native Access) 框架是一个开源的 Java 框架,是 SUN 公司主导开发的,建立在经典的 JN ...
- C#调用C++编写的dll
界面还是C#写的方便点,主要是有一个可视化的编辑器,不想画太多的时间在界面上.但是自己又对C++了解的多一些,所以在需要一个良好的界面的情况下,使用C++来写代码逻辑,将其编译成一个dll,然后用C# ...
- PB调用C#编写的Dll类库
在c# 中编写com组件,供PB调用实例 前言:c#中写的dll直接是不能被pb调用的,只有写成com组件才可以调用,所以用c#写dll时要注意. c#中新建类库 类库类型为通用类库,项目名为AddC ...
- 通过C#去调用C++编写的DLL
这个问题缠了我2个小时才弄出来,其实很简单.当对方提供一个dll给你使用时,你需要去了解这个dll 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...
随机推荐
- 博客开篇:随笔《从windows到linux的转变》。
在QQ群里讨论到了WINDOWS和LINUX.MAC,用手机码了如下回复,索性转过来当做博客的开篇.:) unix 和linux 在外很火的主要原因是开源,国外崇尚自由的精神是从出生就在细胞里的,而w ...
- 偶然翻出很久很久以前写的一款sqlmap UI,有点年头了
- Python + Selenium 实现登录Office 365
最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能.(吐槽:国内网络真是卡,登录Office 365实属不易.另外Selenium这样的网站都要墙,无法理解 ...
- 使用pngquant命令近乎无损压缩PNG图片大小减少70%左右
1.安装 wget http://pngquant.org/pngquant-2.8.2-src.tar.gz tar -xzf pngquant-2.8.2-src.tar.gz cd pngqua ...
- JSON.stringify的使用方法
语法: JSON.stringify(value [, replacer] [, space]) value:是必须要的字段.就是你输入的对象,比如数组啊,类啊等等. replacer:这个是可选的. ...
- 使用jQuery Autocomplete(自动完成)插件
jQuery 的Autocomplete(自动完成.自动填充)插件有不少,但比较下来我感觉,还是bassistance.de 的比较强大,我们就来写一些代码感受一下. 最简单的Autocomplete ...
- C++ 资源大全
http://www.uml.org.cn/c++/201411145.asp http://ezlippi.com/blog/2014/12/c-open-project.html <C++ ...
- 现代DOJO(翻译)
http://dojotoolkit.org/documentation/tutorials/1.10/modern_dojo/index.html 你可能已经不用doio一段时间了,或者你一直想保持 ...
- ARC中KVO开发注意
1 在ARC 中 KVO开发 添加监听和去掉监听必需 一一匹配,不要有过的去掉监听否则会有可能导致对象无法释放. 例如,在一个viewcontroller中添加webview 并监听webview的c ...
- 换一个思路压缩图片,RGB转YUV
一般的压缩方案 做移动平台,终究都是要考虑纹理压缩的问题 IOS/PVR平台上一般会选用PVRTC格式,这个格式压缩还是很给力. Android上设备种类很多,支持的格式各有不同.如果平台能支持下载前 ...