[Reprint]: http://blog.cechina.cn/Chennousstar/41703/message.aspx
1. Introduction to DLL
DLL, dynamic link library, or dynamic link library is a set of modules that can be shared by other applications. As an executable file of the shared function library, DLL encapsulates one or more compiled and linked functions. Multiple processes can use one DLL at a time and share a copy of the DLL in the memory. DLL also helps to share data and resources. It and the executable file (. EXE files). Their difference is that although the dll contains executable code, it cannot be executed independently. It can only be called directly or indirectly by the application that needs to use it.
In general, in Windows operating systems, many applications are not a complete executable file. Correct execution of these files requires calling some relatively independent dynamic link libraries, that is, DLL files. An application can call multiple DLL files. a dll file may also be shared by several applications. Such a DLL file is called a shared DLL file. DLL files are usually stored in the C:/Windows/system directory and may be stored in the directory or subdirectory where the application is located.
When it comes to Dynamic Links, first describe static connections. What is static connection? During the program Link process, you need to link the compiled binary code to the target code. The linker obtains all referenced functions from the static Link Library, and put the referenced functions together with the code into the executable file. The DLL static connection means that the linker copies the code of the referenced library function to the executable module (. dll file or. EXE file) that calls the DLL.
What is dynamic link? Dynamic Links are the information required by the system to allow executable modules (. DLL files or. EXE files) to be included in the executable code of the positioning DLL function. In other words, executable modules (. DLL files or. EXE files) load these modules at runtime (that is, the required modules are mapped to the address space of the calling process ).
What are the advantages of dynamic and static connections?
(1) saves memory and reduces swap operations. Using Dynamic Links, multiple processes can use one DLL at a time and share a copy of the DLL in the memory. With static links, each application contains the code of the referenced library function, so windows must load a copy of the code of the referenced library function for each application in memory.
(2) Saving disk space. With Dynamic Links, only one DLL copy is required on the disk. With static links, each application contains the code of the referenced library function.
(3) It is easier to upgrade. When dynamic links are used, functions in DLL do not need to be re-compiled or re-linked to use their applications as long as their parameters and return values are not changed. When using static links, you need to relink the functions to generate applications.
(4) multi-language programs are supported. As long as the program complies with the function call conventions, programs written in different programming languages can call the same DLL function.
(5) provides a mechanism for extending the MFC Library Class. You can use the existing MFC class derived classes and place them in the MFC extension DLL for the MFC application.
(6) multi-language programs are supported and International versions can be easily created. By putting resources in the DLL, it is much easier to create an international version of the application. Strings of each language version used for applications can be placed in a separate DLL resource file, and appropriate resources can be loaded for different language versions.
The dll contains the following two types of functions:
Export functions: these functions are called by the executable module (. dll file or. EXE file.
Internal functions: these functions are called only from the DLL defining them. DLL also exports data. However, the data is used by the corresponding function.
You can call functions in the DLL using the following methods:
Dynamic Link during loading: the executable module executes an explicit call to export the DLL function. Create a import library for the DLL, and then link the DLL to the application. When loading an application, the import/export database provides the information required to load the DLL and find the exported DLL function.
Dynamic Link during runtime: When a DLL is loaded during runtime, the executable module uses the loadlibrary function or the LoadLibraryEx function. The executable module calls the getprocaddress function to obtain the address of the exported DLL function. When linking, Windows searches for a pre-installed DLL group, such as the performance library (kernel32.dll) and Security Library (user32.dll ). Windows then searches for DLL in the following order:
1. The directory where the executable program of the current process is located.
2. Current directory.
3. Windows System directory. (The getsystemdirectory function obtains the path of the Windows System directory .)
4. Windows directory. (The getwindowsdirectory function obtains the path of the Windows directory .)
5. directories listed in PATH environment variables. Note: libpath environment variables are not used for search.
DLL has a special entry point (the dllmain function) that runs when processes and threads are appended and detached. This action allows you to create and destroy data structures as needed. The file types with the extension. ocx,. CPL, And. DRV are also DLL, although the file extension has changed.
You can create a DLL for the following purposes:
(1) divide programs into separate modules that can be loaded as needed.
(2) store resources specific to languages or regions.
(3) Enable your own applications to use the core code library.
(4) generate a COM object or ActiveX Control (OCX) in the process ).
(5) use the OLE object as the in-process DLL. This usage can improve the performance of OLE links.
(6) Use Control Panel extensions or some types of drivers.
Ii. ActiveX Control with. ocx as the suffix
As mentioned earlier, files with the extension. ocx,. CPL, And. DRV are also DLL files.
The current ActiveX control is equivalent to the previous ole control or OCX. A typical control includes the user interface during design and runtime. The unique idispatch interface defines the properties and methods of the control, the unique iconnectionpoint interface defines events that can be triggered by controls. A control can be run in a container, so it is similar to a DLL in terms of running.
Although both ActiveX and OLE are based on the Component Object Model (COM), they provide completely different services for programmers. Com provides a low-level object bundling mechanism that supports interactive communication between objects. Ole uses com to provide low-level application services, such as using the connection and embedding mechanisms, and allows users to create compound documents. In contrast, ActiveX provides a more refined structure to support embedding controls on network sites and interactive responses to events. ActiveX is optimized to improve the time and space efficiency, and Ole is optimized to facilitate end users to use and integrate desktop system applications. ActiveX also brings some technical changes to Internet technology. For example, ActiveX greatly reduces the amount of code (the amount of code is reduced by 50% to 70) and supports more submissions and asynchronous connections.
ActiveX is based on OLE and COM, but the various development tools of MS can shield many technical details that are confusing in the com model. ActiveX component technology includes the following aspects: (1) Automation server (2) automation controller (3) control (4) COM Object (5) Document (6) container.
ActiveX controls suffixed with. ocx are mainly used for Web and window Forms program development. Applications Use ActiveX/COM components to extend their business logic, transaction processing, and application services.
By the way, you can use. ocx as the suffix to register and uninstall ActiveX controls. In the "run" menu, enter the following code to complete the task:
Regsvr32 Path & "/xxx. ocx" 'Registration
Regsvr32/u Path & "/xxx. ocx" 'uninstall
Path indicates the path of the XXX. ocx directory.
Iii. Differences between OCX and DLL
To. the ActiveX Control with OCX suffix is a special DLL. It is based on OLE and COM and is a visual control with an interactive interface. It defines the properties and methods of the control, define the response of events that can be triggered by the control. A file with a suffix named. dll is a set of modules that contain functions and data and can be shared by other applications.
Modules and class modules in VB
The module is a structure provided by VB to organize different types of Process Code together. There are three types of modules in VB: Form module, standard module and class module. The class module is used to create attributes and method definitions of new object classes.
The module can write some public functions. The class module is used to complete a function. Generally, a class module completes a function.
In other words, the module can put a bunch of functions and sub files, which are non-object-oriented. The class module is a custom class. It is mainly object-oriented. You can customize an object and set its attributes and methods. For example, shutdown can be written as a process in the module.
In the game, for example, a tank, it is better to write it into a class module. For example, the life of a tank, the attacking power of a tank, and the attack scope of a tank are attributes. And the death of tanks. There are also tanks attacked. This is an event.