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 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...
随机推荐
- ZTOOLS HTTP®EXTEST&JSONS 工具包
下载地址:点击下载
- C段旁注工具CCC.exe
C段旁注工具CCC.exe可以进行C段的web站点批量查询 自动排除DNS错误的域名以及IP和当前服务器不符的域名 抓取bing上的所有URL,不光是域名信息,方便直接进入 自动生成html报告,方便 ...
- Back-propagation, an introduction
About Contact Subscribe Back-propagation, an introduction Sanjeev Arora and Tengyu Ma • Dec 20, ...
- u-boot-2010.09移植(B)
前面我们的u-boot只是在内存中运行,要想在nandflash中运行,以达到开机自启的目的,还需作如下修改 一.添加DM9000网卡支持 1.修改board/fl2440/fl2440.c中的boa ...
- Eclipse中web项目部署至Tomcat步骤
Eclipse的web工程至Tomcat默认的部署目录是在工程空间下,本文旨在将部署目录改为Tomcat安装目录,并解决依赖包输出问题. 1.在Eclipse中添加Tomcat服务器. 2.将web工 ...
- RegOpenKeyEx和RegSetValueEx返回ERROR_SUCCESS,但注册表未发生变化。
win7 x64,需要open的时候加上KEY_WOW64_64KEY. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microso ...
- 支付宝支付参数MD5签名
protected String signMD5(List<String> paramNames, String key, String charset) throws Unsupport ...
- pt-online-schema-change 修改主键导致数据删除失败的问题调查
pt-online-schema-change在线DDL工具可以做到DDL操作不锁表,不影响线上操作.对于线上超过100W的大表,一般情况下都用这个工具做DDL,最重要的考虑点还是“不影响线上操作” ...
- ruby 中%Q %q %W %w %x %r %s的用法
%Q 用于替代双引号的字符串. 当你需要在字符串里放入很多引号时候, 可以直接用下面方法而不需要在引号前逐个添加反斜杠 (\") >> %Q(Joe said: "Fr ...
- Join two DataTables in C#
var query = (from x in a.AsEnumerable() join y in b.AsEnumerable() on x.Field<int>("col1& ...