DLL programming technology and applications under Windows

xiaoxiao2021-03-06  95

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.

I. Introduction

Since Windows provides unprecedented standard user interfaces, graphics processing capabilities and simple flexible operations for microcomputers, most of the programming people have 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, DLL establishment and call

The establishment and calling method of the DLL has a detailed introduction in many information, in order to save space, only some of the main summary here.

1.DLL establishment

Regarding the establishment of the DLL, it is an indispensable and must be mastered by the following aspects.

The entry function libmain () is like WinMain () in the C program, and Windows executes the libmain () function each time the DLL is loaded, mainly for some initialization work. The usual form is: int FAR Pascal Libmain (Hinstance Hinstance, Word WDataseg, Word Wheapsize, LPSTR LPSZCMDLINE) {if (WHEAPSIZE! = 0) // Make local heap, data segment can be moved unlockdata (0); // Unlock data segment / * Some user necessary initialization work * / return 1; // initialization success} Exit function WEP ()

When Windows uninstall the DLL from the memory, call the corresponding export function WEP (), mainly do some cleaning work, such as the release of the memory resources; discard some strings, bitmaps and other resources; turn off the open file, etc.. Custom output function

In order 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 to use the short-range pointer; at the same time, plus the pASCAL keyword The running speed of the program can be accelerated, so that the code is simple and efficient, and the program's running speed is improved.

Output function

In the DLL module definition file (.def) is listed by the exports statement on the output function one by one. E.g:

Exports WEP @ 1 residentname // residentname improves DLL efficiency and processing speed Portin @ 2 portout @ 3 // Topic to all output functions Add series number

Use the _export keyword to use the _export keyword in the description of each output function. 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. Such as:

Imports Memorydll.MemoryReadMemorydll.MemoryWrite Let the application run with the DLL module dynamic link first with loadLibrary, and then use the getProcAddress function to check the address of its output function to 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}

Third, DLL application instance source program

1. Direct memory access DLL source code

//.DEF file LIBRARY MEMORYDLLDESCRIPTION 'DLL FOR MEMORY_READ_WRITE' EXETYPE WINDOWSCODE PRELOAD MOVEABLE DISCARDABLEDATA PRELOAD MOVEABLE SINGLEHEAPSIZE 1024 // DLL without its own stack, there is no STACKSIZE statement EXPORTS WEP @ 1 residentnameReadMemory @ 2WriteMemory @ 3 //.CPP file #includeint FAR Pascal libmain (Hinstance Hinstance, Word WDataseg, Word Whaapsize, LPSTR LPSZCMDLINE)

{

IF (WHEAPSIZE! = 0)

UNLOCKDATA (0);

Return

1;

}

INT FAR PASCAL MEMORYREAD (Unsigned Int Dosseg, unsigned int dosoffset)

{

Word WDataSelector, Wselector;

Char far * pdata;

CHAR VALUE;

WDASELECTOR = HiWord (Word Far *) & WDASELECTOR);

Wselector = allocselector (WDASELector);

// Assign selector

SetSelectorLimit (WSELECTOR, 0X2000);

// Save the boundary

SetSelectorBase (WSELECTOR, ((DWORD) Dosseg << 4) (DWORD) DOSOFFSET;

// 基 site

PDATA = (CHAR FAR *) (DWORD) WSELECTOR << 16);

Value = * pdata;

Freeselector (WSELECTOR);

// Release selector

Return (Value);

}

Void Far Pascal MemoryWrite (unsigned int dosoffset, char data)

{

Word WDataSelector, Wselector;

Char far * pdata;

WDASELECTOR = HiWord (Word Far *) & WDASELECTOR);

Wselector = allocselector (WDASELector);

SetSelectorLimit (WSELECTOR, 0X2000);

SetSelectorBase (WSELECTOR, ((DWORD) Dosseg << 4) (DWORD) DOSOFFSET;

PDATA = (CHAR FAR *) (DWORD) WSELECTOR << 16);

* pdata = data;

Freeselector (WSELECTOR);

}

INT FAR PASCAL WEP (INT NPARAM)

{

Return 1;

}

2. DLL source code reading and writing I / O

//.Def file library portdllDescription 'DLL for port_in_out' Extype Windowscode Preload Moveable DiscardableData Prelioad Moveable SingleHeapsize 1024 ///.cpp file #include

#include

Int Far Pascal

Libmain (Hinstance Hinstance, Word Word 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, conclude

On the top, we use DLL technology to facilitate access to direct access and port I / O access to memory in the Windows environment, and the DLLs that are suitable for your application system, such as Port operations for data acquisition cards and extended memory area access, video zone buffers, and BIOS data area operations are in programming tasks. 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.

转载请注明原文地址:https://www.9cbs.com/read-123806.html

New Post(0)