Export function in DLL with .def file

xiaoxiao2021-03-06  41

Usually we need to be in the following three directories at the time of calling the DLL:

(1) System directory of Windows: / windows / system;

(2) Any directory pointed out in DOS;

(3) The directory where the program is located.

First, the structure of the dynamic link library

Dynamic link library defines two functions: export function and internal functions,

The export function can be called by other modules, and the internal function can only be used inside the library. When we use C to customize dynamic library files,

The need to be written is the module definition file (.def) that includes the export function table and the C file that implements the export function function. Below

Sample.dll Take the architecture of the DEF file and the structure of the file.

1. Module Definition File (.def) is a text file for one or more module statements used to describe the DLL attribute, each DEF text

Parts must contain at least the following module definition statement:

· The first statement must be a library statement, pointing out the name of the DLL;

· The exports statement lists the name of the exported function;

· Use the description statement to describe the use of DLL (optional);

• ";" commented on a line (optional).

2. Implement files

Implement the CPP file of the entry table function, including the API function of the DLL entry processing and the code for exporting the function.

Second, create Sample.dll

1. First create a Sample.dll project, start VC 5.0, follow the steps below to generate a DLL project:

· Select File / New / Project in the menu;

· Select Win32 Dynamic-Link Library in the list of project;

· Enter the project name in Project Name: Sample;

· Click the right button on the Location, select the C: / Sample directory;

· Click OK to complete, which has created the project file for Sample.dll.

2. Create a sample.def file:

· Select File / New / Text File in the menu;

· Save the file name "Sample.def" after entering the code:

; Sample.def

Indicate the name of the DLL, the linker put this name into the DLL import library

Library Sample

; Define the export function showme () as an example

Exports

SHOWME

; DEF file end

3. Create Sample.cpp

Select File / New / C Source File item in the menu

. After entering the following code, save the file name "Sample.cpp"

//Sample.cpp

#include

INT Showme (Void);

// DLLENTRYPOINT is the DLL entry point function, responsible for initialization and terminating DLL

Bool WinAPI DLLENTRYPOINT (Hinstance HDLL, DWORD DWREASON, LPVOID RESERVED)

{Switch (dwreason)

{Case DLL-Process-attach:

{Break;

Case DLL-Process-Detach:

{Break;}}

Return True;}

Int Showme (Void)

{// buzzer ring

MessageBeep (Word) -1);

Messagebox ("Hello!");

Return 1;}

4. Compile DLL files

Select Build Sample.dll from the Build menu, generate the sample.dll file, you can call it at any time.

Third, call the DLL file in the application

In the application, you must call the function in the export table in the application, for example, create a dialog-based project TEST with MFC, and place the "Load" button on the dialog box, you must add the loading code.

1. First add variable settings code in TestDlg.cpp:

// Set global variable glibsample for storing DLL handles

Hinstance glibsample = NULL;

// Second variable showME is a pointer to the showme () function in the DLL library

TypedEf Int (Showme) (Void);

SHOWME SHOWME;

2. Use the classwizard to add a loaded DLL code using the ClassWizard:

Void ctestdlg :: OnloadButton ()

{// The code to be added is as follows

IF (Glibmydll! = NULL)

{MessageBox ("The Sample.dll Has Already Been Load.");

Return;}

// Load Sample.dll, unprocessed path, will look in three default paths

Glibsample = loadLibrary ("Sample.dll");

/ / Return to the address of the showme () function in the DLL

SHOWME = (Showme) GetProcaddress (Glibsample, "ShowMe");

3. As long as the DLL load is successful, you can call the showme () function directly (this program run in Windows 95, VC 5.0).

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

New Post(0)