Introduction to DLL Programming Technology and Application in Windows

Source: Internet
Author: User

Transferred from: Internet

Abstract:

This article introduces the basic application methods and applications of DLL technology in Windows programming, and provides the full source code for Direct Memory Access and two practical DLL on port I/O.

Keywords: DLL windows programming memory access I/O

1. Introduction

Because Windows provides an unprecedented standard user interface, graphic processing capabilities, and simple operations for the microcomputer, most programmers have switched to or are switching to Windows programming. In the programming tasks of many practical application systems designed by users, software access to hardware and memory resources is often required, for example, port I/O, DMA, interrupt, direct memory access, etc. It is easy to compile a DOS program, but it is difficult to compile a Windows program, especially a program in the WindowsNT environment.

Because Windows has the "device-independent" feature, it does not advocate dealing with the underlying things of the machine. If you directly use Windows API functions or I/O read/write commands for access and operations, when the program runs, protection mode errors or even crashes. In more serious cases, the system crashes. In Windows, how can we solve the above problems easily? Using DLL (Dynamic Link Libraries) technology is one of the good ways.

DLL is the most important component of windows. Many new functions and features in windows are implemented through DLL. Therefore, it is very important to master and apply it. In fact, Windows itself is composed of many DLL, its most basic three major component modules kernel, GDI and user are DLL, and all its library modules are also designed as DLL. All. DLL ,. DRV ,. fon ,. sys and many. the system files with Exe extension are all DLL files. If you open the WINDOWS \ SYSTEM directory, you can see many DLL modules. Although DLL runs under ring3 priority, it is still a simple way to implement hardware interfaces. Dll can have its own data segment, but does not have its own stack. It uses the same stack mode as the application that calls it, reducing the inconvenience of programming design. At the same time, a dll has only one instance in the memory, so that it can use the memory efficiently and economically. The Code encapsulation implemented by the dll makes the program concise and clear. In addition, it has the biggest feature, that is, the DLL compilation has nothing to do with the specific programming language and compiler, as long as it complies with the DLL development specifications and programming policies, and arranges the correct call interface, no matter which programming language is used, the DLL is universal. For example, the DLL program compiled in bc31 can be used in BC, Vc, VB, Delphi and Other Language Environments. In the bc31 environment, I compiled two DLL files for Direct Memory Access and port I/O in windows, which are used in applications of multiple self-made systems and run well.

Ii. dll creation and calling

DLL creation and calling methods are described in detail in many documents. To save space, here we will only make some major summaries.

1. dll Creation

The establishment of DLL is indispensable and necessary in the following aspects:

Entry function libmain ()

Like winmain () in a C program, Every time Windows loads a DLL, it executes the libmain () function, which is mainly used for initialization. The common form is:

Int far Pascal libmain (hinstance, word wdataseg, word wheapsize, lpstr lpszcmdline)

{

If (wheapsize! = 0) // move the local heap and Data Segment

Unlockdata (0 );

// Unlock the Data Segment

/* Perform initialization necessary for some users */

Return 1; // initialization successful

}

Exit function WEP ()

When Windows detaches a DLL from the memory, it calls the corresponding exit function WEP (), which is mainly used for cleanup, such as releasing occupied memory resources and discarding some strings, bitmaps, and other resources; close open files.

Custom Output Functions

To allow applications in different memory segments to make remote calls, custom output functions must be defined as remote functions (using the far keyword) to prevent unexpected results from using short-range pointers; in addition, the Pascal keyword can accelerate the program running speed, make the code simple and efficient, and improve the program running speed.

Output Function Extraction Method

In the DLL module definition file (. Def), the output functions are listed one by the exports statement. For example:

Exports WEP @ 1 residentname

// Residentname can improve DLL efficiency and processing speed

Portin @ 2

Portout @ 3 // usually attaches a serial number to all output functions

Use the _ export keyword in the description of each output function definition.

