分享
 
 
 

使用“win32 static library”封装类的内部实现

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

本人在开发过程中经常使用第三方改写的界面类,添加这些类以后,整个工程结构比较复杂,给维护增加了难度,所以一直想把这些第三方的类封装起来,生成一个静态连接库,这样,在工程中就少了很多文件,整个工程结构变得比较清晰,在网上查找了一些资料,又研究了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.aspExporting 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 #defines 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 #defines 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.)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有