subject_56598.htm
来自「vc」· HTM 代码 · 共 48 行
HTM
48 行
<p>
序号:56598 发表者:badboy 发表日期:2003-10-18 23:58:36
<br>主题:dll 文件的区别?
<br>内容:vc6.0 下用应用程序向导 创建dll, 选择MFC AppWizard(dll) 后,会出现三个选项,分别为:<BR>1 Reglar DLL with MFC statically linked<BR>2 Regular DLL using shared MFC DLL<BR>3 MFC Extension DLL (using shared MFC DLL)<BR><BR> 我想请问这三者的区别是什么?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-10-19 00:08:49
<br>内容:1,2都是普通dll,区别是用1,那么dll在编译的时候是静态链接的,用2是动态链接的。静态链接就是把dll中的代码嵌入到exe中,优点是当exe运行时,不需要dll的支持,缺点是代码尺寸大。用动态链接恰好相反。<BR><BR>3是扩展dll,允许在dll中包装一个c++类。普通dll只能封装函数。但是扩展dll的类,只能提供给vc编译器使用,也就是说你的扩展dll不能被其他的c++编译器识别,更不能被vb这样的语言使用。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-10-19 01:54:52
<br>内容:已经午夜了!还没有睡觉?多注意休息!<BR><BR>还想请教一下:<BR> 您说的,“静态链接就是把dll中的代码嵌入到exe中”,是什么意思? 应该是说,当我们用1来做dll时,这个dll里所用到的一切MFC类和函数都将被静态联接到此dll文件里。<BR> 感觉您说的这句话,“静态链接就是把dll中的代码嵌入到exe中,优点是当exe运行时,不需要dll的支持,缺点是代码尺寸大。用动态链接恰好相反。” 好象是解释EXE 和DLL 的关系。而我感觉,1 里的静态联接意思是编译制作dll 时的静态联接。 应用程序exe加载dll有两种方法,静态加载和动态加载。他们好象都还是需要dll的支持吧?<BR><BR> 另外,3 允许我们包装一个MFC的派生类,如果是一个完全独立的单独的c++类(非MFC类及其派生类),可以包装进去吗?<BR><BR> <BR> <BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:michael 回复日期:2003-10-19 06:56:56
<br>内容: 可能是xiongli没表达清楚,第一种是说把MFC类库的实现代码加到DLL文件中去。这样DLL文件就会大些。<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-10-19 10:39:49
<br>内容:Visual C++ Concepts: Adding Functionality <BR><BR>Regular DLLs Statically Linked to MFC<BR>A regular DLL statically linked to MFC is a DLL that uses MFC internally, and the exported functions in the DLL can be called by either MFC or non-MFC executables. As the name describes, this kind of DLL is built using the static link library version of MFC. Functions are usually exported from a regular DLL using the standard C interface. For an example of how to write, build, and use a regular DLL, see the sample DLLScreenCap.<BR><BR>Note that the term USRDLL is no longer used in the Visual C++ documentation. A regular DLL that is statically linked to MFC has the same characteristics as the former USRDLL. <BR><BR>A regular DLL, statically linked to MFC has the following features: <BR><BR>The client executable can be written in any language that supports the use of DLLs (C, C++, Pascal, Visual Basic, and so on); it does not have to be an MFC application. <BR>The DLL can link to the same MFC static link libraries used by applications. There is no longer a separate version of the static link libraries for DLLs. <BR>Before version 4.0 of MFC, USRDLLs provided the same type of functionality as regular DLLs statically linked to MFC. As of Visual C++ version 4.0, the term "USRDLL" is obsolete. <BR>A regular DLL, statically linked to MFC has the following requirements: <BR><BR>This type of DLL must instantiate a class derived from CWinApp. <BR>This type of DLL uses the DllMain provided by MFC. Place all DLL-specific initialization code in the InitInstance member function and termination code in ExitInstance as in a normal MFC application. <BR>Even though the term "USRDLL" is obsolete, you must still define "_USRDLL" on the compiler command line. This definition determines which declarations will be pulled in from the MFC header files. <BR>Regular DLLs must have a CWinApp-derived class and a single object of that application class, as does an MFC application. However, the CWinApp object of the DLL does not have a main message pump, as does the CWinApp object of an application. <BR><BR>Note that the CWinApp::Run mechanism does not apply to a DLL, since the application owns the main message pump. If the DLL opens modeless dialogs or has a main frame window of its own, the application's main message pump must call a routine exported by the DLL which in turn calls the CWinApp::PreTranslateMessage member function of the DLL's application object. <BR><BR>For an example of this function, see the DLLScreenCap sample. <BR><BR>Symbols are usually exported from a regular DLL using the standard C interface. The declaration of a function exported from a regular DLL would look something like this:<BR><BR>extern "C" __declspec(dllexport) MyExportedFunction( );<BR>All memory allocations within a regular DLL should stay within the DLL; the DLL should not pass to or receive from the calling executable any of the following: <BR><BR>pointers to MFC objects <BR>pointers to memory allocated by MFC <BR>If you need to do any of the above, or if you need to pass MFC-derived objects between the calling executable and the DLL, then you must build an extension DLL.<BR><BR>It is safe to pass pointers to memory that were allocated by the C run-time libraries between an application and a DLL only if you make a copy of the data. You must not delete or resize these pointers or use them without making a copy of the memory.<BR><BR>A DLL that is statically linked to MFC cannot also dynamically link to the shared MFC DLLs. A DLL that is statically linked to MFC is dynamically bound to an application just like any other DLL; applications link to it just like any other DLL. <BR><BR>The standard MFC static link libraries are named according to the convention described in the topic Naming Conventions for MFC DLLs. However, with MFC version 3.0 and later, it is no longer necessary to manually specify to the linker the version of the MFC library you want linked in. Instead, the MFC header files automatically determine the correct version of the MFC library to link in based on preprocessor defines, such as _DEBUG or _UNICODE. The MFC header files add /DEFAULTLIB directives instructing the linker to link in a specific version of the MFC library. <BR><BR><BR><BR>Regular DLLs Dynamically Linked to MFC<BR>A regular DLL dynamically linked to MFC is a DLL that uses MFC internally, and the exported functions in the DLL can be called by either MFC or non-MFC executables. As the name describes, this kind of DLL is built using the dynamic-link library version of MFC (also known as the shared version of MFC). Functions are usually exported from a regular DLL using the standard C interface. <BR><BR>You must add the AFX_MANAGE_STATE macro at the beginning of all of the exported functions in regular DLLs that dynamically link to MFC to set the current module state to the one for the DLL. This is done by adding the following line of code to the beginning of functions exported from the DLL:<BR><BR>AFX_MANAGE_STATE(AfxGetStaticModuleState( ))<BR>A regular DLL, dynamically linked to MFC has the following features: <BR><BR>This is a new type of DLL introduced by Visual C++ 4.0. <BR>The client executable can be written in any language that supports the use of DLLs (C, C++, Pascal, Visual Basic, and so on); it does not have to be an MFC application. <BR>Unlike the statically-linked regular DLL, this type of DLL is dynamically linked to the MFC DLL (also known as the shared MFC DLL). <BR>The MFC import library linked to this type of DLL is the same one used for extension DLLs or applications using the MFC DLL: MFCxx(D).LIB. <BR>A regular DLL, dynamically linked to MFC has the following requirements: <BR><BR>These DLLs are compiled with _AFXDLL defined, just like an executable which is dynamically linked to the MFC DLL. But _USRDLL is also defined, just like a regular DLL which is statically linked to MFC. <BR>This type of DLL must instantiate a CWinApp-derived class. <BR>This type of DLL uses the DllMain provided by MFC. Place all DLL-specific initialization code in the InitInstance member function and termination code in ExitInstance as in a normal MFC application. <BR>Because this kind of DLL uses the dynamic link library version of MFC, you must explicitly set the current module state to the one for the DLL. To do this, use the AFX_MANAGE_STATE macro at the beginning of every function exported from the DLL. <BR><BR>Regular DLLs must have a CWinApp-derived class and a single object of that application class, as does an MFC application. However, the CWinApp object of the DLL does not have a main message pump, as does the CWinApp object of an application. <BR><BR>Note that the CWinApp::Run mechanism does not apply to a DLL, since the application owns the main message pump. If your DLL brings up modeless dialogs or has a main frame window of its own, your application's main message pump must call a DLL-exported routine that calls CWinApp::PreTranslateMessage.<BR><BR>Place all DLL-specific initialization in the CWinApp::InitInstance member function as in a normal MFC application. The CWinApp::ExitInstance member function of your CWinApp derived class will be called from the MFC provided DllMain function before the DLL is unloaded.<BR><BR>You must distribute the shared DLLs MFCx0.DLL and MSVCRT.DLL (or similar files) with your application.<BR><BR>A DLL that is dynamically linked to MFC cannot also statically link to MFC. Applications link to regular DLLs dynamically linked to MFC it just like any other DLL. <BR><BR>Symbols are usually exported from a regular DLL using the standard C interface. The declaration of a function exported from a regular DLL looks something like this:<BR><BR>extern "C" __declspec(dllexport) MyExportedFunction( );<BR>All memory allocations within a regular DLL should stay within the DLL; the DLL should not pass to or receive from the calling executable any of the following: <BR><BR>pointers to MFC objects <BR>pointers to memory allocated by MFC <BR>If you need to do any of the above, or if you need to pass MFC-derived objects between the calling executable and the DLL, then you must build an extension DLL.<BR><BR>It is safe to pass pointers to memory that were allocated by the C run-time libraries between an application and a DLL only if you make a copy of the data. You must not delete or resize these pointers or use them without making a copy of the memory.<BR><BR>When building a regular DLL that dynamically links to MFC, you need to use the macro AFX_MANAGE_STATE to switch the MFC module state correctly. This is done by adding the following line of code to the beginning of functions exported from the DLL:<BR><BR>AFX_MANAGE_STATE(AfxGetStaticModuleState( ))<BR>The AFX_MANAGE_STATE macro should not be used in regular DLLs that statically link to MFC or in extension DLLs. For more information, see Managing the State Data of MFC Modules.<BR><BR>For an example of how to write, build, and use a regular DLL, see the sample DLLScreenCap. For more information about regular DLLs that dynamically link to MFC, see the section titled "Converting DLLScreenCap to Dynamically Link with the MFC DLL" in the abstract for the sample.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:xiongli 回复日期:2003-10-19 11:12:17
<br>内容:据个例子<BR>如果你的dll只是做一般的工作,不涉及mfc,就用regular dll<BR>如果你的dll要使用mfc,比如封装一个CView的派生类,然后提供一个关于这个派生类的C函数入口,那么就用联结到mfc的dll.<BR>static linked dll has a bigger size,because all the mfc code is put in the dll.shall linked dll has a smaller size,but it must supported by other mfc dll.both of the two kinds of dll can be used either C/C++ or other programming languages<BR>if u want to encompuls a MFC class into a dll,and u also want the client code use the class directly,not in "C interface" form,u must use the extention dll.the extention dll use shall linked mfc.As mfc class can be used in extiontoin dll,i think a regualr c++ class can also be used in this form.u can prove it urself.<BR>我在外面的电脑,中文输入真实麻烦<BR>希望还看得懂
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:badboy 回复日期:2003-10-26 21:57:28
<br>内容:xiongli 老兄,谢谢你给的材料!小弟最近急需了解数据库,您对数据库懂吗?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2003-10-26 22:06:30
<br>内容:i am a database idiot :(<BR>now i am studing the database theory.<BR>Let's get success neck by neck :D
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?