Windows core programming-DLL advanced

Source: Internet
Author: User

DLL entry point function

A d l can have a single entry point function. The system calls this entry point function at different times. This issue will be described below. These calls can be used to provide some information, which is usually used for d l to initialize and clear each process or thread. If your d l does not need such notification information, you do not need to implement this function in the d l source code. For example, if you create a d l that only contains resources, you do not need to implement this function. If you need to receive the notification information in d l, you can implement the entry point function similar to the following:

 

BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD fdwReason, PVOID fImpLoad){   switch(fdwReason)    {      case DLL_PROCESS_ATTACH:         //The DLL is being mapped into the process's address space.         break;      case DLL_THREAD_ATTACH:         //A thread is being created.         break;      case DLL_THREAD_DETACH:         //A thread is exiting cleanly.         break;      case DLL_PROCESS_DETACH:         //The DLL is being unmapped from the process's address space.         break;   }   return(TRUE);  // Used only for DLL_PROCESS_ATTACH}

Note that the function name d l m a I n is case sensitive.

The h I N S T d l parameter contains the instance handle of d L. Same as the h I n s t e x e parameter of the (w) WI n m a I n function, this value is used to identify the virtual memory address in the process address space mapped to the file image of d L. Generally, this parameter should be stored in a global variable, in this way, you can use the function to load resources (such as d I a l o g B o X and l o a d s t r I n g. The last parameter is f I m p L o a d. If d l is implicitly loaded, this parameter will be a non-0 value, if d l is explicitly loaded, its value is 0.

The f d w r e a s o n parameter is used to specify why the system calls the function. This parameter can use one of the four values. The four values are: d l _ p r o c e s _ at Ta c h, d L _ p r o c e s _ d e ta c h, d l _ t h r e a D _ at Ta c h or d l _ t h r e a D _ d e ta c h. These values are described below.

Note that d l uses the d l m a I n function to initialize them. When your d l m a I n function is executed, other d l in the same address space may not execute their d l A I n function. This means they have not been initialized, so you should avoid calling the functions input from other d L. In addition, you should avoid calling l o a d l B r a r y (e x) from d l m a I n internally) and f r e l I B R A R Y functions, because these functions form a dependency loop.

Dll_process_attach notification
When d l is first mapped to the address space of the process, the system will call the d l m a I n function of d l, pass the value d l _ p r o c e s _ at Ta c h of the f d w r e a s o n parameter to it. This problem occurs only when the file image of d l is mapped for the first time. If the thread then calls the l o a d l I B R A R Y (e x) function for d l in the address space mapped to the process, then the operating system only increments the Count of d l, it does not use the value of d L _ p r o c e s _ at Ta c h again to call the d l m a I n function of d l.
When processing d l _ p r o c e s _ at Ta c h, d l should execute any process-related initialization required by the function in d l. For example, d l may contain functions that need to use their own stacks (created in the address space of the process.

Dll_process_detach notification
When d l is detached from the address space of a process, the system calls d l m a I n function of d l, pass the f d w r e a s o n value d l _ p r o c e s _ d e ta c h to it. When d l processes this value, it should execute any process-related cleanup operations. For example, d l can call the h e a p D e S t r o y function to cancel it during d l _ p r o c e s _ d e ta c h notification the created stack.

Dll_thread_attach notification
When creating a thread in a process, the system needs to view all the d l file images mapped to the address space of the process, call the d l m a I n function with d l _ t h r e a D _ at Ta C H value for each file image. This tells all d l to execute initialization operations for each thread. The newly created thread is responsible for executing all the code in the d l m a I n function of d l. The system allows the new thread to start executing its thread function only when all d l have the opportunity to process the notification.

Dll_thread_detach notification
The first choice for terminating a thread is to make its thread function return. This allows the system to call e x I t h r e a d to cancel the thread. The E x I t h r e a D function tells the system that the thread wants to terminate the operation, but the system does not immediately undo it. Instead, it needs to extract the thread that is about to be withdrawn, and let it call all the functions with d L _ t h r e a D _ d e tach values mapped to d l m a I n. This notification tells all d l to execute the cleanup operation for each thread.

Dllmain and C/C ++ runtime Libraries
When writing a d l, you need some initial help of the C/C ++ Runtime Library. For example, if the created d l contains a global variable, the global variable is an instance of the C ++ class. Before you can use this global variable in the d l m a I n function, the variable must call its constructor. This is done by the d l startup code of the C/C ++ Runtime Library. When your d l file image is mapped to the address space of the process, the system actually calls the _ d l m a I n C RTS t a r t u p function, instead of calling the d l m a I n function.

Delayed DLL Loading(But how to delay it? ^_^)

Microsoft Visual C ++ 6.0 provides an outstanding new feature that makes operations on d l easier. This feature is called delayed loading d l. The delayed loading d l is an implicit link d l, which is loaded only when your code tries to reference a symbol contained in d l. Delayed loading of d l is very useful in the following situations:

• If your application uses several d l, the initialization time is long, because the loader needs to map all the required d l to the address space of the process. One way to solve this problem is to load each d l separately when the process is running. Delayed loading of d l can more easily complete such loading.

• If you call a new function in the Code and try to run your application on an earlier version of the system without this function, the loader will report an error, the application is not allowed to run. You need a way for your application to run. If (at runtime) finds that the application runs on the old system, you will not call the missing function.

Function Forwarder

The function forwarder is a project in the output section of d l, which is used to transfer the call to one function to another function in d l.
DLL Transfer
M I c r o s o f t adds a d l transfer feature to Windows 2000. This feature forces the operating system to Load file modules from your application directory first. Only when the loader cannot find the file in the application directory can it search for other directories. To force a program to be loaded, you always need to first find the application directory. The task is to put a file in the application directory. The content of this file can be ignored, but this file must be called a p n a m e. l o c a l. For example, if an executable file is named s u p e r a p. e x e, the transfer file must be called s u p e r a p. e x e. L o c a l. In the system, l o a d l I B R A R Y (e x) has been modified to check whether the file exists. If the file exists in the application directory, the modules in the directory are loaded. If this module does not exist in the application directory, l o a d l I B R A R Y (e x) will run normally. This feature is very useful for registered c o m objects. It enables the application to put its c o m object d l into its own directory, so that other applications registered with the same c o m object will not interfere with your operations.

Zz

 

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.