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

 

未完。。。。

相關文章
相關標籤/搜索