1. 如果函数只有传入参数,比如:

//C++中的输出函数
int __declspec(dllexport) test(const int N)
{
return N+;
}

对应的C#代码为:

[DllImport("test.dll", EntryPoint = "#1")]
public static extern int test(int m); private void button1_Click(object sender, EventArgs e)
{
textBox1.Text= test().ToString();
}

2. 如果函数有传出参数,比如:

void __declspec(dllexport) test(const int N, int& Z)
{
Z=N+;
}

对应的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 = ;
test1(, ref N);
textBox1.Text= N.ToString();
}

3. 带传入数组:

void __declspec(dllexport) test(const int N, const int n[], int& Z)
{
for (int i=; 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 = ;
int[] n;
n = new int[];
for (int i = ; i < ; 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=; i<M; i++)
{
N[i]=n[i]+;
}
}

对应的C#代码:

[DllImport("test.dll", EntryPoint = "#1")]
public static extern void test(int N, int[] n, [MarshalAs(UnmanagedType.LPArray,SizeParamIndex=)] int[] Z); private void button1_Click(object sender, EventArgs e)
{
int N = ;
int[] n, Z;
n = new int[N];Z = new int[N];
for (int i = ; i < N; i++)
{
n[i] = i;
}
test(n.Length, n, Z);
for (int i=; 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[])  

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[];
test(i, ref a, ref b, t);

字符数组的传递基本与4相似,只是mashalAs 时前面加上Out。

C#调用C++的dll各种传参的更多相关文章

  1. C#调用DLL各种传参

    C++#define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数 typedef struct { int osVersi ...

  2. Node调用C++(dll)

    最近开始搞毕设了,打算用自己拿手的js来搞,但是仿佛入坑了,Node还不是很熟.总之,兵来将挡,水来土掩嘛,带着问题学习才是最高效的. 折腾1:Node 调用 C++ 刚开始,虽然我老师把dll文件给 ...

  3. golang调用c++的dll库文件

    最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...

  4. vs2010下C++调用lib或dll文件

    注: DLL:表示链接库,包含dll,lib文件: dll: 表示my.dll文件 lib: 表示my.lib文件 C++ 调用.lib的方法: 一: 隐式的加载时链接,有三种方法 1  设置工程的 ...

  5. Mybatis调用PostgreSQL存储过程实现数组入参传递

    注:本文来源于 < Mybatis调用PostgreSQL存储过程实现数组入参传递  > 前言 项目中用到了Mybatis调用PostgreSQL存储过程(自定义函数)相关操作,由于Pos ...

  6. C++学习3--编程基础(vector、string、三种传参)

    知识点学习 Vector容器 vector是C++标准程序库中的一个类,其定义于头文件中,与其他STL组件一样,ventor属于STD名称空间: ventor是C++标准程序库里最基本的容器,设计之初 ...

  7. C#调用C/C++ DLL 参数传递和回调函数的总结

    原文:C#调用C/C++ DLL 参数传递和回调函数的总结 Int型传入: Dll端: extern "C" __declspec(dllexport) int Add(int a ...

  8. 浅谈C++三种传参方式

    浅谈C++三种传参方式 C++给函数传参中,主要有三种方式:分别是值传递.指针传递和引用传递. 下面通过讲解和实例来说明三种方式的区别. 值传递 我们都知道,在函数定义括号中的参数是形参,是给函数内专 ...

  9. 关于C#调用非托管DLL,报“内存已损坏的”坑,坑,坑

    因客户需求,与第三方对接,调用非托管DLL,之前正常对接的程序,却总是报“内存已损坏的异常”,程序进程直接死掉,折腾到这个点(2018-05-11 00:26),终于尘埃落定,直接上程序. 之前的程序 ...

随机推荐

  1. boost::multi_index 提供一种千人在线即时排行榜的设计思路

    原文地址: http://www.limerence2017.com/2019/06/23/cpp01/ 做游戏或金融后台开发,经常会遇到设计开发排行榜的需求.比如玩家的充值排行,战力排行等等.而这种 ...

  2. Swift3.0基础语法学习<一>

    // // ViewController.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/15. // Copyright © 2016年 ...

  3. 【转】Spark History Server 架构原理介绍

    [From]https://blog.csdn.net/u013332124/article/details/88350345 Spark History Server 是spark内置的一个http ...

  4. PJzhang:U盘容量变小后的恢复以及U盘加密

    猫宁!!! 参考链接:https://jingyan.baidu.com/article/8ebacdf0544ae049f65cd5da.html 我的一个U盘,16G,制作了deepin linu ...

  5. 配置中心 Spring Cloud config

    配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. 1.服务端 创建spring boot 项目 主要依赖 <dependenc ...

  6. OpenStack组件——Horizon Web界面管理服务

    1.horizon 介绍 理解 horizon Horizon 为 Openstack 提供一个 WEB 前端的管理界面 (UI 服务 )通过 Horizone 所提供的 DashBoard 服务 , ...

  7. 应用安全 - 工具|平台 - CDN - 使用|命令 - 汇总

    简介 用途 使用缓存适应高并发请求 功能 ()抗DDOS ()隐藏真实IP 全球DNS地址分布:http://www.ab173.com/dns/dns_world.php全球IP地址段分布:http ...

  8. Java 200道题

    1. junit用法,before,beforeClass,after, afterClass的执行顺序 一个测试类单元测试的执行顺序为: @BeforeClass –> @Before –&g ...

  9. SpringSecurity 配置

    SpringSecurity+JWT https://www.jianshu.com/p/5b9f1f4de88d https://blog.csdn.net/qq_35494808/article/ ...

  10. 2019牛客暑期多校训练营(第二场)-E MAZE

    题目链接:https://ac.nowcoder.com/acm/contest/882/E 题意:n×m的矩阵,0表示可以走,1表示墙,不能通过.有q中操作,一种是改变坐标(x,y)的状态,一种是询 ...