在实际项目工作中,经常用到C#调用C++ 或者C编写的dll文件。

dll支持一般函数声明和类的定义声明,但是一般为了简化,都是 采用函数声明的方式。这里主要并不是写 dll的编写。

先在vs中创建一个dll项目

添加一个新的cpp文件 测试代码如下:

struct Student   //定义了一个结构体,结构体中包含了基本类型,字符串类型和数组,基本上能满足很多情况了
{
int no;
char name[10];
int score[4];
};

extern "C" __declspec(dllexport) void __stdcall returnString(char* s)  //这是返回一个字符串的方法,和下面的可以作为对比。即返回字符串可以将字符串的指针作为参数或者作为函数返回值这两种方式
{
strcpy(s,"123456");
}

//extern "C" __declspec(dllexport) char* __stdcall returnString()
//{
// return "aaaaaa";
//}

extern "C" __declspec(dllexport) int __stdcall returnInt() //这是一个基本功能,返回了一个基本类型的值
{
return 123;
}

extern "C" __declspec(dllexport) void __stdcall returnIntArray(int *a) // 这是返回了一个基本类型的数组
{
for (int i=0;i<10;i++)
{
a[i] = i+1;
}
}

extern "C" __declspec(dllexport) void __stdcall returnStruct(Student & stu) //这是返回了一个结构体类型   注意:为什么我们一般在返回时使用参数返回,而不是使用函数体返回,因为有的时候会需要返回多个参数值,我们就一致习惯采用这种方式
{
stu.no = 100;
strcpy(stu.name,"csl");
for (int j=0;j<4;j++)
{
stu.score[j] = j*2+14;
}
}

//extern "C" __declspec(dllexport) void __stdcall returnStructArray(Student* stu) //这是返回了一个结构体指针类型,即返回值是一个结构体数组,可以返回多个结构体对象 和下面的方式是一样的
//{
// //stu = (Student*)malloc(sizeof(Student)*10);
// for (int i=0;i<10;i++)
// {
// stu[i].no = i+1;
// strcpy(stu[i].name,"csl");
// }
//
//}

extern "C" __declspec(dllexport) void __stdcall returnStructArray(Student stu[])
{
//stu = (Student*)malloc(sizeof(Student)*10);
for (int i=0;i<10;i++)
{
stu[i].no = i+1;
strcpy(stu[i].name,"csl");
for (int j=0;j<4;j++)
{
stu[i].score[j] = j*2+14;
}
}

}

------------C#中的调用---------------

public struct Student
{
public int no;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] // 指定数组尺寸
public Int32[] score;
}; //结构体声明

//调用函数的声明

[DllImport("E:\\C#\\TestLibDLL\\Debug\\TestLibDLL.dll")]
public static extern void returnString(StringBuilder s); // 字符串类型直接使用stringbuilder

[DllImport("E:\\C#\\TestLibDLL\\Debug\\TestLibDLL.dll")]
public static extern int returnInt();//返回基本类型

[DllImport("E:\\C#\\TestLibDLL\\Debug\\TestLibDLL.dll")]
public static extern void returnIntArray(int[] a); //返回数组 因为数组是引用类型,所以直接这么声明

[DllImport("E:\\C#\\TestLibDLL\\Debug\\TestLibDLL.dll")]
public static extern void returnStruct(out Student stu); //返回结构体 因为C++定义中使用得&引用,所以我们C#中需要使用out or  ref

[DllImport("E:\\C#\\TestLibDLL\\Debug\\TestLibDLL.dll")] 
public static extern void returnStructArray(IntPtr ptr); //对于返回结构体的数组,网上的说法是使用MarshalAs操作内存指针 自己试过用 student[]数组作为参数,得不到结果 ,使用指针的确可以

//具体操作

static void Main(string[] args)
{
//string s ;
// s = returnString();
//Console.WriteLine(s);

StringBuilder s = new StringBuilder();
returnString(s);
Console.WriteLine(s.ToString());

int nInt = returnInt();
Console.WriteLine(nInt);

int[] arrayList = new int[10];
returnIntArray(arrayList);

Student stu = new Student();
returnStruct(out stu);

Student[] stus = new Student[10];

int size = Marshal.SizeOf(typeof(Student)) * 10;
IntPtr ptr = Marshal.AllocHGlobal(size);
returnStructArray(ptr);
for (int i = 0; i < 10; i++)
{
IntPtr temp = (IntPtr)(UInt32)(ptr + i * size / 10);
stus[i] = (Student)Marshal.PtrToStructure(temp, typeof(Student));
}
Marshal.FreeHGlobal(ptr); //释放内存

Console.ReadKey();
}

