使用“win32 static library”封装类的内部实现
来源:互联网 发布:tagsinput.js 参数 编辑:程序博客网 时间:2024/05/22 00:45
本人在开发过程中经常使用第三方改写的界面类,添加这些类以后,整个工程结构比较复杂,给维护增加了难度,所以一直想把这些第三方的类封装起来,生成一个静态连接库,这样,在工程中就少了很多文件,整个工程结构变得比较清晰,在网上查找了一些资料,又研究了CJlibrary的源代码,终于实现了自己的目的,下面就是一个例子的实现过程。
例子目的要把一个CListCtrl改写的继承类CXListCtrl封装起来;
创建lib库
第一步,创建一个空的“Win32 Static Library”工程;
第二步,添加文件,选择菜单“Project”->“Add To Project”->“Files”,选择CXListCtrl.cpp和CXListCtrl.h文件,添加到工程中,修改CXListCtrl.h的类定义,把
class CXListCtrl : public CListCtrl修改为class AFX_CLASS_EXPORT CXListCtrl : public CListCtrl;
第三步,然后新建一个头文件stdafx.h,把CXListCtrl类需要的mfc头文件加入到stdafx.h中,本例stdafx.h文件内容如下,
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h> // MFC OLE classes
#include <afxodlgs.h> // MFC OLE dialog classes
#include <afxdisp.h> // MFC OLE automation classes
#endif // _AFX_NO_OLE_SUPPORT
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#pragma warning(disable : 4251) //去掉4251的警告
#pragma warning(disable : 4275) //去掉4275的警告
第四步,设置win32 Release工程参数,“Project”->“Setting”->“General”->“Microsoft Foundation Classes”选择“Not Use MFC”,在“Setting”->“C/C++”->“Preprocessor definitions”填入“NDEBUG,WIN32,_WINDOWS”
第五步,编译生成Release版本的“lib”文件
调用过程
第一步,创建一个Dialog工程;
第二步,把CXListCtrl.h头文件使用上面的方式加入到工程中能够,然后可以编写使用CXListCtrl类的代码;
第三步,把生成的lib文件拷入到Dialog工程目录下,修改stdafx.h文件,在文件后面加入
#pragma comment(lib, "xxx.lib") //生成的lib文件
第四步,使用win32 Release方式编译,编译通过,调用成功!
下面是一篇转贴的相关文章:
出处:http://www.codeproject.com/dll/dllexport.asp
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.)
- 使用“win32 static library”封装类的内部实现
- 使用MFC Extension Dll 封装界面类的内部实现
- win32平台下malloc的内部实现
- win32平台下malloc的内部实现
- win32平台下malloc的内部实现
- 封装自定义控件的sdk(创建静态库static library)
- Android App内部自动更新Library的使用
- create and use win32 static library
- 类内部的封装性
- VC++6.0创建一个含gdiplus的Win32 Static Library工程编译时报错解决办法
- 类内部的 static、const、enum 变量
- c++ 类 函数内部的 static 类型
- EF AES Library 的封装类
- 关于win32的C++ 类封装
- 好用的win32线程封装类
- iOS (static library)静态库的创建与使用
- 封装一个Win32弹出提示框的函数(用于STATIC等控件显示Tooltip)
- VC win32 static library静态链接库简单示例
- CRM方法
- tomcat下配置数据库连接池
- 制作Javascript弹出窗口技巧九则
- 一个猜数字游戏的算法
- 使用VML画圆角表格
- 使用“win32 static library”封装类的内部实现
- 如何增强烟花照片效果
- 网络设计师复习知识点一:ATM网络
- PHOTOSHOP给MM照片上色
- PHOTOSHOP CS打造素描MM
- 转学习:窗口对话框等
- MySQL中各种字段的取值范围
- 一个功能强大的JAVA格状布局管理器
- Bill Gates 的大學畢業典禮演講(转自候捷主页)