Initializing a DLL

Source: Internet
Author: User

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'sCwinapp object'sInitinstance andExitinstance.

Extension DLL

InDllmain function generated by the mfc dll wizard.

Non-MFC DLL

In a function calledDllmain that you provide.

In Win32, all DLLs might contain an optional entry-point function (usually calledDllmain) That is called for both initialization and termination. this gives you an opportunity to allocate or release additional resources as needed. windows callthe 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 CILSDllmain. Depending on the type of DLL, you shoshould have a function calledDllmainIn your source code or you shoshould useDllmainProvided in the MFC library.

A. Key Points of initialization of extension DLL

1. For dll_process_attach: it must callAfxinitextensionmodule(). You shocould check the return valueAfxinitextensionmodule; If a zero value is returned fromAfxinitextensionmodule, Return zero from yourDllmainFunction. and creating a newCdynlinklibraryObject during initialization allows the extension DLL to exportCruntimeclassObjects or resources to the client application.

2. For dll_process_detach:AfxtermextensionmoduleIs not must to be called. Only if your extension DLL will be explicitly linked to an executable (meaning the executable CILSAfxloadlibraryTo link to the DLL), you should add a callAfxtermextensionmodule. 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 ofAfxfreelibraryCall). If your extension DLL will be linked implicitly to the application, the callAfxtermextensionmoduleIs not necessary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.