Visual Studio 2010調(diào)用非C#編寫的DLL文件
背景
在項目過程中,有時候你需要調(diào)用非C#編寫的DLL文件,尤其在使用一些第三方通訊組件的時候,通過C#來開發(fā)應(yīng)用軟件時,就需要利用DllImport特性進行方法調(diào)用。本篇文章將引導(dǎo)你快速理解這個調(diào)用的過程。
步驟
1. 創(chuàng)建一個CSharpInvokeCPP的解決方案:
2. 創(chuàng)建一個C++的動態(tài)庫項目:
3. 在應(yīng)用程序設(shè)置中,選擇“DLL”,其他按照默認(rèn)選項:
***點擊完成,得到如圖所示項目:
我們可以看到這里有一些文件,其中dllmain.cpp作為定義DLL應(yīng)用程序的入口點,它的作用跟exe文件有個main或者WinMain入口函數(shù)是一樣的,它就是作為DLL的一個入口函數(shù),實際上它是個可選的文件。它是在靜態(tài)鏈接時或動態(tài)鏈接時調(diào)用LoadLibrary和FreeLibrary時都會被調(diào)用。詳細(xì)內(nèi)容可以參考(http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx)。
4. 現(xiàn)在我們打開CSharpInvokeCPP.CPPDemo.cpp文件:
現(xiàn)在我們加入以下內(nèi)容:
- // CSharpInvokeCPP.CPPDemo.cpp : 定義 DLL 應(yīng)用程序的導(dǎo)出函數(shù)。
 - //
 - #include "stdafx.h"
 - extern "C" __declspec(dllexport) int Add(int x, int y)
 - {
 - return x + y;
 - }
 - extern "C" __declspec(dllexport) int Sub(int x, int y)
 - {
 - return x - y;
 - }
 - extern "C" __declspec(dllexport) int Multiply(int x, int y)
 - {
 - return x * y;
 - }
 - extern "C" __declspec(dllexport) int Divide(int x, int y)
 - {
 - return x / y;
 - }
 
extern "C" 包含雙重含義,從字面上即可得到:首先,被它修飾的目標(biāo)是“extern”的;其次,被它修飾的目標(biāo)是“C”的。而被extern "C"修飾的變量和函數(shù)是按照C語言方式編譯和連接的。
__declspec(dllexport)的目的是為了將對應(yīng)的函數(shù)放入到DLL動態(tài)庫中。
extern "C" __declspec(dllexport)加起來的目的是為了使用DllImport調(diào)用非托管C++的DLL文件。因為使用DllImport只能調(diào)用由C語言函數(shù)做成的DLL。
5. 編譯項目程序,***在Debug目錄生成CSharpInvokeCPP.CPPDemo.dll和CSharpInvokeCPP.CPPDemo.lib
我們用反編譯工具PE Explorer查看下該DLL里面的方法:
可以發(fā)現(xiàn)對外的公共函數(shù)上包含這四種“加減乘除”方法。
6. 現(xiàn)在來演示下如何利用C#項目來調(diào)用非托管C++的DLL,首先創(chuàng)建C#控制臺應(yīng)用程序:
7. 在CSharpInvokeCSharp.CSharpDemo項目上新建一個CPPDLL類,編寫以下代碼:
- public class CPPDLL
 - {
 - [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
 - public static extern int Add(int x, int y);
 - [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
 - public static extern int Sub(int x, int y);
 - [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
 - public static extern int Multiply(int x, int y);
 - [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
 - public static extern int Divide(int x, int y);
 - }
 
DllImport作為C#中對C++的DLL類的導(dǎo)入入口特征,并通過static extern對extern “C”進行對應(yīng)。
8. 另外,記得把CPPDemo中生成的DLL文件拷貝到CSharpDemo的bin目錄下,你也可以通過設(shè)置【項目屬性】->【配置屬性】->【常規(guī)】中的輸出目錄:
這樣編譯項目后,生成的文件就自動輸出到CSharpDemo中了。
9. 然后在Main入口編寫測試代碼:
- static void Main(string[] args)
 - {
 - int result = CPPDLL.Add(10, 20);
 - Console.WriteLine("10 + 20 = {0}", result);
 - result = CPPDLL.Sub(30, 12);
 - Console.WriteLine("30 - 12 = {0}", result);
 - result = CPPDLL.Multiply(5, 4);
 - Console.WriteLine("5 * 4 = {0}", result);
 - result = CPPDLL.Divide(30, 5);
 - Console.WriteLine("30 / 5 = {0}", result);
 - Console.ReadLine();
 - }
 
運行結(jié)果:
方法得到調(diào)用。 
 
10. 以上的方法只能通過靜態(tài)方法對于C++中的函數(shù)進行調(diào)用。那么怎樣通過靜態(tài)方法去調(diào)用C++中一個類對象中的方法呢?現(xiàn)在我在CPPDemo項目中添加一個頭文件userinfo.h:
- class UserInfo {
 - private:
 - char* m_Name;
 - int m_Age;
 - public:
 - UserInfo(char* name, int age)
 - {
 - m_Name = name;
 - m_Age = age;
 - }
 - virtual ~UserInfo(){ }
 - int GetAge() { return m_Age; }
 - char* GetName() { return m_Name; }
 - };
 
在CSharpInvokeCPP.CPPDemo.cpp中,添加一些代碼:
- #include "malloc.h"
 - #include "userinfo.h"
 - typedef struct {
 - char name[32];
 - int age;
 - } User;
 - UserInfo* userInfo;
 - extern "C" __declspec(dllexport) User* Create(char* name, int age)
 - {
 - User* user = (User*)malloc(sizeof(User));
 - userInfo = new UserInfo(name, age);
 - strcpy(user->name, userInfo->GetName());
 - user->age = userInfo->GetAge();
 - return user;
 - }
 
這里聲明一個結(jié)構(gòu),包括name和age,這個結(jié)構(gòu)是用于和C#方面的結(jié)構(gòu)作個映射。
注意:代碼中的User*是個指針,返回也是一個對象指針,這樣做為了防止方法作用域結(jié)束后的局部變量的釋放。
strcpy是個復(fù)制char數(shù)組的函數(shù)。
11. 在CSharpDemo項目中CPPDLL類中補充代碼:
- [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
 - public static extern IntPtr Create(string name, int age);
 - [StructLayout(LayoutKind.Sequential)]
 - public struct User
 - {
 - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
 - public string Name;
 - public int Age;
 - }
 
其中這里的結(jié)構(gòu)User就和C++中的User對應(yīng)。
12. 在Program.cs中補充代碼:
- IntPtr ptr = CPPDLL.Create("李平", 27);
 - <STRONG><FONT color=#ff0000>CPPDLL.User user = (CPPDLL.User)Marshal.PtrToStructure(ptr, typeof(CPPDLL.User));</FONT></STRONG>
 - Console.WriteLine("Name: {0}, Age: {1}", user.Name, user.Age);
 
注意:紅色字體部分,這里結(jié)構(gòu)指針首先轉(zhuǎn)換成IntPtr句柄,然后通過Marshal.PtrToStructrue轉(zhuǎn)換成你所需要的結(jié)構(gòu)。
運行結(jié)果:
原文鏈接:http://www.cnblogs.com/liping13599168/archive/2011/03/31/2000320.html
【編輯推薦】

























 
 
 
 
 
 
 