在WPF中,使用C++编写的DLL文件
2014-10-08 14:31
260 查看
文章来源:/article/1621505.html
WPF运行在CLR上的,它的代码是托管代码。
C++编写的DLL代码是非托管的。
在WPF中调用C++编写的DLL代码需要使用:
[csharp] view
plaincopy
using System.Runtime.InteropServices;
[DllImport("Dll.dll", EntryPoint = "add",CallingConvention=CallingConvention.Cdecl)]
public static extern int add(int a, int b);
下面详细说明之。
在Visual Studio 2010中,File --> New --> Project --> Visual C++ --> Win32 --> Win32 Project
比如我们为工程起名为Dll,在弹出的对话框中,选择DLL,点击Finish即可。
在Dll.cpp中添加代码
[cpp] view
plaincopy
#include "stdafx.h"
int add(int a,int b)
{
returna+b;
}
为了导出时不发生名字改编,我们添加一个模块定义文件Dll.def,方法是右击工程名àAddàNew Item,在弹出的对话框中,选择Module-Difinition File(.def),文件名为Dll.def。
在Dll.def中添加代码
[cpp] view
plaincopy
LIBRARY Dll.dll
EXPORTS
add
Build工程即可。在Debug目录下,会有Dll.dll文件生成。
【注】关于C++ DLL 这一部分,可以参考我的前期博文。
新建一个WPF工程。
将Dll.dll文件拷贝到WPF工程的Debug目录下。
[csharp] view
plaincopy
//一些其他的命名空间
using System.Runtime.InteropServices;
namespace Wpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
[DllImport("Dll.dll", EntryPoint = "add",CallingConvention=CallingConvention.Cdecl)]
public static extern int add(int a, int b);
public MainWindow()
{
InitializeComponent();
inta = MainWindow.add(12,12);
MessageBox.Show(a.ToString());
}
}
}
1. Dll.dll一定要拷贝到WPF工程的Debug目录下
2. 一定要注意堆栈的调用约定
Visual C++ 代码的默认调用约定是C调用约定(__cdecl)
而不是标准调用约定(__stdcall)或Pascal调用约定(__pascal)
所以在DllImport中,CallingConvention参数一定要设置成CallingConvention.Cdecl。
当然,我们也可以通过修改Dll.dll的调用约定,如
[cpp] view
plaincopy
int WINAPI add(int a,int b)
将add函数的调用约定设置为WINAPI,也就是标准调用约定,
这样,在WPF中引入时,DllImport的CallingConvention参数就可以省略不写,因为默认是标准调用约定
[csharp] view
plaincopy
[DllImport("Dll.dll", EntryPoint = "add"]
WPF运行在CLR上的,它的代码是托管代码。
C++编写的DLL代码是非托管的。
在WPF中调用C++编写的DLL代码需要使用:
[csharp] view
plaincopy
using System.Runtime.InteropServices;
[DllImport("Dll.dll", EntryPoint = "add",CallingConvention=CallingConvention.Cdecl)]
public static extern int add(int a, int b);
下面详细说明之。
编译生成DLL文件
在Visual Studio 2010中,File --> New --> Project --> Visual C++ --> Win32 --> Win32 Project比如我们为工程起名为Dll,在弹出的对话框中,选择DLL,点击Finish即可。
在Dll.cpp中添加代码
[cpp] view
plaincopy
#include "stdafx.h"
int add(int a,int b)
{
returna+b;
}
为了导出时不发生名字改编,我们添加一个模块定义文件Dll.def,方法是右击工程名àAddàNew Item,在弹出的对话框中,选择Module-Difinition File(.def),文件名为Dll.def。
在Dll.def中添加代码
[cpp] view
plaincopy
LIBRARY Dll.dll
EXPORTS
add
Build工程即可。在Debug目录下,会有Dll.dll文件生成。
【注】关于C++ DLL 这一部分,可以参考我的前期博文。
在WPF中使用DLL
新建一个WPF工程。将Dll.dll文件拷贝到WPF工程的Debug目录下。
[csharp] view
plaincopy
//一些其他的命名空间
using System.Runtime.InteropServices;
namespace Wpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
[DllImport("Dll.dll", EntryPoint = "add",CallingConvention=CallingConvention.Cdecl)]
public static extern int add(int a, int b);
public MainWindow()
{
InitializeComponent();
inta = MainWindow.add(12,12);
MessageBox.Show(a.ToString());
}
}
}
注意事项
1. Dll.dll一定要拷贝到WPF工程的Debug目录下2. 一定要注意堆栈的调用约定
Visual C++ 代码的默认调用约定是C调用约定(__cdecl)
而不是标准调用约定(__stdcall)或Pascal调用约定(__pascal)
所以在DllImport中,CallingConvention参数一定要设置成CallingConvention.Cdecl。
当然,我们也可以通过修改Dll.dll的调用约定,如
[cpp] view
plaincopy
int WINAPI add(int a,int b)
将add函数的调用约定设置为WINAPI,也就是标准调用约定,
这样,在WPF中引入时,DllImport的CallingConvention参数就可以省略不写,因为默认是标准调用约定
[csharp] view
plaincopy
[DllImport("Dll.dll", EntryPoint = "add"]
相关文章推荐
- 在 WPF 中,使用 C++ 编写的 DLL 文件
- Linux下,使用C/C++编写"静态链接库"的一个简单例子
- Linux下,使用C/C++编写一个简单的消息处理程序
- 使用 C++ 编写内核模式驱动程序的优点与缺点
- 如何使用C++编写CGI程序接口
- 使用 C++ 编写内核模式驱动程序的优点与缺点
- 使用C++编写类似MFC的CString,部分功能,重新复习使用操作符重载
- Silverlight实用窍门系列:22.Silverlight使用WebService调用C++,Delphi编写的DLL文件【实例源码下载】
- 【转帖】使用 C++ 编写内核模式驱动程序的优点与缺点
- 使用c++开发excel插件(第4章编写一个完整的xll)
- 用C++编写的使用嵌套循环的程序
- [C++]实验一:使用VC++6.0环境编写C++程序
- 使用C++编写web services
- 使用C++编写Apache的模块 -- 命令表,保存配置信息
- 如何使用C++制作WPF项目
- Linux下,使用C/C++编写的一个简单的信号处理例程
- 综合应用WPF/WCF/WF/LINQ之二十四:使用UserControl技术编写一个翻页控件
- 在C#中使用C++编写的类
- Windows下使用Eclipse+CDT编写C/C++程序
- 用C++编写一个使用嵌套循环的程序