一、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
未完。。。。
轉載于:https://www.cnblogs.com/zhaotianff/p/8991847.html