📄 vc++动态链接库编程之非mfc dll-开发者网络-vc++-天极ye2sky.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0040)http://dev.yesky.com/430/2142930_1.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Yesky</TITLE>
<META content="VC++动态链接库编程之非MFC DLL," name=description>
<META content="VC++动态链接库编程之非MFC DLL," name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT
src="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/logo.gif"
border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道></A><A
href="http://dev.yesky.com/">程序开发></A><A
href="http://dev.yesky.com/devjava/">Java</A><A
href="http://dev.yesky.com/ysdevvb">VB</A><A
href="http://dev.yesky.com/ysdevvc">VC</A><A
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A
href="http://dev.yesky.com/ysdevc">C/C++</A><A
href="http://dev.yesky.com/web/">Web开发</A><A
href="http://dev.yesky.com/msdn/">微软专栏</A><A
href="http://dev.yesky.com/devydkf/">移动</A><A
href="http://dev.yesky.com/devsjk/">数据库</A><A
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A
href="http://soft.yesky.com/lesson/">产品中心</A><A
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A
href="http://q.yesky.com/group/202019"><FONT color=#ff0000>群乐:<FONT
face=Verdana>Vista系统</FONT></FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极网</A> > <A
href="http://dev.yesky.com/">开发频道</A> > VC++动态链接库编程之非MFC DLL</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/430/2142930_1.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>VC++动态链接库编程之非MFC DLL</H1></DIV>
<DIV id=conauthor><SPAN>2005-10-08 09:49 </SPAN><SPAN>作者: 宋宝华 </SPAN><SPAN>出处:
作者授权转载 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2142930.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN id=contentAdv></SPAN></DIV> 4.4
DllMain函数<BR><BR> Windows在加载DLL的时候,需要一个入口函数,就如同控制台或DOS程序需要main函数、WIN32程序需要WinMain函数一样。在前面的例子中,DLL并没有提供DllMain函数,应用工程也能成功引用DLL,这是因为Windows在找不到DllMain的时候,系统会从其它运行库中引入一个不做任何操作的缺省DllMain函数版本,并不意味着DLL可以放弃DllMain函数。<BR><BR> 根据编写规范,Windows必须查找并执行DLL里的DllMain函数作为加载DLL的依据,它使得DLL得以保留在内存里。这个函数并不属于导出函数,而是DLL的内部函数。这意味着不能直接在应用工程中引用DllMain函数,DllMain是自动被调用的。<BR><BR> 我们来看一个DllMain函数的例子(<A
href="http://www.mydown.com/code/244/244555.html" target=_blank><FONT
color=#0000ff>单击此处下载本工程</FONT></A>)。<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved)<BR>{<BR> switch (ul_reason_for_call)<BR> {<BR> case
DLL_PROCESS_ATTACH:<BR> printf("\nprocess attach of
dll");<BR> break;<BR> case DLL_THREAD_ATTACH:<BR> printf("\nthread
attach of dll");<BR> break;<BR> case
DLL_THREAD_DETACH:<BR> printf("\nthread detach of
dll");<BR> break;<BR> case DLL_PROCESS_DETACH:<BR> printf("\nprocess
detach of dll");<BR> break;<BR> }<BR> return
TRUE;<BR>}</TD></TR></TBODY></TABLE><BR> DllMain函数在DLL被加载和卸载时被调用,在单个线程启动和终止时,DLLMain函数也被调用,ul_reason_for_call指明了被调用的原因。原因共有4种,即PROCESS_ATTACH、PROCESS_DETACH、THREAD_ATTACH和THREAD_DETACH,以switch语句列出。<BR><BR> 来仔细解读一下DllMain的函数头BOOL
APIENTRY DllMain( HANDLE hModule, WORD ul_reason_for_call, LPVOID lpReserved
)。<BR><BR> APIENTRY被定义为__stdcall,它意味着这个函数以标准Pascal的方式进行调用,也就是WINAPI方式;<BR><BR> 进程中的每个DLL模块被全局唯一的32字节的HINSTANCE句柄标识,只有在特定的进程内部有效,句柄代表了DLL模块在进程虚拟空间中的起始地址。在Win32中,HINSTANCE和HMODULE的值是相同的,这两种类型可以替换使用,这就是函数参数hModule的来历。<BR><BR> 执行下列代码:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>hDll = LoadLibrary("..\\Debug\\dllTest.dll");<BR>if (hDll !=
NULL)<BR>{<BR> addFun = (lpAddFun)GetProcAddress(hDll,
MAKEINTRESOURCE(1));<BR> //MAKEINTRESOURCE直接使用导出文件中的序号<BR> if (addFun !=
NULL)<BR> {<BR> int result = addFun(2, 3);<BR> printf("\ncall add in
dll:%d",
result);<BR> }<BR> FreeLibrary(hDll);<BR>}</TD></TR></TBODY></TABLE><BR> 我们看到输出顺序为:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>process attach of dll<BR>call add in dll:5<BR>process detach of
dll</TD></TR></TBODY></TABLE><BR> 这一输出顺序验证了DllMain被调用的时机。<BR><BR> 代码中的GetProcAddress
( hDll, MAKEINTRESOURCE ( 1 )
)值得留意,它直接通过.def文件中为add函数指定的顺序号访问add函数,具体体现在MAKEINTRESOURCE ( 1
),MAKEINTRESOURCE是一个通过序号获取函数名的宏,定义为(节选自winuser.h):<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>#define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))<BR>#define
MAKEINTRESOURCEW(i) (LPWSTR)((DWORD)((WORD)(i)))<BR>#ifdef
UNICODE<BR>#define MAKEINTRESOURCE MAKEINTRESOURCEW<BR>#else<BR>#define
MAKEINTRESOURCE MAKEINTRESOURCEA</TD></TR></TBODY></TABLE><BR> 4.5
__stdcall约定<BR><BR> 如果通过VC++编写的DLL欲被其他语言编写的程序调用,应将函数的调用方式声明为__stdcall方式,WINAPI都采用这种方式,而C/C++缺省的调用方式却为__cdecl。__stdcall方式与__cdecl对函数名最终生成符号的方式不同。若采用C编译方式(在C++中需将函数声明为extern
"C"),__stdcall调用约定在输出函数名前面加下划线,后面加“@”符号和参数的字节数,形如_functionname@number;而__cdecl调用约定仅在输出函数名前面加下划线,形如_functionname。<BR><BR> Windows编程中常见的几种函数类型声明宏都是与__stdcall和__cdecl有关的(节选自windef.h):<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>#define CALLBACK __stdcall //这就是传说中的回调函数<BR>#define WINAPI __stdcall
//这就是传说中的WINAPI<BR>#define WINAPIV __cdecl<BR>#define APIENTRY WINAPI
//DllMain的入口就在这里<BR>#define APIPRIVATE __stdcall<BR>#define PASCAL
__stdcall </TD></TR></TBODY></TABLE><BR> 在lib.h中,应这样声明add函数:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>int __stdcall add(int x, int
y);</TD></TR></TBODY></TABLE><BR> 在应用工程中函数指针类型应定义为:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>typedef int(__stdcall *lpAddFun)(int,
int);</TD></TR></TBODY></TABLE><BR> 若在lib.h中将函数声明为__stdcall调用,而应用工程中仍使用typedef
int (* lpAddFun)(int,int),运行时将发生错误(因为类型不匹配,在应用工程中仍然是缺省的__cdecl调用),弹出如图7所示的对话框。
<BR><BR>
<TABLE width="90%" align=center border=0>
<TBODY>
<TR>
<TD>
<DIV align=center><IMG
src="VC++动态链接库编程之非MFC DLL-开发者网络-VC++-天极Ye2sky.files/vrn4lm2rlf11.jpg"
border=0><BR>图7
调用约定不匹配时的运行错误</DIV></TD></TR></TBODY></TABLE><BR> 图8中的那段话实际上已经给出了错误的原因,即“This
is usually a result of …”。<BR><BR> <FONT color=#0000ff><A
href="http://www.mydown.com/code/244/244554.html"
target=_blank>单击此处下载__stdcall调用例子工程源代码</A></FONT>。<BR><BR>
<DIV class=right>共3页。 <A class=page
href="http://dev.yesky.com/430/2142930.shtml" target=_self><FONT
face=Webdings>9</FONT></A> <A class=page
href="http://dev.yesky.com/430/2142930.shtml" target=_self><FONT
face=Webdings>7</FONT></A> <A class=page
href="http://dev.yesky.com/430/2142930.shtml" target=_self><B>1</B></A> <A
class=page href="http://dev.yesky.com/430/2142930_1.shtml" target=_self><FONT
color=#666666><B>2</B></FONT></A> <A class=page
href="http://dev.yesky.com/430/2142930_2.shtml" target=_self><B>3</B></A> <A
class=page href="http://dev.yesky.com/430/2142930_2.shtml" target=_self><FONT
face=Webdings>8</FONT></A> <A class=page
href="http://dev.yesky.com/430/2142930_2.shtml" target=_self><FONT
face=Webdings>:</FONT></A></DIV></DIV><!--广告代码开始-->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -