1.基本数据类型的传递

常见数据类型的传递

C/C++

C#

长度

short

short

2Bytes

int

int

4Bytes

long(该类型在传递的时候常常会弄混)

int

4Bytes

bool

bool

1Byte

char(Ascii码字符)

byte

1Byte

wchar_t(Unicode字符,该类型与C#中的Char兼容)

char

2Bytes

float

float

4Bytes

double

double

8Bytes

C++传入、传出C#数组都可以实现,传入数组直接传入,传出数组需要用指针传出。

(1).C++传入数组(测试1加到9的和)

.cpp文件
extern "C" __declspec(dllexport) void test(const int N, const int n[], int& Z)
{
for (int i = ; i<N; i++)
{
Z += n[i];
}
}
.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Application6
{
class Program
{
[DllImport(@"C:\Users\wdy\Documents\visual studio 2013\Projects\Application5\Debug\Application5.dll", EntryPoint = "test", CallingConvention = CallingConvention.Cdecl)]
public static extern double test(int N, int[] n, ref int Z); static void Main(string[] args)
{
int N = ;
int[] n;
n = new int[];
for (int i = ; i < ; i++)
{
n[i] = i;
}
test(n.Length, n, ref N);
Console.WriteLine(N.ToString());
Console.Read();
}
}
}

结果:

(2).C++传出数组(测试传出结构体数组指针字符串)

.cpp文件
#include "stdafx.h"
#include "Application3.h" struct cmppe_submit
{
char user[][];
}; extern "C" __declspec(dllexport) void GetUser(cmppe_submit* lpSubit)
{
strcpy(lpSubit->user[], "waqerqwdsewf");
}
.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Application4
{
class Program
{
struct cmppe_submit
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = )]
public byte[] dst_addr;
}
[DllImport(@"C:\users\wdy\documents\visual studio 2013\Projects\Application3\Debug\Application3.dll", EntryPoint = "GetUser", CallingConvention = CallingConvention.Cdecl)]
private extern static void GetUser(ref cmppe_submit lpSubmit);//用ref声明结构 static void Main(string[] args) {
cmppe_submit submit;
submit.dst_addr = new byte[];
GetUser(ref submit); string str = System.Text.Encoding.Default.GetString(submit.dst_addr, , ); Console.WriteLine(str); Console.Read();
}
}
}

调试中出现过的Bug:

.Program::GetUser”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。

添加属性:EntryPoint = "GetUser", CallingConvention = CallingConvention.Cdecl

结果:

(3)C++传入、传出带参数结构体

.h文件
#define TESTCPPDLL_API __declspec(dllexport)
struct Vector3 { float X, Y, Z; }; EXTERN_C TESTCPPDLL_API void __stdcall SendStructFromCSToCPP(Vector3 vector);
.cpp文件
TESTCPPDLL_API void __stdcall SendStructFromCSToCPP(Vector3 vector) {
vector.X = vector.X + ;
cout << "got vector3 in cpp,x:"; cout << vector.X+; cout << ",Y:"; cout << vector.Y; cout << ",Z:"; cout << vector.Z; }
.CS文件
[StructLayout(LayoutKind.Sequential)] struct Vector3
{ public float X, Y, Z; } [DllImport(@"C:\Users\wdy\Documents\visual studio 2013\Projects\TestCPPDLL\x64\Debug\TestCPPDLL.dll", EntryPoint = "SendStructFromCSToCPP")] extern static void SendStructFromCSToCPP(Vector3 vector);
Vector3 vector = new Vector3() { X = , Y = , Z = }; //将vector传递给C++并在C++中输出 SendStructFromCSToCPP(vector);

结果

C#调用C++数组,结构体DLL的更多相关文章

  1. C语言基础知识点整理(函数/变量/常量/指针/数组/结构体)

    函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  2. C数组&结构体&联合体快速初始化

    背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组某些元 ...

  3. c++ 数组 结构体

    接下来的一点时间我将会记录下我看的c++的一些心得体会,人贵在坚持,希望我可以一直坚持下去!!Go Fighting!   一.c++复合数据类型: 数组类型的一些注意事项: sizeof的用法: 当 ...

  4. C89,C99: C数组&结构体&联合体快速初始化

    1. 背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组 ...

  5. Leetcode LRU缓存,数组+结构体实现

    一.算法思路 LRUCache类有以下函数和变量: LRUCache(int capacity): capacity是当前对象能够存储的键值对(key,value)最大个数. int get(int ...

  6. 【C++】结构体/结构体数组/结构体指针/结构体嵌套/函数参数/const

    一.结构体声明 struct Student { //成员列表 string name; int age; int score; }; //s3;定义时直接声明 int main() { struct ...

  7. C#调用C/C++动态库 封送结构体,结构体数组

    一. 结构体的传递 #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数 typedef struct { int ...

  8. C#调用C/C++动态库,封装各种复杂结构体

    C#调用C/C++动态库,封装各种复杂结构体. 标签: c++结构内存typedefc# 2014-07-05 12:10 6571人阅读 评论(1) 收藏 举报  分类: C(8)  C#(6)  ...

  9. C# 调用C++结构体

    参考网址:C#调用C/C++动态库,封装各种复杂结构体._liguo9860的专栏-CSDN博客 现在公司要做一个使用C#程序调用C++的一个DLL库,解析文件的功能.所以在网上找了一些资料.     ...

随机推荐

  1. 关于android studio几种常见的错误解决

    我也是从ec转到as的,没办法,大势所趋嘛,然而,在使用as的过程中遇到了非常多匪夷所思的错误,如今就说一下今天我遇到的这个错误. 美工妹子给了我一张图片,用来当做button的背景图,当然,这个图也 ...

  2. JSP编程技术5-购物车的实现-session会话对象

    首先十分感谢大家对我的文章的支持,我是个刚刚才找到自己方向的在校大学生,当然我的知识和能力有限,眼下正在努力恶补学习中.当我看到自己首次发表到CSDN首页的文章才几个小时阅读量就超过了100时,对我来 ...

  3. legend---一、如何实现js跳转到php页面

    legend---一.如何实现js跳转到php页面 一.总结 一句话总结:url还是同样的方式,只不过注意引号内嵌的时候的转义. 代码: onClick="javascript:if(con ...

  4. Ehcache整合spring配置,配置springMVC缓存

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1.   需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...

  5. 程序发布出现: 服务器无法处理请求--->无法生成临时类(result = 1)。 错误CS2001:未能找到源文件“C:\ Windows \ TEMP \ lph54vwf.0.cs”

    服务器上发布的web服务程序出错: 服务器无法处理请求--->无法生成临时类(result = 1).错误CS2001:未能找到源文件“C:\ Windows \ TEMP \ lph54vwf ...

  6. HP 1022N 网络打印机安装步骤

    HP 1022N 网络打印机安装步骤

  7. 【Linux下安装配置Jupyter】

    """ 第一步 安装 """ pip3 install -i https://pypi.douban.com/simple jupyter ...

  8. 二:2.1 字符串与循环中的 while

    字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...

  9. Swift学习笔记(11)--类与结构体

    类与结构是编程人员在代码中会经常用到的代码块.在类与结构中可以像定义常量,变量和函数一样,定义相关的属性和方法以此来实现各种功能. 和其它的编程语言不太相同的是,Swift不需要单独创建接口或者实现文 ...

  10. CCF模拟题 窗口

    窗口 时间限制: 1.0s 内存限制: 256.0MB   问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的 ...