对C#调用C++ dll文件进行总结的更多相关文章

  1. Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()

    Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...

  2. C#调用C++ DLL 文件

    说来惭愧,都注册一年多了,却没有发表过一篇正式的博文,中间很多学习的过程也没有记录下来.如今到了一个新的环境,也有了学习的机会,一定要把每天的收获记录一下. 要做的东西需要引用C++编写的DLL,刚开 ...

  3. java调用c#dll文件配置

    1 在强大的c#语言和java语言之间,二者难免会因为某些特殊的要求会相互调用. 下面就以java调用c#的dll为例做详细介绍 1  在vs中的环境设置如下图,图片中程序仅作为讲解程序,在项目编译成 ...

  4. VS2010 C#调用C++ DLL文件 【转】

    http://www.soaspx.com/dotnet/csharp/csharp_20110406_7469.html 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第 ...

  5. 用vc生成可被python调用的dll文件

    前提已经有.c 和.i文件 用swid编译了.i文件生成了wrap.c文件和.py文件 vc创建dll工程 将.h加入到头文件中.c文件和wrap.c文件添加到源文件中 将.i文件添加到工程目录下To ...

  6. 制作和unity调用动态链接库dll文件

    首先用vc建立一个dll工程 然后在里面建立一个testunity.h文件.内容如下 1 extern "C" int _declspec(dllexport)testunity( ...

  7. unity调用C++ dll文件

    首先建立Plugins文件夹,把dll文件放在里面 一一对应,我踩的坑是文件名加了后缀.dll,虽然不知道网上为什么都加了我这加了就报找不到dll文件错误,反正解决啦

  8. C#调用C++dll文件 运行提示找不到函数的入口点

    1.首先用DllAnalyzer查看dll的输出函数名,发现输出的函数名有所变化,多了@xxx和一些别的奇怪的字符,实际上是因为C++重载机制造成的,使用使用extern "C"关 ...

  9. VS2010 C#调用C++ DLL文件

    http://www.soaspx.com/dotnet/csharp/csharp_20110406_7469.html http://www.cnblogs.com/warensoft/archi ...

随机推荐

  1. zencart技术联盟交流群

    增加500人的免费互助"zencart技术联盟交流群"!名额不多,先到先得! zencart技术联盟俱乐部(1群) ( 已满) zencart技术联盟俱乐部(2群) 群号:1990 ...

  2. 实现经常使用的配置文件/初始化文件读取的一个C程序

    在编程中,我们常常会遇到一些配置文件或初始化文件. 这些文件通常后缀名为.ini或者.conf.能够直接用记事本打开.里面会存储一些程序參数,在程序中直接读取使用.比如,计算机与server通信.se ...

  3. 深度学习论文笔记-Deep Learning Face Representation from Predicting 10,000 Classes

    来自:CVPR 2014   作者:Yi Sun ,Xiaogang Wang,Xiaoao Tang 题目:Deep Learning Face Representation from Predic ...

  4. 安卓Activity界面切换添加动画特效

    在Android 2.0之后有了overridePendingTransition() ,其中里面两个参数,一个是前一个activity的退出两一个activity的进入, @Override pub ...

  5. JavaScript与Flash的通信

    当Flash置于HTML容器中时,经常会遇到AS与JS的通信问题,例如:JS能否调用AS中的变量.方法,AS能否调用JS中的变量.方法等等.答案是肯定的.随着技术的不断发展,解决方案也是多种多样的. ...

  6. [AngularJS] Using ngModel in Custom Directives

    You can use ngModel in your own directives, but there are a few things you'll need to do to get it w ...

  7. 一个公网地址部署LVS/DR模式

    http://blog.chinaunix.net/uid-7411781-id-3436142.html 一个公网地址部署LVS/DR模式   网上看了很多关于LVS的文章,在选取2种模式LVS/D ...

  8. Logistic回归总结

    原文:http://blog.csdn.net/dongtingzhizi/article/details/15962797  Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2 ...

  9. JS_CSS_logon_Mask

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 使用 collectionView 实现表头,区头,区尾

    UICollectionView 的使用是跟表的使用是一样,瀑布流的布局会比表的效果更好,这里说一下 collectionView 设置表头, 区头,区尾 设置表头可以约束 collectionVie ...