Initializing A Dll

It's so clear that there are three type dlls: regular dll, extension dll and non-MFC dll on windows. Below table from MSDN shows how to initialize it:

Typically, your DLL has initialization code (such as allocating memory) that must execute when your DLL loads. When using Visual C++, where you add code to initialize your DLL depends on the type of DLL you are building. If you do not need to add initialization or termination code, there is nothing special you have to do when building your DLL. If you need to initialize your DLL, the following table describes where to add your code.

DLL type Where to add initialization and termination code

Regular DLL

In the DLL's CWinApp object's InitInstance and ExitInstance.

Extension DLL

In the DllMain function generated by the MFC DLL Wizard.

Non-MFC DLL

In a function called DllMain that you provide.

In Win32, all DLLs might contain an optional entry-point function (usually called DllMain) that is called for both initialization and termination. This gives you an opportunity to allocate or release additional resources as needed. Windows calls the entry-point function in four situations: process attach, process detach, thread attach, and thread detach.

The C run-time library provides an entry-point function called _DllMainCRTStartup, and it calls DllMain. Depending on the type of DLL, you should have a function called DllMain in your source code or you should use the DllMain provided in the MFC library.

A. Key Points Of Initialization Of Extension dll

1. For DLL_PROCESS_ATTACH: it must call AfxInitExtensionModule(). You should check the return value of AfxInitExtensionModule; if a zero value is returned from AfxInitExtensionModule, return zero from your DllMain function. And Creating a new CDynLinkLibrary object during initialization allows the extension DLL to export CRuntimeClass objects or resources to the client application.

2. For DLL_PROCESS_DETACH: AfxTermExtensionModule is not must to be called. Only If your extension DLL will be explicitly linked to an executable (meaning the executable calls AfxLoadLibrary to link to the DLL), you should add a call to AfxTermExtensionModule. This function allows MFC to clean up the extension DLL when each process detaches from the extension DLL (which happens when the process exits or when the DLL is unloaded as a result of a AfxFreeLibrary call). If your extension DLL will be linked implicitly to the application, the call to AfxTermExtensionModule is not necessary.

转载于:https://www.cnblogs.com/taoxu0903/archive/2009/07/05/1517095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值