📄 333.htm
字号:
<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%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="333.files/print.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_PrintLnk
href="http://www.codeproject.com/KB/DLL/SimpleDllP3.aspx?display=Print">Print</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Broken Article?" src="333.files/report.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_BrokenLnk
href="http://www.codeproject.com/script/Articles/Report.aspx?aid=8417">Report
Article</A> <IMG style="VERTICAL-ALIGN: middle" alt=Watch
src="333.files/watchon.png"> <A class="" id=bmw_8417,2
onclick="return watchMe(8417, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8417&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="333.files/link.png" width=16> <A class="" id=bm_8417,2
onclick="return bookmarkMe(8417, 2);"
href="http://www.codeproject.com/script/Bookmarks/Add.aspx?obid=8417&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="333.files/discuss.gif" width=15> <A
href="http://www.codeproject.com/KB/DLL/SimpleDllP3.aspx#_comments">Discuss</A>
<IMG style="VERTICAL-ALIGN: middle" height=16
alt="Recommend Article" src="333.files/mail.gif" width=16> <A
id=ctl00_ArticleHeaderLinks_Recommend
href="http://www.codeproject.com/script/common/TellFriend.aspx?obtid=2&obid=8417">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>95 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="333.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="333.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="333.files/red.gif" width=20 align=center
border=0></TD>
<TD width=20 bgColor=white height=7><IMG height=7
src="333.files/red.gif" width=20 align=center
border=0></TD>
<TD noWrap width=20 bgColor=white height=7><IMG
height=7 src="333.files/red.gif" width=3
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.21</A> <SPAN
id=ctl00_ArticleRating_PopularityLbl></SPAN><SPAN
id=ratingVal>Rating: <B>4.15</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="3 votes, 3.2%" height=1
alt="3 votes, 3.2%" src="333.files/pollcol.gif" width=10
border=0><BR>1</TD>
<TD><IMG title="5 votes, 5.3%" height=1
alt="5 votes, 5.3%" src="333.files/pollcol.gif" width=10
border=0><BR>2</TD>
<TD><IMG title="4 votes, 4.2%" height=1
alt="4 votes, 4.2%" src="333.files/pollcol.gif" width=10
border=0><BR>3</TD>
<TD><IMG title="6 votes, 6.3%" height=1
alt="6 votes, 6.3%" src="333.files/pollcol.gif" width=10
border=0><BR>4</TD>
<TD><IMG title="77 votes, 81.1%" height=20
alt="77 votes, 81.1%" src="333.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="333.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%2fSimpleDllP3.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%2fSimpleDllP3.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/SimpleDllP3/SimpleDllP3_Source.zip">Download
source files - 46.3 KB</A>
<LI><A
href="http://www.codeproject.com/KB/DLL/SimpleDllP3/SimpleDllP3_Precompiled.zip">Download
demo project - 7.0 KB</A> </LI></UL>
<H2>Introduction</H2>
<P>In the third part of the assortment of articles "DLLs are Simple!" I
describe how to create a DLL using a DEF file. What is a DEF file? It is a
module-definition (*.<EM>def</EM>) file that is a text file containing one
or more module statements that describe various attributes of a DLL,
including: </P>
<TABLE class=ArticleTable>
<TBODY>
<TR>
<TD><STRONG>LIBRARY statement</STRONG> </TD>
<TD>statement identifies the DEF file as belonging to a DLL </TD></TR>
<TR>
<TD><STRONG>EXPORTS statement</STRONG> </TD>
<TD>lists the names of the functions exported by the DLL </TD></TR>
<TR>
<TD><STRONG>DESCRIPTION statement</STRONG> </TD>
<TD>describes the purpose of the DLL (optional)</TD></TR></TBODY></TABLE><PRE lang=C++>LIBRARY <SPAN class=code-string>"</SPAN><SPAN class=code-string>DefExported"</SPAN>
DESCRIPTION <SPAN class=code-string>'</SPAN><SPAN class=code-string>DefExported For Present in CodeProject'</SPAN>
EXPORTS
Multiply @1</PRE>
<H2>How to Build a DLL Using a DEF File</H2>
<OL>
<LI>Run VC++.
<LI>Choose File>New.
<LI>In the dialog box, choose "MFC AppWizard (DLL)" and name it, e.g.
<CODE>DefExported</CODE>.
<LI>Declare a member function: <PRE lang=C++><SPAN class=code-keyword>public</SPAN>:
<SPAN class=code-keyword>int</SPAN> Multiply(<SPAN class=code-keyword>int</SPAN> PartOne,<SPAN class=code-keyword>int</SPAN> PartTwo);
CDefExportedApp();</PRE>
<LI>Then define it: <PRE lang=C++><SPAN class=code-keyword>int</SPAN> CDefExportedApp::Multiply(<SPAN class=code-keyword>int</SPAN> PartOne, <SPAN class=code-keyword>int</SPAN> PartTwo)
{
<SPAN class=code-keyword>return</SPAN> PartOne*PartTwo;
}</PRE>
<LI>In the FileView tab, click "Source Files" and double click on
<EM>DefExported.def.</EM>
<LI>After the <CODE>EXPORT</CODE> statement, enter <CODE>[function name]
<STRONG><EM>@</EM></STRONG>[number]</CODE> like this: <PRE lang=C++>LIBRARY <SPAN class=code-string>"</SPAN><SPAN class=code-string>DefExported"</SPAN>
DESCRIPTION <SPAN class=code-string>'</SPAN><SPAN class=code-string>DefExported For Present in CodeProject'</SPAN>
EXPORTS
; Explicit exports can go here
Multiply @1</PRE>
<LI>Click the Build Button.
<LI>Bring the DLL out of the oven!! </LI></OL>
<H2>How to Use a DLL</H2>
<P>To use a DLL dynamically, there are three simple API functions:</P>
<UL>
<LI><CODE>LoadLibrary ( [path of DLL] ) </CODE>Loads a DLL into the
process address, returning a handle to the DLL.
<LI><CODE>GetProcAddress ( [loaded library] , [function name]
)</CODE><BR>Returns a handle of a function so that it can be used in
your application.
<LI><CODE>FreeLibrary( [handle of loaded DLL] )</CODE><STRONG>
</STRONG><BR>Releases the memory allocated when the DLL was loaded.
</LI></UL>
<OL>
<LI>Run VC++.
<LI>Choose from the menu File>New.
<LI>In the dialog box, choose "MFC AppWizard (EXE)" and name it, e.g.
<CODE>DynamicLoad.</CODE>
<LI>Select "Dialog Based" and click the Finish button.
<LI>Place a button control on the dialog and double click on it to
create its click event.
<LI>Before typing the code for the button click
(<CODE>BN_CLICKED</CODE>) event, we must define a new function pointer
with the correct number of parameters. This is done according to the
parameters of the function we exported above. <PRE lang=C++><SPAN class=code-keyword>typedef</SPAN> <SPAN class=code-keyword>int</SPAN> (CALLBACK* LPFNMLTPLY)(<SPAN class=code-keyword>int</SPAN>, <SPAN class=code-keyword>int</SPAN>);
<P>Sometimes you have to convert some variable types. For more information about <SPAN class=code-keyword>this</SPAN> conversion, see Microsoft Support Article ID: <A class=code-string href="http://www.codeproject.com/KB/DLL/<span">"</SPAN><SPAN class=code-string>http://support.microsoft.com/default.aspx?scid=kb;en-us;117428"</SPAN>>Q117428</A>. </P>
</PRE>
<LI>Enter the code for the button click event: <PRE lang=C++>HINSTANCE hClcltr=LoadLibrary(<SPAN class=code-string>"</SPAN><SPAN class=code-string>DefExported.dll"</SPAN>);
LPFNMLTPLY lpfnMuliply;
lpfnMuliply = (LPFNMLTPLY)GetProcAddress(hClcltr,<SPAN class=code-string>"</SPAN><SPAN class=code-string>Multiply"</SPAN>);</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -