DLL programming technology and applications under Windows
Abstract: This article describes the basic use methods and applications of DLL technology in Windows programming, gives all source code for two practical DLLs of direct memory access and port I / O. Keywords: DLL Windows programming memory access I / O
1. Because Windows provides unprecedented standard user interfaces, graphics processing capabilities and simple flexible operations for microcomputers, most of the programming people have turned or being turned to Windows programming. In the programming task of the actual application system designed by many users, it is often necessary to implement access to hardware resources and memory resources, such as port I / O, DMA, interrupt, direct memory access, and more. If you have a DOS program, this is a lifted thing, but it is more difficult to prepare a Windows program, especially the procedure in the WindowsNT environment. Because Windows has the characteristics of "unrelated to the device", it is not advocated to deal with the bottom of the machine. If you use the Windows API function or I / O readout instruction to access and operate, the program runs often generates a protection mode error or even Cancer, more serious situations can cause the system to crash. So how to solve the above problems under Windows? It is one of the good ways with DLL (Dynamic Link Libraries) technology. DLL is the most important component of Windows, many new features in Windows, new features are implemented by DLL, so master it, it is very important. In fact, Windows itself is composed of many DLLs. Its most basic three-large forming module KERNEL, GDI and user are DLL, and all of its library modules are also designed as a DLL. Any system file that is .dll, .drv, .fon, .sys, and many .EXE is DLL. If you open the Windows / System directory, you can see a lot of DLL modules. Although the DLL runs under Ring3 priority, it is still a simple way to implement hardware interfaces. DLL can have its own data segment, but there is no own stack, use the same stack mode as the application called it, reduce the inconvenience of programming design; at the same time, only one instance in memory, make it efficient and economical Use memory; DLL implementation code package makes the program simple and clear; there is also a biggest feature, that is, DLL's preparation is independent of specific programming language and compiler, as long as the DLL development specification and programming strategy, and arrange The correct call interface, regardless of the DLL prepared in the programming language, is versatile. For example, the DLL program compiled in BC31 can be used in a variety of locales such as BC, VC, VB, Delphi. The author compiled in the BC31 environment to direct memory access and port I / O two DLLs in the BC31, which is well running in the application of multiple homemade systems.
Second, the establishment of DLL and the establishment of the DLL and the calling method have a detailed introduction in many information. In order to save space, only some of the main summary. 1.DLL's establishment of the DLL is the establishment of the following aspects is indispensable and must be mastered: the entrance function libmain () is like WinMain () in the C program, Windows is executed each time the DLL is loaded. The libmain () function is mainly used for some initialization. The usual form is:
INT FAR PASCAL LIBMAIN (Hinstance Hinstance, Word WDataseg, Word Wheapsize, LPSTR LPSZCMDLINE) {if (Whaeapsize! = 0) // Make local heap, data segment can be moved to unlockdata (0); // Unlock data segment / * here Make some user necessary initialization work * / return 1; // initialization success}
When the exit function WEP () Windows is uninstalling the DLL from the memory, call the corresponding export function WEP (), mainly do some cleaning work, such as the memory resource that is occupied; discard some strings, bitmaps and other resources; close the open file and many more. Custom output function To remotely call the application in different memory segments, the custom output function must be defined as a remote function (using the FAR key) to prevent accidental results in the use of the short-range pointer; Plus the Pascal keyword can speed up the running speed of the program, so that the code is simple and efficient, and the program's running speed is improved. The extraction method of the output function is listed in the DLL module definition file (.def) by the exports statement lists one by one by one. For example: Exports WEP @ 1 ResidentName // ResidentName can improve DLL efficiency and processing speed Portin @ 2 portout @ 3 // Usually use the _export keyword to use the _export keyword in the description of each output function addition series number to each output function Resulting. One of the above two methods is optionally, it is not repetitive. The following two instances use two different drawings, please pay attention.
2. DLL's call is loaded with the DLL, Windows looks for the order of the corresponding DLL as follows:. Current work Dry. Windows directory; getWindowsDirectory () function provides the path name of this directory. Windows system catalog, system subdirectory; call the getSystemDireTory () function to get the path name of this directory. DOS's path commands all directories in the Ruspe. All directories in the list of images in the network.
The mode of calling the output function in the DLL module: No matter what language is used to call the compiled DLL, there are basically two call modes, namely static call mode, and dynamic call mode. Static call mode is completed by the compilation system to the DLL load and the DLL uninstalled encoding (if any other program is used using the DLL, Windows reduces the application record of the DLL 1 until all related programs end the DLL Release it when you use it), simple and practical, but not flexible enough, you can only meet the general requirements. The dynamic call mode is to load and unload the DLL by the programmer to reach the purpose of calling the DLL, which is more complicated, but more efficiently uses memory, is an important way to prepare a large application. Specifically, the following method can be used. In the application module definition file, the function name to call the DLL is listed with the imports statement. For example, Imports Memorydll.MemoryReadMemorydll.MemoryWrite When the application is running with the DLL module dynamic link first with loadLibrary, use the getProcAddress function to check the address of its output function with the getProcAddress function, and get its pointer to call. Such as: Handle Hlibrary; INT PortValue; Hlibrary = LoadLibrary ("portdll.dll"); // Load DLLIF (Hlibrary> 31) // Load Success {LPFUNC = GetProcaddress (Hlibrary, "Portin"); // Check Take the portin function address if (lpfunc! = (Farproc) null) // Take success, call PortValue = (* lpfunc) (port); // Read the value of the port port FREELIBRARY (HLIBRARY); // Release the occupied memory}
Three, DLL application example source 1. Direct memory access DLL file source //.DEF LIBRARY MEMORYDLLDESCRIPTION 'DLL FOR MEMORY_READ_WRITE' EXETYPE WINDOWSCODE PRELOAD MOVEABLE DISCARDABLEDATA PRELOAD MOVEABLE SINGLEHEAPSIZE 1024 // DLL without its own stack, so there is no statement EXPORTS STACKSIZE . WEP @ 1 residentnameReadMemory @ 2WriteMemory @ 3 // CPP file #include
int FAR PASCAL MemoryRead (unsigned int DosSeg, unsigned int DosOffset) {WORD wDataSelector, wSelector; char far * pData; char value; wDataSelector = HIWORD ((DWORD) (WORD FAR *) & wDataSelector); wSelector = AllocSelector (wDataSelector); / / Assign selector setSelectorLimit (WSELECTOR, 0X2000); // Save the boundate setSelectorBase ((DWORD) DOSSEG << 4) (DWORD) DOSOFFSET); // 基地 address PDATA = (Char Far * (DWORD) WSELECTOR << 16); value = * pdata; freselector (WSELector); // Release selector return (value);
void FAR PASCAL MemoryWrite (unsigned int DosSeg, unsigned int DosOffset, char Data) {WORD wDataSelector, wSelector; char far * pData; wDataSelector = HIWORD ((DWORD) (WORD FAR *) & wDataSelector); wSelector = AllocSelector (wDataSelector); SetSelectorLimit (Wselector, 0x2000); SetSelectorBase ((DWORD) DOSSEG << 4) (DWORD) DOSOFFSET; PDATA = (DWORD) WSELECTOR << 16); * pdata = data; Freeselector (WSELECTOR);
INT FAR PASCAL WEP (INT NPARAM) {Return 1;}
2. DLL source code //.DEF file library portdlldescription 'DLL for port_in_out' EXTYPE WINDOWSCODE PRELOAD MOVEABLEDATA PRELOAD MOVEABLEDATA PRELOAD MOVEABLE SINGLEHEAPSIZE 1024
//.Cpp file #include
int FAR PASCAL LibMain (HINSTANCE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine) {if (wHeapSize = 0!) UnlockData (0); return 1;} int FAR PASCAL _export PortOut (int port, unsigned char value) {outp ( Port, value); Return 1;}
INT FAR PASCAL _EXPORT PORTIN (INT port) {int result; result = INP (port); return (result)
INT FAR PASCAL _EXPORT WEP (INT NPARAM) {Return 1;}
The above two instances of the .def file and .cpp file each make up a .prj file, and compile the link into a .exe or .dll file, you can call it in the application.
Fourth, the end language is above, we use DLL technology to easily implement the direct access and port I / O access to memory in the Windows environment, equivalent to these two examples, and more suitable for your own application system DLLs, such as port operations for data acquisition cards and extended memory area access, video zone buffers, and BIOS data area operations. If necessary, just update the DLL directly, and use not to make any changes to the application itself, you can make a big improvement in the application's function and user interface, and implement the version upgrade. Therefore, mastering DLL technology is very beneficial to Windows program developers.