在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)

在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)

原文链接:

http://www.cnblogs.com/liping13599168/archive/2011/03/31/2000320.html


背景 

     在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用。本篇文章将引导你快速理解这个调用的过程。

 

步骤

1. 创建一个CSharpInvokeCPP的解决方案:

image

 

2. 创建一个C++的动态库项目:

image

 

3. 在应用程序设置中,选择“DLL”,其他按照默认选项:

image

最后点击完成,得到如图所示项目:

image

      我们可以看到这里有一些文件,其中dllmain.cpp作为定义DLL应用程序的入口点,它的作用跟exe文件有个main或者WinMain入口函数是一样的,它就是作为DLL的一个入口函数,实际上它是个可选的文件。它是在静态链接时或动态链接时调用LoadLibrary和FreeLibrary时都会被调用。详细内容可以参考(http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx)。

 

4. 现在我们打开CSharpInvokeCPP.CPPDemo.cpp文件:

现在我们加入以下内容:

01// CSharpInvokeCPP.CPPDemo.cpp : 定义 DLL 应用程序的导出函数。
02//
03 
04#include "stdafx.h"
05 
06extern "C" __declspec(dllexport)int Add(intx, int y)
07{
08    returnx + y;
09}
10extern "C" __declspec(dllexport)int Sub(intx, int y)
11{
12    returnx - y;
13}
14extern "C" __declspec(dllexport)int Multiply(intx, int y)
15{
16    returnx * y;
17}
18extern "C" __declspec(dllexport)int Divide(intx, int y)
19{
20    returnx / 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

image

我们用反编译工具PE Explorer查看下该DLL里面的方法:

image

可以发现对外的公共函数上包含这四种“加减乘除”方法。

 

6. 现在来演示下如何利用C#项目来调用非托管C++的DLL,首先创建C#控制台应用程序:

image

 

7. 在CSharpInvokeCSharp.CSharpDemo项目上新建一个CPPDLL类,编写以下代码:

01public classCPPDLL
02{
03    [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
04    publicstatic extern int Add(int x, int y);
05 
06    [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
07    publicstatic extern int Sub(int x, int y);
08 
09    [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
10    publicstatic extern int Multiply(int x, int y);
11 
12    [DllImport("CSharpInvokeCPP.CPPDemo.dll")]
13    publicstatic extern int Divide(int x, int y);
14}

DllImport作为C#中对C++的DLL类的导入入口特征,并通过static extern对extern “C”进行对应。

 

8. 另外,记得把CPPDemo中生成的DLL文件拷贝到CSharpDemo的bin目录下,你也可以通过设置【项目属性】->【配置属性】->【常规】中的输出目录:

image

这样编译项目后,生成的文件就自动输出到CSharpDemo中了。

 

9. 然后在Main入口编写测试代码:

01static voidMain(string[] args)
02{
03    intresult = CPPDLL.Add(10, 20);
04    Console.WriteLine("10 + 20 = {0}", result);
05 
06    result = CPPDLL.Sub(30, 12);
07    Console.WriteLine("30 - 12 = {0}", result);
08 
09    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}

 

运行结果:

image

方法得到调用。

10. 以上的方法只能通过静态方法对于C++中的函数进行调用。那么怎样通过静态方法去调用C++中一个类对象中的方法呢?现在我在CPPDemo项目中添加一个头文件userinfo.h:

01class UserInfo {
02private:
03    char* m_Name;
04    intm_Age;
05public:
06    UserInfo(char* name,int age)
07    {
08        m_Name = name;
09        m_Age = age;
10    }
11    virtual~UserInfo(){ }
12    intGetAge() { return m_Age; }
13    char* GetName() {return m_Name; }
14};

 

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

01#include "malloc.h"
02#include "userinfo.h"
03 
04typedef struct {
05    charname[32];
06    intage;
07} User; 
08 
09UserInfo* 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    returnuser;
20}

这里声明一个结构,包括name和age,这个结构是用于和C#方面的结构作个映射。

注意:代码中的User*是个指针,返回也是一个对象指针,这样做为了防止方法作用域结束后的局部变量的释放。

strcpy是个复制char数组的函数。

 

11. 在CSharpDemo项目中CPPDLL类中补充代码:

01[DllImport("CSharpInvokeCPP.CPPDemo.dll")]
02public staticextern IntPtr Create(stringname, int age);
03 
04[StructLayout(LayoutKind.Sequential)]
05public structUser
06{
07    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
08    publicstring Name;
09 
10    publicint Age;
11}

 

其中这里的结构User就和C++中的User对应。

 

12. 在Program.cs中补充代码:

1IntPtr ptr = CPPDLL.Create("李平", 27);
2<strong><font color="#ff0000">CPPDLL.User user = (CPPDLL.User)Marshal.PtrToStructure(ptr,typeof(CPPDLL.User));</font></strong>
3Console.WriteLine("Name: {0}, Age: {1}", user.Name, user.Age);

 

注意:红色字体部分,这里结构指针首先转换成IntPtr句柄,然后通过Marshal.PtrToStructrue转换成你所需要的结构。

 

运行结果:

image

 

最后附上我的源代码:CSharpInvokeCPP.rar,希望对大家有所帮助:)

作者: Leepy
 
邮箱:sunleepy(AT)gmail.com
 
    
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值