天天看點

C#與C++互操作

一、C#調用C++庫

1、建立C++庫

打開VisualStudio,建立一個C++工程,輸入項目名稱HelloWorldLib

C#與C++互操作

确定,然後下一步。選擇應用程式類型為DLL

C#與C++互操作

單擊完成,我們就建立好了一個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 }      

二、以子產品定義檔案的方式導出

在工程上右鍵,選擇添加-》建立項

C#與C++互操作

然後選擇代碼-》子產品定義檔案

C#與C++互操作

在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#與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++庫,這裡委托的作用就比較明顯了。把委托比喻為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#與C++互操作

好的,前面函數裡面涉及的都是簡單資料類型,下面來介紹一下複雜資料類型。這裡指的是結構體

在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++互操作,很重要的一個地方就是,要注意資料類型的對應。有時還需要加上一些限制,

關于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

C#與C++互操作

這裡我們使用C#封裝一個讀取XML節點的函數(僅供示範)

先建立一個XML檔案,并儲存為Student.xml

1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
2 <Student>
3     <Name>自由在高處</Name>
4     <Age>17</Age>
5     <Size>40</Size>
6 </Student>      

在CSharpLib工程中建立一個XmlQuery類(包含XPathQuery和GetFirstStudent兩個成員函數)和一個Student結構體,代碼如下:

1 public class XmlQuery
 2     {
 3         private string fileName;
 4         private XDocument doc;
 5 
 6         public XmlQuery(string fileName)
 7         {
 8             this.fileName = fileName;
 9             doc = XDocument.Load(fileName);
10         }
11 
12         public XmlQuery()
13         {
14 
15         }
16 
17         public string XPathQuery(string xPath)
18         {
19             if (doc == null)
20                 return "";
21 
22             var result = doc.XPathSelectElement(xPath);
23 
24             if (result == null)
25                 return "";
26 
27             return result.Value;
28         }
29 
30         public Student GetFirstStudent()
31         {
32             if (doc == null)
33                 return new Student();
34 
35             var root = doc.Root;
36 
37             Student student = new Student();
38             student.Name = root.Element("Name").Value;
39             student.Age = Convert.ToInt32( root.Element("Age").Value);
40             student.Size = Convert.ToInt32(root.Element("Size").Value);
41 
42             return student;
43         }
44     }
45 
46     public struct Student
47     {
48         /// <summary>
49         /// 需要限定長度,否則會轉換失敗
50         /// </summary>
51         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
52         public string Name;
53 
54         /// <summary>
55         /// 基本資料類型注意封送時對應的類型即可
56         /// </summary>
57         public int Age;
58 
59         public int Size;
60     }      

然後我們建立一個C++控制台程式UseCSharpLib

C#與C++互操作

建立完成後,将前面編譯的CSharpLib.dll和Student.xml拷貝到編譯目錄下和代碼目錄下

C#與C++互操作
C#與C++互操作

然後到屬性頁裡開啟公共語言運作時支援

C#與C++互操作

使用#using引用前面編譯并複制到代碼目錄下的C#庫。這裡的相對路徑是相對代碼工程檔案所在的位置

1 #using "CSharpLib.dll"      

包含必要的頭檔案及引入命名空間

1 #include<msclr\marshal_cppstd.h>
2 
3 using namespace System;
4 using namespace msclr::interop;
5 using namespace std;
6 using namespace CSharpLib;      

使用gcnew執行個體化對象,并調用XmlQuery類的成員函數XPathQuery:

1 XmlQuery^ query = gcnew XmlQuery("Student.xml");
2 System::String ^str = query->XPathQuery("Student/Name");      

XPathQuery函數傳回類型是System.String類型,可以直接使用.Net中String類提供的功能,也可以轉換成char*來進行下一步的操作

1 //直接使用System.String類中的屬性和函數
 2 System::Console::WriteLine(str);
 3 System::Console::WriteLine(str->Length);
 4     
 5 //将System.String轉換成char*
 6 System::IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
 7 char* chData = (char *)(void *)ptr;
 8     
 9 cout << chData << endl;
10 cout << strlen(chData) << endl;
11 
12 //釋放記憶體
13 System::Runtime::InteropServices::Marshal::FreeHGlobal(ptr);      

運作結果如下:

C#與C++互操作

GetFirstStudent函數傳回了一個Student結構體,下面的代碼示範了調用該函數後,将傳回值轉換為C++中的結構體。

首先在C++中定義一個結構體:

1 struct StudentCPP
2 {
3     char Name[256]; //大小要跟C#中的保持一緻
4     int Age;
5     int Size;
6 };      

調用GetFirstStudent

1 CSharpLib::Student stu = query->GetFirstStudent();
 2 
 3 //可以直接操作stu
 4 int age = stu.Age;
 5 System::String^ name = stu.Name;
 6 int size = stu.Size;
 7 
 8 //也可以轉換為C++中的結構體
 9 System::IntPtr stuPtr = System::Runtime::InteropServices::Marshal::AllocHGlobal(System::Runtime::InteropServices::Marshal::SizeOf(stu));
10 System::Runtime::InteropServices::Marshal::StructureToPtr(stu, stuPtr,true);
11 StudentCPP* student = (StudentCPP*)(void*)stuPtr;
12 
13 cout << "Name: " << student->Name << endl
14     << "Age: " << student->Age << endl
15     << "Size: " << student->Size << endl;
16 
17 //釋放記憶體
18 System::Runtime::InteropServices::Marshal::FreeHGlobal(stuPtr);      

運作如果如下:

C#與C++互操作

說明:

1、需要注意C#和C++在進行互操作時的資料類型對應 。可以參考以下連結

https://www.cnblogs.com/zhaotianff/p/12896297.html

2、C#中的類和結構體可以轉換成C++中的結構體,但要注意一些原則,可以參考以下連結

https://www.cnblogs.com/zhaotianff/p/12510286.html

https://www.cnblogs.com/zhaotianff/p/13300438.html

3、暫時隻想到這兩點

最後再附上示例代碼,玩得愉快。

時隔兩年才更新在C++中調用C#庫,我也是醉了啊

上一篇: 練習8