一、C#調用C++庫
1、創建C++庫
打開VisualStudio,創建一個C++工程,輸入項目名稱HelloWorldLib
確定,然后下一步。選擇應用程序類型為DLL
單擊完成,我們就創建好了一個C++庫的項目。
這里為了方便,我們直接在HelloWorldLib.cpp里定義函數
C++庫導出有兩種方式
一、以C語言接口的方式導出
這種方法就是在函數前面加上 extern "C" __declspec(dllexport)
加上extern "C"后,會指示編譯器這部分代碼按C語言的進行編譯,而不是C++的。
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 }
二、以模塊定義文件的方式導出
在工程上右鍵,選擇添加-》新建項
然后選擇代碼-》模塊定義文件
在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位的,否則會報錯。
2、使用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++再進行一次封裝就行了。
二、C++調用C#庫
這里用到是C++/CLI,就是如何用C++在·NET中編程。就是因為有這個東西的存在,C++才能調用C#的庫
下面新建一個C#類庫CSharpLib
未完。。。。