用DLL链接MFC项目 [英] link a MFC project with a dll

查看:77
本文介绍了用DLL链接MFC项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我创建了一个简单的MFC应用程序.而且我还创建了一个新的dll.

现在,我想从我创建的MFC应用程序链接/创建dll的对象.

这怎么可能?

执行此操作的步骤将非常有用.

谢谢,
Skunkhead:)

Hi guys,

I have created a simple MFC Application. And I also have created a new dll.

Now i want to link/create the object of the dll from the MFC application which I create.

How is this possible?

Steps to do this will be much appricated.

Thanks,
Skunkhead :)

推荐答案

Santosh的答案是正确的(期望extern:如果您从dll项目中提供.h文件,则没有必要). br/>
我将介绍有关静态链接dll的更多信息:

首先,您应该在dll项目中正确声明函数.如果您的项目中尚不存在此文件,建议您添加一个具有以下定义的文件(例如 ImportExport.h ):

Santosh''s answer is correct (expect for extern: it is not necessary if you provide the .h files from your dll project).

I will just explain a little bit more about statically linking the dll:

First you should declare your functions properly in your dll project. If this doesn''t already exist in your project, I suggest you add a file (for example ImportExport.h) with the following defines:

#ifdef YOURDLL_EXPORTS
#define YOURDLL_API __declspec(dllexport)
#else
#define YOURDLL_API __declspec(dllimport)
#endif



然后转到项目属性 C/C ++ 预处理器,在预处理器定义中,将YOURDLL_EXPORTS添加到列表中.对所有配置都执行此操作.

现在在您的dll项目中,使用YOURDLL_API修改所有的类和函数定义以导出它们(您需要包含 ImportExport.h :



And go to your project properties, C/C++, Preprocessor, in the Preprocessor Definitions, add YOURDLL_EXPORTS to the list. Do that for all configurations.

Now inside your dll project, modify all your class and functions definitions using YOURDLL_API to export them (you need to include ImportExport.h:

class YOURDLL_API MyClass { ... };
YOURDLL_API void MyFunction();



ImportExport.h 技术被广泛使用,并使维护更加容易.

之后,编译后将生成2个重要文件:
- .dll 文件(运行时需要)
- .lib 文件(使用dll的应用程序在编译时需要)

在继续操作之前,请确保您具有.lib文件.如果找不到,请检查项目属性.您可以在链接器高级导入库下找到其路径.

编译后,您可以使用非常有用的工具来确保所有导出均正常: http://www.dependencywalker.com/ [ ^ ]
使用该工具检查所有函数/类是否已正确导出.

现在,要在其他项目中使用dll:
-要进行编码,请使用dll项目中必要的 .h 文件来编码所需的任何内容
-不要在您的项目中定义 YOURDLL_EXPORTS ,因此编译器会自动导入这些函数(因此我们不需要将这些函数声明为extern). br/> -要进行项目编译,您需要将 .lib 文件提供给编译器.转到projet属性,链接器输入,然后将.lib文件添加到其他依赖项字段中.不要忘记对所有配置(Debug,Release等)执行此操作.

要运行该应用程序,请不要忘记将dll复制到最终exe旁边.



The ImportExport.h technic is widely used and makes maintenance much easier.

After that, after compilation, 2 important files will be produced:
- the .dll file (needed at runtime)
- the .lib file (needed at compilation time by the application that will use the dll)

Make sure you have the .lib file before continuing. If you can''t find it, check your project properties. You can find its path under, Linker, Advanced, Import Library.

After compilation, you can make sure all exports are OK with a very useful tool: http://www.dependencywalker.com/[^]
Use that tool to check that all your functions/classes are properly exported.

Now, to use the dll in your other projects:
- for coding, use the necessary .h files from your dll projects to code whatever you want
- DO NOT define YOURDLL_EXPORTS in your project, so the compile will import these functions automatically (so we don''t need to declare the functions as extern).
- to make project compile, you need to give the .lib file to the compiler. Go to the projet properties, Linker, Input, and add your .lib file in the Additional Dependencies field. Don''t forget to do that for all configurations (Debug, Release, ...).

To run the application, don''t forget to copy the dll next to the final exe.


如果您使用
选择功能dll,首先要注意的是 __declspec(dllimport)
然后只需定义函数extern并在mfc项目中调用该函数即可.
如果您为基于对话框的dll应用创建了函数,则
阅读我的答案
DLL对话框 [ ^ ]

有关dll链接的更多信息,请阅读给出的信息
MFC .DLL [
First thing if you cerate function dll using
__declspec(dllimport)
then just define function extern and called the function in mfc project.
if you created function for dialog based dll appilcation then
read my answer
DLL Dialog[^]

and for more information about dll linking please read the information given
MFC .DLL [^]


As you say you want call function then
Its so simple if you used
__declspec(dllimport)


只需定义dll函数外部,并在链接器的命令行上仅提供dll .lib文件名
在dll项目中


just define dll function extern and on command line of linker just provide dll .lib file name
in dll project


这篇关于用DLL链接MFC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