C#与C++与互操做

1、C#调用C++库html

一、建立C++库ios


打开VisualStudio,建立一个C++工程,输入项目名称HelloWorldLib编程

肯定,而后下一步。选择应用程序类型为DLL函数

单击完成,咱们就建立好了一个C++库的项目。url

这里为了方便,咱们直接在HelloWorldLib.cpp里定义函数spa

C++库导出有两种方式.net

1、以C语言接口的方式导出指针

这种方法就是在函数前面加上 extern "C" __declspec(dllexport)code

加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C++的。htm

 1 #include "stdafx.h"
 2 #include<iostream>
 3 
 4 extern "C" __declspec(dllexport) void HelloWorld(char* name);  5 
 6 
 7 extern "C" __declspec(dllexport) void HelloWorld(char* name)  8 {  9     std::cout << "Hello World " << name << std::endl; 10 }

 

2、以模块定义文件的方式导出

在工程上右键,选择添加-》新建项

而后选择代码-》模块定义文件

在Source.def中输入

LIBRARY EXPORTS HelloWorld

EXPORTS下面就是要导出的函数,这里不须要添加分号隔开,直接换行就行。

此时,咱们函数的定义以下

 1 #include "stdafx.h"
 2 #include<iostream>
 3 
 4 void HelloWorld(char* name);  5 
 6 
 7 void HelloWorld(char* name)  8 {  9     std::cout <<"Hello World "<< name << std::endl; 10 }

编译,生成dll。这里须要注意的是,若是生成是64位的库,C#程序也要是64位的,不然会报错。

二、使用C#调用

接下来咱们新建一个C#控制台项目

打开前面C++库生成的目录,将HelloWorldLib.dll复制到C#工程的Debug目录下。也能够不复制,只需在引用dll的时候写上完整路径就好了。这里我是直接复制到Debug目录下

 1 using System.Runtime.InteropServices;  2 
 3 namespace ConsoleApplication2  4 {  5     class Program  6  {  7         [DllImport("HelloWorldLib.dll")]  8         public static extern void HelloWorld(string name);  9 
10         //能够经过EntryPoint特性指定函数入口,而后为函数定义别名
11 
12         [DllImport("HelloWorldLib.dll", EntryPoint = "HelloWorld")] 13         public static extern void CustomName(string name); 14         static void Main(string[] args) 15  { 16             HelloWorld("LiLi"); 17             //跟上面是同样的
18             CustomName("QiQi"); 19  } 20  } 21 }

运行程序,结果以下:

这样就成功建立了一个C#能够调用的C++库

下面咱们动态调用C++库,这里委托的做用就比较明显了。把委托比喻为C++的函数指针,一点也不为过。

咱们在C++库中再新增一个函数GetYear(),用来获取当前年份。

1 int GetYear(); 2 
3 int GetYear() 4 { 5  SYSTEMTIME tm; 6     GetLocalTime(&tm); 7 
8     return tm.wYear; 9 }

记得在导出文件中(Source.def)增长GetYear。编译,生成新的DLL

再新建一个C#控制台程序

代码以下:

 1 using System;  2 using System.Runtime.InteropServices;  3 
 4 namespace ConsoleApplication3  5 {  6 
 7     class Program  8  {  9         [DllImport("kernel32.dll")] 10         public static extern IntPtr LoadLibrary(string lpFileName); 11 
12         [DllImport("kernel32.dll")] 13         public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); 14 
15         [DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)] 16         public static extern bool FreeLibrary(IntPtr hModule); 17 
18         //声明委托,这里的签名,须要跟C++库中的对应
19         delegate int GetYearDelegate(); 20 
21         static void Main(string[] args) 22  { 23  GetYearDelegate m_fGetYear; 24             IntPtr hModule = LoadLibrary("HelloWorldLib.dll"); 25             if(hModule != IntPtr.Zero) 26  { 27                 IntPtr hProc = GetProcAddress(hModule, "GetYear"); 28                 if(hProc != IntPtr.Zero) 29  { 30                     m_fGetYear = (GetYearDelegate)Marshal.GetDelegateForFunctionPointer(hProc, typeof(GetYearDelegate)); 31 
32                     //在这里能够调用
33                     int year = m_fGetYear(); 34                     Console.WriteLine("年份是:" + year); 35  } 36  } 37  } 38  } 39 }

运行结果:

好的,前面函数里面涉及的都是简单数据类型,下面来介绍一下复杂数据类型。这里指的是结构体

在C++库中定义一个GetDate()的函数,代码以下。这里也要记得在导出文件中添加(Source.def)

struct MyDate { int year; int month; int day; }; MyDate GetDate(); MyDate GetDate() { SYSTEMTIME tm; GetLocalTime(&tm); MyDate md; md.day = tm.wDay; md.month = tm.wMonth; md.year = tm.wYear; return md; }

 新建一个C#控制台程序,完整代码以下

 1 using System;  2 using System.Runtime.InteropServices;  3 
 4 namespace ConsoleApplication3  5 {  6     struct MyDate  7  {  8         public int Year;  9         public int Month; 10         public int Day; 11  } 12 
13 
14     class Program 15  { 16         [DllImport("kernel32.dll")] 17         public static extern IntPtr LoadLibrary(string lpFileName); 18 
19         [DllImport("kernel32.dll")] 20         public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); 21 
22         [DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)] 23         public static extern bool FreeLibrary(IntPtr hModule); 24 
25         delegate IntPtr GetDateDelegate(); 26 
27         static void Main(string[] args) 28  { 29  GetDateDelegate m_fGetDate; 30             IntPtr hModule = LoadLibrary("HelloWorldLib.dll"); 31 
32             if (hModule != IntPtr.Zero) 33  { 34                 IntPtr hProc = GetProcAddress(hModule, "GetDate"); 35                 if (hProc != IntPtr.Zero) 36  { 37                     m_fGetDate = (GetDateDelegate)Marshal.GetDelegateForFunctionPointer(hProc, typeof(GetDateDelegate)); 38                     IntPtr ptr = m_fGetDate(); 39                     if(ptr != IntPtr.Zero) 40  { 41                         MyDate md = (MyDate)Marshal.PtrToStructure(ptr, typeof(MyDate)); 42                         Console.WriteLine("{0}年-{1}月-{2}日",md.Year,md.Month,md.Day); 43  } 44  } 45  } 46  } 47  } 48 }

运行结果以下:

C#与C++互操做,很重要的一个地方就是,要注意数据类型的对应。有时还须要加上一些限制,

关于C#与C++数据类型对应

能够参考如下连接:

https://www.cnblogs.com/zjoch/p/5999335.html

大部分硬件厂商提供的SDK都是须要C++来调用的,有了上面的知识,使用C#来调用一些硬件的SDK就比较容易了。只须要使用C++再进行一次封装就好了。

2、C++调用C#库

这里用到是C++/CLI,就是如何用C++在·NET中编程。就是由于有这个东西的存在,C++才能调用C#的库

下面新建一个C#类库CSharpLib

 

未完。。。。

相关文章
相关标签/搜索