Windows program design Reading Notes-Dynamic Link Library

Source: Internet
Author: User

1. Basic definition.

Generally, the dynamic link library does not directly execute or receive messages. They are independent files that contain functions that can be called by programs or other DLL to complete certain jobs. It takes effect only when it is called by other modules.

The so-called "Dynamic Link" refers to the process in which Windows links a function call in a module to the actual function in the library module, which occurs at the runtime.

Some Dynamic Link Libraries are pure resources that only contain data and do not contain code.

DLL, but there are other extensions such as EXE, Fon, and DRV. However, only the DLL is automatically loaded by windows. Other programs need to explicitly call loadlibrary or LoadLibraryEx to load the DLL.

 

The code of the target library in lib format is automatically added to the EXE file of the program when the link program is used for static link.

The input library, in lib format, does not contain code. Instead, it provides information for the linked program to determine the function calls in the source code.

The target library and input library are used during program development and are statically linked. The dynamic link library is used during the running of the program.

 

When multiple processes call the same DLL, each process allocates its own address space for the DLL. The DLL stores different data for each process. However, shared memory can be used.

The Dynamic Link Library does not accept messages, but can call getmessage and peekmessage.

Because the message in the modal dialog box is received outside the message loop of the program, you can create a modal dialog box in the library.

 

Ii. Simple DLL format:

 

/*----------------------
Edrlib. h header file
----------------------*/

 

# Ifdef _ cplusplus
# Define export extern "C" _ declspec (dllexport)
# Else
# Define export _ declspec (dllexport)
# Endif

Export bool callback edrcentertexta (HDC, prect, pcstr );
Export bool callback edrcentertextw (HDC, prect, pcwstr );

# Ifdef Unicode
# Define edrcentertext edrcentertextw
# Else
# Define edrcentertext edrcentertexta
# Endif

 

 

 

/*-------------------------------------------------
Edrlib. c -- Easy Drawing routine library module
(C) Charles Petzold, 1998
-------------------------------------------------*/

 

# Include <windows. h>
# Include "edrlib. H"

Int winapi dllmain (hinstance hmodule, DWORD fdwreason, pvoid pvreserved)
{

Hinstance = hmodule;

Switch (dwreason)

{

Case dll_process_attach: // The DLL is being mapped into
// Process's address space
Disablethreadlibrarycils (hmodule );
// This tells the system we don't want
// Dll_thread_attach and dll_thread_detach
// Modifications sent to the specified
// DLL's dllmain Function
Break;

Case dll_process_detach: // The DLL is being unmapped from
// Process address space
Break;

Case dll_thread_attach: // a thread is being created
Break;

Case dll_thread_detach: // a thread is exiting cleanly
Break;

Default:
Break;
}

Return true; // true indicates the initialization is successful.
}

Export bool callback edrcentertexta (HDC, prect PRC, pcstr pstring)
{

//.... Do
}

Export bool callback edrcentertextw (HDC, prect PRC, pcwstr pstring)
{

//.... Do

}

 

Supports Unicode and non-Unicode

Dllmain // entry function, called when the library is started for the first time and when the library is terminated, used to perform initialization and cancel initialization.
Export // identifies the export function. Used to add the function name to the. Lib file.

# Define export extern "C" _ declspec (dllexport) // supports C and C ++

 

Iii. dll shared memory. The DLL is started multiple times, but this memory is shared.
# Pragma data_seg ("shared ")
Int itotal = 0;
Wchar szstrings [max_strings] [max_length + 1] = {'\ 0 '};
# Pragma data_seg ()

 

# Pragma comment (linker, "/section: shared, RWS") // indicates the memory segment named shared, which can be read and written for sharing. It is equivalent to setting:/section: shared, RWS in Project Properties

 

 

4. Do not enter the Dynamic Link Library:

  Hmodule loadlibrary (lpctstrLpfilename// File name of Module);

  Farproc getprocaddress (hmoduleHmodule,// Handle to DLL moduleLpcstrLpprocname// Function name);

  Bool freelibrary (hmoduleHmodule// Handle to DLL module);

 

5. Pure resource DLL.

Dll contains a dllmain return true; you can add resources to the resource.

The following uses bitmap as an example:

  Hlibrary = loadlibrary (text ("bitlib. dll "))

Hbitmap = loadbitmap (hlibrary, makeintresource ("bitmap1 "));

Freelibrary (hlibrary );

Related Article

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.