📄 111.htm
字号:
<DIV class=MenuCat>Feature Zones</DIV>
<DIV class=MenuChapter><A
href="http://www.codeproject.com/kb/Showcase/">Product Showcase</A></DIV>
<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%3d7324%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%252fAdm%252fimages%252facresso_IS09_Banner_160x600.gif%26C%3dFalse%26adid%3d7324%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="111.files/print.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_PrintLnk
href="http://www.codeproject.com/KB/DLL/Dllfun.aspx?display=Print">Print</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Broken Article?" src="111.files/report.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_BrokenLnk
href="http://www.codeproject.com/script/Articles/Report.aspx?aid=8248">Report
Article</A> <IMG style="VERTICAL-ALIGN: middle" alt=Watch
src="111.files/watchon.png"> <A class="" id=bmw_8248,2
onclick="return watchMe(8248, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8248&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="111.files/link.png" width=16> <A class="" id=bm_8248,2
onclick="return bookmarkMe(8248, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8248&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="111.files/discuss.gif" width=15> <A
href="http://www.codeproject.com/KB/DLL/Dllfun.aspx#_comments">Discuss</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Recommend Article" src="111.files/mail.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_Recommend
href="http://www.codeproject.com/script/common/TellFriend.aspx?obtid=2&obid=8248">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>126 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="111.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="111.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="111.files/red.gif" width=20 align=center
border=0></TD>
<TD noWrap width=20 bgColor=white height=7><IMG
height=7 src="111.files/red.gif" width=8
align=center border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="111.files/white.gif" width=20 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:
7.17</A> <SPAN
id=ctl00_ArticleRating_PopularityLbl></SPAN><SPAN
id=ratingVal>Rating: <B>3.42</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="22 votes, 17.5%" height=5
alt="22 votes, 17.5%" src="111.files/pollcol.gif"
width=10 border=0><BR>1</TD>
<TD><IMG title="6 votes, 4.8%" height=1
alt="6 votes, 4.8%" src="111.files/pollcol.gif" width=10
border=0><BR>2</TD>
<TD><IMG title="5 votes, 4.0%" height=1
alt="5 votes, 4.0%" src="111.files/pollcol.gif" width=10
border=0><BR>3</TD>
<TD><IMG title="8 votes, 6.3%" height=1
alt="8 votes, 6.3%" src="111.files/pollcol.gif" width=10
border=0><BR>4</TD>
<TD><IMG title="85 votes, 67.5%" height=20
alt="85 votes, 67.5%" src="111.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="111.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%2fDllfun.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%2fDllfun.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/Dllfun/SourceCode.zip">Download
source code - 21.8 kb</A>
<LI><A
href="http://www.codeproject.com/KB/DLL/Dllfun/DemoProject.zip">Download
demo project - 10.5 kb</A></LI></UL>
<P><IMG height=257 alt="Sample screenshot" src="111.files/Image1.gif"
width=306></P>
<H2>Introduction</H2>
<P>Some programmers think DLLs are complicated but it's true they are like
EXEs. However I have seen programmers who can't build DLL. There are
multiple way to create a DLL. For usage with exe, easiest way is "Implicit
linking". In this way, we require <I>.LIB</I> file produced by DevStudio.
Below, we create a DLL and then use it with exe. Inside DLL, we calculate
(multiply) two numbers that has been send from the exe. Practically, exe
is "Sender/Receiver" and DLL is "Calculator".</P>
<H2>How to build MFC DLL?</H2>
<OL>
<LI>Run VC++.
<LI>Choose : File > New.
<LI>Create "MFC AppWizard (DLL)" (named e.g. : MyFirstDll).
<LI>Declare in top of file: <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>) : DLLEXPORT</CODE>.</P>
<LI>Now for exporting per function that you want, type before it :
"<CODE>DLLEXPORT</CODE>" like this: <PRE>DLLEXPORT <SPAN class=code-keyword>int</SPAN> Multiply(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN class=code-keyword>int</SPAN> ParTwo)
{
<SPAN class=code-keyword>int</SPAN> Mlt=ParOne*ParTwo;
<SPAN class=code-keyword>return</SPAN> Mlt;
} </PRE>
<LI>Press Build button.
<LI>Bring out DLL from oven!!
<P>Note that linker also builds an " import library " with the same DLL
name but with <I>.lib</I> extension.</P></LI></OL>
<H2>How to use MFC DLL?</H2>
<OL>
<LI>Run VC++.
<LI>Choose : File > New.
<LI>Create "MFC AppWizard (exe)".
<LI>Choose "Dialog based".
<LI>Choose : Project > Add To Project > New > C/C++ Header
File.
<LI>Name file e.g. : Imports.
<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>) : DLLIMPORT</CODE>.</P>
<LI>Type after it: <PRE>DLLIMPORT <SPAN class=code-keyword>int</SPAN> Multiply(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN class=code-keyword>int</SPAN> ParTwo);</PRE>
<P>This is the same function that you defined in DLL. Here, it has been
introduced as an "import function". But it is unrecognized for current
project and we must resolve it for linker.</P>
<LI>Copy <I>.lib</I> file from release or debug folder (depends current
project setting) in previous project to current directory project
because it must link with exe. Inside <I>.lib</I> file (same import
library), exists information about exported functions of DLL.
<LI>Back to VC++ environment and Choose : Project > Settings >
Link (tab).
<LI>Type [<I>.lib</I> file previous project] in "Object/library
modules:". For example, <I>MyFirstDll.lib.</I> Then press OK. It will
resolve externals. Here, "<CODE><SPAN class=code-keyword>int</SPAN>
Multiply(<SPAN class=code-keyword>int</SPAN> ParOne,<SPAN
class=code-keyword>int</SPAN> ParTwo);</CODE>" function.
<LI>Because we intend to use function in <I>...Dlg.cpp</I> file (e.g.<I>
SimpleDllDlg.cpp</I>), type in top of file: <PRE><SPAN class=code-comment>//</SPAN><SPAN class=code-comment>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -