📄 05o009.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>
<body>
<div align="justify">
<table border="0" width="100%" class="font" height="57">
<tr>
<td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">VC知识库(五)</font>
</td>
<td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">www.vckbase.com</font>
</td>
</tr>
<tr>
<td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
<small>
<p align="center"><big>在VC中导出C++类</big></p></small>
</td>
</tr>
<tr>
<td width="100%" height="17" class="info" align="center" colspan="2">
<small><p align="center">闻怡洋译 wyy_cq@21cn.com <a href="http://www.vchelp.com">http://www.vchelp.com</a></p>
</small>
</td>
</tr>
<tr>
<td width="100%" height="22" class="font" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="5" class="font" colspan="2">
<p><font size="2">如何在VC中导出类,这是一个常有人问起的问题,下面我以一个简单的例子来说明这个问题:</font>
</p>
<p><font size="2">首先使用Wizard创建一个Win32 Dynamic-Link Library工程,然后定义一个简单的C++类CInDLL。由于该类会被工程之外的文件所引用,所以需要对这个类进行引出。因为只有引出后所生成的DLL中才带有供足够的信息以在连接和运行时被正确引入到进程空间中。有两种方法可以引出类,使用__declspec(dllexport)定义和使用定义文件。</font>
</p>
<p><font size="2">下面先讲使用__declspec(dllexport)的方法:将类定义改为:class __declspec(dllexport) CInDLL 就可以了。(译者:你也许不相信会有这么简单,我也不相信。:-) 这样产生的工程在编译时是正确的但是在使用时会产生错误,因为你包含的头文件中也是使用__declspec(dllexport),而使用这个DLL的工程中并没有引出这个类,而是需要引入这个类)在使用时需要将类定义改为class __declspec(dllimport) CInDLL就可以了。</font>
</p>
<p><font size="2">使用定义文件可以有效的避免这个问题,这种方法是利用宏定义在不同的地方产生不同的编译代码:在头文件中加入如下的代码:</font><pre><font size="2">
#ifdef _CLASSINDLL
#define CLASSINDLL_CLASS_DECL __declspec(dllexport)
#else
#define CLASSINDLL_CLASS_DECL __declspec(dllimport)
#endif
#endif // ClassInDLL_H
//将class __declspec(dllexport) CInDLL改为
class CLASSINDLL_CLASS_DECL CInDLL
{
CInDLL();
...
}
</font></pre>
<font size="2">
在实现这个类的CPP文件的顶部加入#define _CLASSINDLL语句。</font>
<pre><font size="2">
#define _CLASSINDLL
CInDLL::CInDLL()
{
}
...
</font></pre>
<p><font size="2">这样一来在使用这个类时就可以不做任何改动了。<a href=ftp://ftp.iftech.com/DevJournal/ao_dll/ClassInDll.zip>DownLoad Demo Source</a></font></p>
<p><font size="2">(译者:这中方法在没有使用MFC时可以使用,如果你使用MFC生成MFC DLL那么只要做如下定义就可以了class AFX_EXT_CLASS yourClass)</font></p>
</td>
</tr>
<tr>
<td width="100%" height="12" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="6" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="8" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="17" class="font" colspan="2"></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -