📄 222.htm
字号:
<DIV class=MenuChapter><A href="http://www.codeproject.com/Zones/IBM">IBM
DeveloperWorks</A></DIV>
<DIV class=MenuChapter><A
href="http://www.codeproject.com/Zones/WhitePapers">WhitePapers /
Webcasts</A></DIV>
<DIV class=MenuChapter><A
href="http://www.codeproject.com/Zones/Acresso">InstallShield
2009</A></DIV><BR>
<DIV style="MARGIN-BOTTOM: 10px; TEXT-ALIGN: center">
<SCRIPT type=text/javascript>document.write(unescape("%3ca%20href%3d%22http%3a%2f%2fwww.codeproject.com%2fRedir.aspx%3fadid%3d6247%26way%3dban%22%20target%3d%22_blank%22%20rel%3d%22nofollow%22%3e%3cimg%20src%3d%22http%3a%2f%2fwww.codeproject.com%2fscript%2fAdm%2fServeImg.aspx%3fFile%3d%252fscript%252fadmentor%252fimages%252fcp_daily_150x80.gif%26C%3dFalse%26adid%3d6247%22%20alt%3d%22%22%20border%3d%220%22%20width%3d%22150%22%20height%3d%2280%22%3e%3c%2fa%3e"));</SCRIPT>
</DIV>
<SCRIPT type=text/javascript>document.write(unescape("%3ca%20href%3d%22http%3a%2f%2fwww.codeproject.com%2fRedir.aspx%3fadid%3d7313%26way%3dban%22%20target%3d%22_blank%22%20rel%3d%22nofollow%22%3e%3cimg%20src%3d%22http%3a%2f%2fwww.codeproject.com%2fscript%2fAdm%2fServeImg.aspx%3fFile%3d%252fscript%252fadmentor%252fimages%252fSourceGear_160x600.png%26C%3dFalse%26adid%3d7313%22%20alt%3d%22%22%20border%3d%220%22%20width%3d%22160%22%20height%3d%22600%22%3e%3c%2fa%3e"));</SCRIPT>
</TD>
<TD class=ArticlePane vAlign=top>
<TABLE class=SearchHeaderBar cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD style="WIDTH: 60%; WHITE-SPACE: nowrap" vAlign=center
align=right><FORM style="MARGIN: 0px" name=Search
action=/info/search.aspx method=get><B>Search </B> <INPUT
class=SmallText style="WIDTH: 200px" name=artkw> <SELECT
class=SmallText style="FONT-WEIGHT: bold" name=sbo> <OPTION
value=kw selected>Articles</OPTION> <OPTION
value=fm>Messages</OPTION> <OPTION value=s>Jobs</OPTION></SELECT> <INPUT class=SmallText style="FONT-WEIGHT: bold" type=submit value=" Go! ">
</FORM></TD>
<TD class=TinyText style="WHITE-SPACE: nowrap"><A
href="http://www.codeproject.com/info/search.aspx">Advanced
Search</A><BR><A
href="http://www.codeproject.com/script/Content/SiteMap.aspx">Sitemap</A>
| <A id=ctl00_SearchBarCtrl_AddToIESearchLnk
title="Add The Code Project to your IE search Providers"
onclick="window.external.AddSearchProvider('http://www.codeproject.com/info/OpenSearch.xml');return false;"
href="http://www.codeproject.com/">Add to IE Search</A>
</TD></TR></TBODY></TABLE><SPAN id=ctl00_ResultMessage></SPAN>
<DIV id=ctl00_ArtDiv>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR vAlign=top>
<TD vAlign=top width="100%">
<TABLE>
<TBODY></TBODY></TABLE>
<DIV class=SmallText><IMG style="VERTICAL-ALIGN: middle" height=16
alt=print src="222.files/print.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_PrintLnk
href="http://www.codeproject.com/KB/DLL/SimpleDll2.aspx?display=Print">Print</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Broken Article?" src="222.files/report.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_BrokenLnk
href="http://www.codeproject.com/script/Articles/Report.aspx?aid=8341">Report
Article</A> <IMG style="VERTICAL-ALIGN: middle" alt=Watch
src="222.files/watchon.png"> <A class="" id=bmw_8341,2
onclick="return watchMe(8341, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8341&obtid=2&bma=AddWatch"
name=ajaxBookmarkWatchLink>Watch</A> <SPAN class=TinyText
style="VISIBILITY: hidden"></SPAN><SPAN
id=ctl00_ArticleHeaderLinks_ArticleBmkWatch_Message></SPAN>
<IMG style="VERTICAL-ALIGN: middle" height=16 alt=Bookmark
src="222.files/link.png" width=16> <A class="" id=bm_8341,2
onclick="return bookmarkMe(8341, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8341&obtid=2&bma=AddBookmark"
name=ajaxBookmarkLink>Bookmark</A> <SPAN class=TinyText
style="VISIBILITY: hidden"></SPAN><SPAN
id=ctl00_ArticleHeaderLinks_ArticleBmk_Message></SPAN> <IMG
style="VERTICAL-ALIGN: middle" height=16 alt=Discuss
src="222.files/discuss.gif" width=15> <A
href="http://www.codeproject.com/KB/DLL/SimpleDll2.aspx#_comments">Discuss</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Recommend Article" src="222.files/mail.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_Recommend
href="http://www.codeproject.com/script/common/TellFriend.aspx?obtid=2&obid=8341">Send to a friend</A>
</DIV></TD>
<TD style="WIDTH: 100px; WHITE-SPACE: nowrap; TEXT-ALIGN: right">
<TABLE id=CurRat>
<TBODY>
<TR>
<TD>
<TABLE>
<TBODY>
<TR>
<TD class=SmallText id=ctl00_ArticleRating_VL
style="WHITE-SPACE: nowrap" align=right><SPAN
id=ctl00_ArticleRating_VoteLabel>69 votes for this
Article.</SPAN></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR>
<TD width=20 bgColor=white height=7><IMG height=7
src="222.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="222.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="222.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="222.files/red.gif" width=20 align=center
border=0></TD>
<TD noWrap width=20 bgColor=white height=7><IMG
height=7 src="222.files/red.gif" width=10
align=center border=0></TD></TR></TBODY></TABLE></TD></TR>
<TR id=ctl00_ArticleRating_PopularityRow>
<TD class=SmallText align=right colSpan=2><A
id=ctl00_ArticleRating_PopularityLnk
title="Calculated as rating x Log10(# votes)"
href="http://www.codeproject.com/script/Articles/TopArticles.aspx?ta_so=1">Popularity:
8.25</A> <SPAN
id=ctl00_ArticleRating_PopularityLbl></SPAN><SPAN
id=ratingVal>Rating: <B>4.49</B> out of
5</SPAN></TD></TR></TBODY></TABLE></TD>
<TD>
<DIV>
<TABLE class=HistTable
title="Voting Distribution. Recent data only">
<TBODY>
<TR>
<TD><IMG title="2 votes, 2.9%" height=1
alt="2 votes, 2.9%" src="222.files/pollcol.gif" width=10
border=0><BR>1</TD>
<TD><IMG title="2 votes, 2.9%" height=1
alt="2 votes, 2.9%" src="222.files/pollcol.gif" width=10
border=0><BR>2</TD>
<TD><IMG title="2 votes, 2.9%" height=1
alt="2 votes, 2.9%" src="222.files/pollcol.gif" width=10
border=0><BR>3</TD>
<TD><IMG title="2 votes, 2.9%" height=1
alt="2 votes, 2.9%" src="222.files/pollcol.gif" width=10
border=0><BR>4</TD>
<TD><IMG title="61 votes, 88.4%" height=20
alt="61 votes, 88.4%" src="222.files/pollcol.gif"
width=10
border=0><BR>5</TD></TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<DIV class="Callout SmallText" id=ctl00_confirmWarning
style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 20px; PADDING-TOP: 10px"><IMG
style="FLOAT: left; MARGIN-RIGHT: 10px" height=32
src="222.files/warning_32.png" width=32><B>Is your email address OK?</B>
You are signed up for our <A id=ctl00_SubscribeLink
href="http://www.codeproject.com/script/Membership/Subscribe.aspx?rp=%2fKB%2fDLL%2fSimpleDll2.aspx">newsletters</A>
but your email address has not been reconfirmed in a long time. To make
this warning go away please click <A id=ctl00_RequestConfirmLink
href="http://www.codeproject.com/script/Membership/SendConfirmRequest.aspx?rp=%2fKB%2fDLL%2fSimpleDll2.aspx">here</A>
to have a confirmation email sent so we can confirm your email address and
continue sending you your newsletters.</DIV><SPAN id=intelliTXT>
<DIV id=contentdiv><!-- Main Page Contents Start --><!-- Article Starts -->
<UL class=download>
<LI><A
href="http://www.codeproject.com/KB/DLL/SimpleDll2/SourceCode.zip">Download
source files - 22.1 Kb</A>
<LI><A
href="http://www.codeproject.com/KB/DLL/SimpleDll2/DemoProject.zip">Download
demo project - 7.52 Kb</A> </LI></UL>
<P><IMG height=243 alt="Sample Image - SimpleDll2.gif"
src="222.files/SimpleDll2.gif" width=462></P>
<H2>Introduction</H2>
<P>In my previous article, I mentioned simply DLL creation and using it.
But it exports function only. Now, I want to describe "how to export
classes from a DLL?"</P>
<H2>How to build MFC DLL Containing New Class?</H2>
<OL>
<LI>Run VC++.
<LI>Choose : File > New.
<LI>Create "MFC AppWizard (DLL)" (named e.g.: MyScndDll).
<LI>Click right mouse button on root branch in "ClassView", point and
click "<U>N</U>ew Class...".
<LI>Select Generic Class as class type, and name it e.g. CRectArea.
<LI>Declare in top of file of "<I>RectArea.h</I>": <PRE><SPAN class=code-comment>//</SPAN><SPAN class=code-comment>
</SPAN><SPAN class=code-preprocessor>#define</SPAN> DLLEXPORT <SPAN class=code-keyword>__declspec</SPAN>(<SPAN class=code-keyword>dllexport</SPAN>)
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment></SPAN></PRE>
<P><CODE><SPAN class=code-keyword>__declspec</SPAN>(<SPAN
class=code-keyword>dllexport</SPAN>)</CODE>'s purpose is to add the
"export directive" to the object file so you don't need a <I>.DEF</I>
file. To make code more readable, define a macro for <CODE><SPAN
class=code-keyword>__declspec</SPAN>(<SPAN
class=code-keyword>dllexport</SPAN>)</CODE>: <CODE>DLLEXPORT</CODE>.</P>
<LI>Now, for exporting per class that you want, type before its
declaration: "<CODE>DLLEXPORT</CODE>" like this: <PRE><SPAN class=code-keyword>class</SPAN> DLLEXPORT CRectArea
{
<SPAN class=code-keyword>public</SPAN>:
CRectArea();
<SPAN class=code-keyword>virtual</SPAN> ~CRectArea();
};</PRE>
<LI>Now, declare a member function for newly created class. E.g.,
<CODE>Calculate</CODE>: <PRE><SPAN class=code-keyword>class</SPAN> DLLEXPORT CRectArea
{
<SPAN class=code-keyword>public</SPAN>:
<SPAN class=code-keyword>int</SPAN> Calculate(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN class=code-keyword>int</SPAN> ParTwo);
CRectArea();
<SPAN class=code-keyword>virtual</SPAN> ~CRectArea();
};</PRE>
<LI>and define it: <PRE><SPAN class=code-keyword>int</SPAN> CRectArea::Calculate(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN class=code-keyword>int</SPAN> ParTwo)
{
<SPAN class=code-keyword>return</SPAN> ParOne*ParTwo;
}</PRE>
<LI>Press Build button.
<LI>Bring out DLL from oven!! </LI></OL>
<P>Note: linker also builds an "import library" with same DLL name but
<I>.lib</I> extension.</P>
<H2>How to use MFC DLL?</H2>
<OL>
<LI>Run VC++.
<LI>Choose : File > New.
<LI>Create "MFC AppWizard (exe)".
<LI>Choose "Dialog based", then click Finish.
<LI>Choose: Project > Add To Project > New > C/C++ Header File.
<LI>Name file, e.g.: <I>Imports</I>.
<LI>Declare in <I>Imports.h</I>: <PRE><SPAN class=code-comment>//</SPAN><SPAN class=code-comment>
</SPAN><SPAN class=code-preprocessor>#define</SPAN> DLLIMPORT <SPAN class=code-keyword>__declspec</SPAN>(<SPAN class=code-keyword>dllimport</SPAN>)
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment></SPAN></PRE>
<P><CODE><SPAN class=code-keyword>__declspec</SPAN>(<SPAN
class=code-keyword>dllimport</SPAN>)</CODE>'s purpose is to add the
"import directive" to the object file. To make code more readable,
define a macro for <CODE><SPAN
class=code-keyword>__declspec</SPAN>(<SPAN
class=code-keyword>dllimport</SPAN>)</CODE>: <CODE>DLLIMPORT</CODE>.</P>
<LI>Type after it, declaration of exported function from DLL. Note: we
must type "<CODE>DLLIMPORT</CODE>" whenever intending to import class: <PRE><SPAN class=code-keyword>class</SPAN> DLLIMPORT CRectArea
{
<SPAN class=code-keyword>public</SPAN>:
<SPAN class=code-keyword>int</SPAN> Calculate(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN class=code-keyword>int</SPAN> ParTwo);
CRectArea();
<SPAN class=code-keyword>virtual</SPAN> ~CRectArea();
};</PRE>
<P>This is the same class that you declared and defined in the DLL.
Here, have introduce as "import class". But it is unrecognized for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -