背景
在項目過程中,有時候你需要調用非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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// CSharpInvokeCPP.CPPDemo.cpp : 定義 DLL 應用程序的導出函數。
//
#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" 包含雙重含義,從字面上即可得到:首先,被它修飾的目標是“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
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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類的導入入口特征,並通過static extern對extern “C”進行對應。
8. 另外,記得把CPPDemo中生成的DLL文件拷貝到CSharpDemo的bin目錄下,你也可以通過設置【項目屬性】->【配置屬性】->【常規】中的輸出目錄:
這樣編譯項目后,生成的文件就自動輸出到CSharpDemo中了。
9. 然后在Main入口編寫測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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();
}
|
運行結果:
方法得到調用。
10. 以上的方法只能通過靜態方法對於C++中的函數進行調用。那么怎樣通過靜態方法去調用C++中一個類對象中的方法呢?現在我在CPPDemo項目中添加一個頭文件userinfo.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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中,添加一些代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#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;
}
|
這里聲明一個結構,包括name和age,這個結構是用於和C#方面的結構作個映射。
注意:代碼中的User*是個指針,返回也是一個對象指針,這樣做為了防止方法作用域結束后的局部變量的釋放。
strcpy是個復制char數組的函數。
11. 在CSharpDemo項目中CPPDLL類中補充代碼:
1
2
3
4
5
6
7
8
9
10
11
|
[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;
}
|
其中這里的結構User就和C++中的User對應。
12. 在Program.cs中補充代碼:
IntPtr ptr = CPPDLL.Create("李平", 27); CPPDLL.User user = (CPPDLL.User)Marshal.PtrToStructure(ptr, typeof(CPPDLL.User)); Console.WriteLine("Name: {0}, Age: {1}", user.Name, user.Age);
注意:紅色字體部分,這里結構指針首先轉換成IntPtr句柄,然后通過Marshal.PtrToStructrue轉換成你所需要的結構。
運行結果:
最后附上我的源代碼:CSharpInvokeCPP.rar,希望對大家有所幫助:)
出處:http://www.cnblogs.com/liping13599168/archive/2011/03/31/2000320.html