1. 程式人生 > >C#調用c++類的導出函數

C#調用c++類的導出函數

nbsp ret 第三方 creat logs 特性 其中 debug 屬性

C# 需要調用C++東西,但是有不想做成COM,就只好先導出類中的函數處理。

不能直接調用,需單獨導出函數

參考:http://blog.csdn.net/cartzhang/article/details/9097043

c# 調用c++導出類的一個示例

參考:http://blog.csdn.net/huiyouyongdeyu2011/article/details/6547931

還沒測試此例子

以下是VS2010 C#調用C++ DLL文件的例子

背景

在項目過程中,有時候你需要調用非C#編寫的DLL文件,尤其在使用一些第三方通訊組件的時候,通過C#來開發應用軟件時,就需要利用DllImport特性進行方法調用。本篇文章將引導你快速理解這個調用的過程。

步驟

1. 創建一個CSharpInvokeCPP的解決方案:

技術分享

2. 創建一個C++的動態庫項目:

技術分享

3. 在應用程序設置中,選擇“DLL”,其他按照默認選項:

技術分享

最後點擊完成,得到如圖所示項目:

技術分享

我們可以看到這裏有一些文件,其中dllmain.cpp作為定義DLL應用程序的入口點,它的作用跟exe文件有個main或者WinMain入口函數是一樣的,它就是作為DLL的一個入口函數,實際上它是個可選的文件。它是在靜態鏈接時或動態鏈接時調用LoadLibrary和FreeLibrary時都會被調用。詳細內容可以參考(http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx

)。

4. 現在我們打開CSharpInvokeCPP.CPPDemo.cpp文件:

現在我們加入以下內容:


1 // CSharpInvokeCPP.CPPDemo.cpp : 定義 DLL 應用程序的導出函數。
2 //
3
4 #include "stdafx.h"
5
6 extern "C" __declspec(dllexport) int Add(int x, int y)
7 {
8 return x + y;
9 }
10 extern "C" __declspec(dllexport) int Sub(int x, int y)
11 {
12 return x - y;
13 }
14 extern "C" __declspec(dllexport) int Multiply(int x, int y)
15 {
16 return x * y;
17 }
18 extern "C" __declspec(dllexport) int Divide(int x, int y)
19 {
20 return x / y;
21 }

extern "C" 包含雙重含義,從字面上即可得到:首先,被它修飾的目標是“extern”的;其次,被它修飾的目標是“C”的。而被extern "C"修飾的變量和函數是按照C語言方式編譯和連接的。

__declspec(dllexport)的目的是為了將對應的函數放入到DLL動態庫中。

extern "C" __declspec(dllexport)加起來的目的是為了使用DllImport調用非托管C++的DLL文件。因為使用DllImport只能調用由C語言函數做成的DLL。

5. 編譯項目程序,最後在Debug目錄生成CSharpInvokeCPP.CPPDemo.dll和CSharpInvokeCPP.CPPDemo.lib

技術分享

我們用反編譯工具PE Explorer查看下該DLL裏面的方法:

技術分享

可以發現對外的公共函數上包含這四種“加減乘除”方法。

6. 現在來演示下如何利用C#項目來調用非托管C++的DLL,首先創建C#控制臺應用程序:

技術分享

7. 在CSharpInvokeCSharp.CSharpDemo項目上新建一個CPPDLL類,編寫以下代碼:


1 public class CPPDLL
2 {
3 [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
4 public static extern int Add(int x, int y);
5
6 [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
7 public static extern int Sub(int x, int y);
8
9 [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
10 public static extern int Multiply(int x, int y);
11
12 [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
13 public static extern int Divide(int x, int y);
14 }

DllImport作為C#中對C++的DLL類的導入入口特征,並通過static extern對extern “C”進行對應。

8. 另外,記得把CPPDemo中生成的DLL文件拷貝到CSharpDemo的bin目錄下,你也可以通過設置【項目屬性】->【配置屬性】->【常規】中的輸出目錄:

技術分享

這樣編譯項目後,生成的文件就自動輸出到CSharpDemo中了。

9. 然後在Main入口編寫測試代碼:


1 static void Main(string[] args)
2 {
3 int result = CPPDLL.Add(10, 20);
4 Console.WriteLine("10 + 20 = {0}", result);
5
6 result = CPPDLL.Sub(30, 12);
7 Console.WriteLine("30 - 12 = {0}", result);
8
9 result = CPPDLL.Multiply(5, 4);
10 Console.WriteLine("5 * 4 = {0}", result);
11
12 result = CPPDLL.Divide(30, 5);
13 Console.WriteLine("30 / 5 = {0}", result);
14
15 Console.ReadLine();
16 }

運行結果:

技術分享

方法得到調用。

10. 以上的方法只能通過靜態方法對於C++中的函數進行調用。那麽怎樣通過靜態方法去調用C++中一個類對象中的方法呢?現在我在CPPDemo項目中添加一個頭文件userinfo.h:


1 class UserInfo {
2 private:
3 char* m_Name;
4 int m_Age;
5 public:
6 UserInfo(char* name, int age)
7 {
8 m_Name = name;
9 m_Age = age;
10 }
11 virtual ~UserInfo(){ }
12 int GetAge() { return m_Age; }
13 char* GetName() { return m_Name; }
14 };

在CSharpInvokeCPP.CPPDemo.cpp中,添加一些代碼:


1#include "malloc.h"
2#include "userinfo.h"
3
4typedef struct {
5 char name[32];
6 int age;
7} User;
8
9UserInfo* userInfo;
10
11extern "C" __declspec(dllexport) User* Create(char* name, int age)
12{
13 User* user = (User*)malloc(sizeof(User));
14
15 userInfo = new UserInfo(name, age);
16 strcpy(user->name, userInfo->GetName());
17 user->age = userInfo->GetAge();
18
19 return user;
20}

這裏聲明一個結構,包括name和age,這個結構是用於和C#方面的結構作個映射。

註意:代碼中的User*是個指針,返回也是一個對象指針,這樣做為了防止方法作用域結束後的局部變量的釋放。

strcpy是個復制char數組的函數。

11. 在CSharpDemo項目中CPPDLL類中補充代碼:


1 [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
2 public static extern IntPtr Create(string name, int age);
3
4 [StructLayout(LayoutKind.Sequential)]
5 public struct User
6 {
7 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
8 public string Name;
9
10 public int Age;
11 }

其中這裏的結構User就和C++中的User對應。

12. 在Program.cs中補充代碼:


1 IntPtr ptr = CPPDLL.Create("李平", 27);
2 CPPDLL.User user =(CPPDLL.User)Marshal.PtrToStructure(ptr, typeof(CPPDLL.User));
3 Console.WriteLine("Name: {0}, Age: {1}", user.Name, user.Age);

註意:這裏結構指針首先轉換成IntPtr句柄,然後通過Marshal.PtrToStructrue轉換成你所需要的結構。

運行結果:

技術分享

C#調用c++類的導出函數