VC++ MFC Tutorial: Create MFC Extension Dll, Export MFC Classes
来源:互联网 发布:软件分发平台 编辑:程序博客网 时间:2024/05/19 18:42
VC++ MFC Tutorial: Create MFC Extension Dll, Export MFC Classes
By Steve Driessens.How to simplify importing and exporting classes from an extension DLL
Exporting C++ classes from extension DLLs and importing those classes into applications can be a little confusing at times. This article discusses one of many ways to simplify this. Also discussed is a technique to ensure that your DLL's.LIB file is automatically linked into any application (or other DLL) using your DLL, avoiding the need to alter your project link settings.
When building an extension DLL, you want the compiler/linker to export selected C++ classes, but when building your application you want to import those classes.
Traditionally, this has been done by using the AFX_CLASS_EXPORT
and AFX_CLASS_IMPORT
defines (defined in afxv_dll.h). Swapping these #define
s in and out depending on whether you're building the DLL itself or building an application (or another DLL) which uses your exported classes.
If we look at how AFX_CLASS_EXPORT
and AFX_CLASS_IMPORT
are defined in afxv_dll.h we see the following.
#define AFX_CLASS_EXPORT __declspec(dllexport)#define AFX_CLASS_IMPORT __declspec(dllimport)
So, when exporting our classes from our DLL we want the class declarations from the DLL to look like this:-
class __declspec(dllexport) CMyClass : public CObject{...}
And, when importing our C++ classes into our application we want the class declarations from the DLL to look like this:-
class __declspec(dllimport) CMyClass : public CObject{...}
OK, so here's how I do things.
In the stdafx.h
file for the export DLL, include two #define
s at the bottom of the file like this:-
#define _MYLIB_DLLAPI_#define _MYLIB_NOAUTOLIB_
Now, in the main header file for your DLL, say mylib.h
(the main 'point of entry' header for your DLL that you will include in you application later), add the following at the top:-
// The following will ensure that we are exporting our C++ classes when // building the DLL and importing the classes when build an application // using this DLL.#ifdef _MYLIB_DLLAPI_ #define MYLIB_DLLAPI __declspec( dllexport )#else #define MYLIB_DLLAPI __declspec( dllimport )#endif// The following will ensure that when building an application (or another// DLL) using this DLL, the appropriate .LIB file will automatically be used// when linking.#ifndef _MYLIB_NOAUTOLIB_#ifdef _DEBUG#pragma comment(lib, "mylibd.lib")#else#pragma comment(lib, "mylib.lib")#endif#endif
Now, just declare all the C++ classes you want exported from the DLL like this:-
(Note: Any C++ classes not declared with MYLIB_DLLAPI
will not be exported from the DLL)
class MYLIB_DLLAPI CMyClass : public CObject{...}
So, how does it work?
When building your DLL, _MYLIB_DLLAPI_
is defined in the DLL's stdafx.h
file, so MYLIB_DLLAPI
is then defined as __declspec( dllexport )
and your C++ classes will be exported.
When building your application, _MYLIB_DLLAPI_
isn't defined, so MYLIB_DLLAPI
will be defined as __declspec( dllimport )
and your classes will be imported.
The other nifty part is the _MYLIB_NOAUTOLIB_
. If _MYLIB_NOAUTOLIB_
isn't defined, (i.e. when building your application), an entry like #pragma comment(lib, "mylibd.lib")
appears which tells the linker to automatically link in your DLL's .LIB file. Hence, there's no need to add the .LIB file to the Object/library modules section in your application project link settings (something I invariable forgot to do!).
The above is basically a 'set and forget' technique. All you'll ever need to do to use you extension DLL is just include it's header in your application, and all the ugly class export/import stuff is sorted for you.
(I can't remember where I picked up this technique originally, but full credit to it's originator as it's proved invaluable over the years.)
From: http://www.ucancode.net/Visual_C_MFC_Example/MFC-Extension-Dll-Export-VC-Classes.htm
- VC++ MFC Tutorial: Create MFC Extension Dll, Export MFC Classes
- MFC--Extension DLL(扩展DLL)
- MFC--Extension DLL(扩展DLL)
- VC++钩子DLL框架代码(MFC Extension DLL using shared MFC DLL)
- Export dialogs in MFC Extension DLLs
- MFC Extension dll VS Regular Dll
- Regular DLL(MFC规则DLL)、MFC...Extension DLL(MFC扩展DLL 区别
- CJ60LIB MFC EXTENSION DLL 6.09 FINAL VERSION
- 一个MFC Extension DLL的Dialog例子
- MFC Loads Wrong Resource in Extension DLL
- vc mfc 调用C# dll
- MFC DLL——Regular DLL and Extension DLL
- MFC DLL—Regular DLL and Extension DLL
- MFC DLL——Regular DLL and Extension DLL
- mfc dll
- MFC DLL
- MFC dll
- MFC Dll
- Top journal of computer science (Media & Software Technology)
- c++ 源码 +工具+库
- ios开发 NSUserDefaults的使用
- Cassandra失效检测原理
- 重构学习总结
- VC++ MFC Tutorial: Create MFC Extension Dll, Export MFC Classes
- C++对象的内存分布和虚函数表
- 如何在excel数据透视表的顶部显示列总计
- Python多版本共存导致动态库混乱的问题
- 获利能力分析实际数据收集流程
- 使用java的html解析器jsoup和jQuery实现一个自动重复抓取任意网站页面指定元素的web应用
- JAVA中extends 与implements区别
- 使用github管理代码
- explicit构造函数