You can select either of the above two methods, which cannot be repeated. The following two instances use the two different extraction methods.

2. dll call

When loading a DLL, Windows looks for the corresponding DLL in the following sequence:

. Current work disk.

Windows Directory; The getwindowsdirectory () function provides the path name of the directory.

The Windows System directory, that is, the system subdirectory. You can call the getsystemdiretory () function to obtain the path name of this directory.

All directories listed in the DOS path command.

All directories in the directory list of images in the network.

Call method of the output function in the DLL module:

No matter which language is used to call the compiled DLL, there are basically two call Methods: static call method and dynamic call method. Static call method: the compilation system loads the DLL and the encoding of the DLL uninstallation when the application ends. (if other programs use the DLL, the Windows application record for the DLL is reduced by 1, it will not be released until all related programs end using the DLL). It is simple and practical, but not flexible enough to meet general requirements. The dynamic calling method is implemented by the programmer using API functions to load and uninstall the DLL. It is complicated to use, but can use the memory more effectively, is an important way to compile large-scale applications. Specifically, the following method can be used for calling. In the application module definition file, use the imports statement to list the function names of the DLL to be called. For example:

Imports

Memorydll. memoryread

Memorydll. memorywrite

Dynamically link the application program to the DLL module

First load the DLL in loadlibrary, then use the getprocaddress function to obtain the address of the output function, and obtain its pointer for calling. For example:

Handle hlibrary;

Farproc lpfunc;

Int portvalue;

Hlibrary = loadlibrary ("portdll. dll ");

// Load the DLL

If (hlibrary> 31) // load successful

{

Lpfunc = getprocaddress (hlibrary, "portin ");

// Retrieve the portin function address

If (lpfunc! = (Farproc) null)

// Call if the check is successful

Portvalue = (* lpfunc) (port); // read port value

Freelibrary (hlibrary );

// Release the occupied memory

}

Iii. dll application source code

1. DLL source code accessed by Direct Memory

//. Def File

Library

Memorydll

Description 'dll for memory_read_write'

Exetype windows

Code

Preload moveable discardable

Data preload moveable single

Heapsize 1024

// DLL does not have its own stack, so there is no stacksize statement

Exports WEP @ 1 residentname

Readmemory

@ 2

Writememory @ 3

//. Cpp File

# Include <windows. h>

Int far Pascal libmain (hinstance, word wdataseg, word wheapsize, 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;

Wdataselector = hiword (DWORD) (word far *) & wdataselector );

Wselector = allocselector (wdataselector );

// Assign Selector

Setselectorlimit (wselector, 0x2000 );

// Set the access limit

Setselectorbase (wselector, (DWORD) dosseg) <4) + (DWORD) dosoffset );

// Set the base address

Pdata = (char far *) (DWORD) wselector <16 );

Value = * pdata;

Freeselector (wselector );

// Release the 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 (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 for reading and writing I/O through the port

//. Def File

Library portdll

Description 'dll

Port_in_out'

Exetype windows

Code preload moveable discardable

Data

Preload moveable single

Heapsize 1024

//. Cpp File

# Include

<Windows. h>

# Include <dos. h>

Int far Pascal

Libmain (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 = Indium (port );

Return (result );

}

Int far Pascal _ export WEP (INT nparam)

{

Return 1;

}

Separate. def file and. the CPP file is composed of one. prj file, and compile the link. EXE or. the DLL file can be called in the application.

Iv. Conclusion

Above, we use DLL technology to conveniently implement direct access to memory and access to port I/O in windows, you can also compile more DLL suitable for your application system, for example, many practical programming tasks such as port operations for data acquisition card and extended memory access, video zone buffer and BIOS data zone operations. If necessary, you only need to update the DLL directly. without making any changes to the application itself, you can greatly improve the functions and user interfaces of the application to upgrade the version. Therefore, mastering the DLL technology is very beneficial to Windows program developers.

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.