1、内存对齐的规则
(适用于c++、c#)
首先需要了解c++、c#的内存对齐的规则:
结构体的数据成员,第一个成员的偏移量为0,后面的每个数据成员存储的起始位置要从自己大小的整数倍开始。
子结构体中的第一个成员偏移量应当是子结构体中最大成员的整数倍。
结构体总大小必须是其内部最大成员的整数倍。
以下为c#示例代码:
internal class program { struct info { double dd;//32-40 bool bo;//40-48 } struct mystruct { char c;//0-1 decimal d;//8-16 int a;//16-20 double b;//24-32 info info;//32- } static unsafe void main(string[] args) { console.writeline(sizeof(mystruct));//输出结果:48 } }
2、调用约定
_cdecl称之为c调用约定,参数从右至左的方式入栈,函数本身不清理栈,此工作由调用者负责,所以允许可变参数函数存在。我们自己编写的程序一般为_cdecl
_stdcall称之为标准调用约定,参数从右至左的方式入栈,函数本身清理栈,所以不允许可变参数函数存在。windowsapi一般为_stdcall
3、c#传递基本数据类型到c++
c++与c#的基本类型对应关系如下表所示,数据通过值传递
c++基本类型 | c#基本类型 |
---|---|
int | int |
char | sbyte |
short | short |
float | float |
double | double |
long long | long |
bool | bool |
char* | string(传值,需charset = charset.ansi) |
int*,double* | ref int,ref double |
int&,double& | ref int,ref double |
**不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
c++ 代码
//native.h文件 extern "c" { __declspec(dllexport) void __cdecl testbasicdata(bool d1,char d2,short d3, int d4,long long d5,float d6,double d8); } //native.cpp文件 #include "native.h" void __cdecl testbasicdata(bool d1, char d2, short d3, int d4, long long d5, float d6, double d8) { return;//在此处添加断点,观察在c#中的数据传递到了c++代码中 }
c#代码
[dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int testbasicdata(bool d1, sbyte d2, short d3, int d4, long d5, float d6, double d8); static void main(string[] args) { testbasicdata(true, 64, 128, 123456, 123456789, 12.45f, 3.142592); }
注意:使用vs调试过程中,需要在c#工程中勾选启动本地代码调试如下入所示,这样调试的时候才会进入c++代码。
建议在vs的解决方案属性中,将c#控制台项目依赖c++的dll项目,如下入所示。这样编译c#项目会自动编译c++项目。
4、c#传递基本数据类型的数组到c++
**不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
以下以传递int*为例
c++代码如下,生成nativedll.dll文件
//native.h文件 #pragma once extern "c" { __declspec(dllexport) int __cdecl testadddoubles(int* d, int length); } //native.cpp文件 #include "native.h" int __cdecl testadddoubles(int* d, int length) { int re = 0; for (size_t i = 0; i < length; i++) { re += d[i]; d[i] = 99;//改变d地址内的数据,在c#代码中也可以看到dpoint地址内的数据被改为99 } return re; }
c#代码如下
[dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int testadddoubles(intptr d, int length); static void main(string[] args) { intptr dpoint = marshal.allochglobal(sizeof(int)*4);//在非托管内存中创建4个int大小内存的指针 unsafe { int* ptr = (int*)dpoint.topointer(); ptr[0] = 1; ptr[1] = 3; ptr[2] = 5; ptr[3] = 7; } var re = testadddoubles(dpoint, 4); console.writeline(re);//输出结果为16 marshal.freehglobal(dpoint);//非托管内存需要在c#代码中释放 console.readline(); }
5、c#传递基本类型组成的结构体给c++
5.1 按值传递基本类型组成的结构体
**不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
c++代码如下
//native.h文件 extern "c" { struct shape//注意:一定要与c#中的结构体对齐方式一致 { int x; int y; int z; double area; double volume; }; __declspec(dllexport) int __cdecl teststructor(shape p); } //native.cpp文件 #include"native.h" int __cdecl teststructor(shape p) { return sizeof(p); }
c#代码如下
struct shape//注意:一定要与c++中的结构体对齐方式一致 { public int x; public int y; public int z; public double area; public double volume; } [dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int teststructor(shape shape); static void main(string[] args) { shape shape = new shape() { x = 10, y = 20, z = 30, area = 123.45, volume = 3456.98 }; var len = teststructor(shape);//传值得方式将结构体传给c++ console.writeline(len); }
5.2 按引用传递基本类型组成的结构体给c++结构体指针
**不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
c++代码如下
//native.h文件 extern "c" { struct shape//注意:一定要与c#中的结构体对齐方式一致 { int x; int y; int z; double area; double volume; }; __declspec(dllexport) int __cdecl teststructorpointer(shape* p); } //native.cpp文件 #include"native.h" int __cdecl teststructorpointer(shape* p) { int r = sizeof(*p); p->x = 100; p->y = 100; p->z = 100; p->area = 10.1; p->volume = 10.1; return r; }
c#代码如下
struct shape//注意:一定要与c++中的结构体对齐方式一致 { public int x; public int y; public int z; public double area; public double volume; } [dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int teststructorpointer(ref shape shape); static void main(string[] args) { shape shape = new shape() { x = 10, y = 20, z = 30, area = 123.45, volume = 3456.98 }; var len = teststructorpointer(ref shape);//ref方式将结构体传给c++,通过断点调试,查看c#中的shape也更改了 console.writeline(len); }
6、c#传递元素有数组的结构体
**不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
结构体是按值进行传递的。
c++代码如下
//native.h文件 #pragma once extern "c" { struct student { char name[50]; int age; double score; }; __declspec(dllexport) int __cdecl teststudentstructor(student s); } //native.cpp文件 #include "native.h" int __cdecl teststudentstructor(student s) { int len = sizeof(s); return len; }
c#中的结构体映射到c++内存中,要求结构体只能包含非托管类型
c#代码如下
unsafe struct student//注意:一定要与c++中的结构体对齐方式一致 { public fixed byte name[50]; public int age; public double score; } [dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int teststudentstructor(student s); static unsafe void main(string[] args) { student s = new student(); s.age = 12; s.score = 123.4; var name = encoding.getencoding("gb2312").getbytes("abcd\0");//c++的字符串编码为gb2312,结尾添加\0 marshal.copy(name, 0,new intptr(s.name),name.length); var len = teststudentstructor(s); console.writeline(len); }
7、c#传递元素有数组的结构体指针
** 不要将c#中托管堆上的数据,按照传引用的方式传递到c++中 **
c++代码如下
//native.h文件 #pragma once extern "c" { struct student { char name[50];//结构体中含有char数组 int age; double score; }; __declspec(dllexport) int __cdecl teststudentstructorpointer(student* s); } //native.cpp文件 #include "native.h" int __cdecl teststudentstructorpointer(student* s)//c++中改变s,c#中也会变 { int len = sizeof(*s); s->age = 1000; s->score = 1000.0; for (size_t i = 0; i < sizeof(s->name); i++) { s->name[i] = 123; } return len; }
c#中的结构体映射到c++内存中,要求结构体只能包含非托管类型
c#代码如下
unsafe struct student//注意:一定要与c++中的结构体对齐方式一致 { public fixed byte name[50]; public int age; public double score; } [dllimport("nativedll", callingconvention = callingconvention.cdecl)] public static extern int teststudentstructorpointer(ref student s);//使用ref static void main(string[] args) { student s = new student(); s.age = 12; s.score = 123.4; var name = encoding.getencoding("gb2312").getbytes("abcd\0");//结尾添加\0 unsafe { marshal.copy(name, 0, new intptr(s.name), name.length); } var len = teststudentstructorpointer(ref s);//ref方式将结构体传给c++,通过断点查看c#中的s也更改了 console.writeline(len); }
到此这篇关于c#调用c++的实现步骤的文章就介绍到这了,更多相关c#调用c++内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论