Delphi Method and Some Precautions and Skills

xiaoxiao2021-03-06  97

The original article has many small problems, but this article is not lost is a DLL study base article. (Note: The problem in the article did not make any modifications) to pick it from: http://hanyi.coDelphi.com/jiqiao/26.html

The first chapter uses a dynamic link library (DLL)

Life DLL You must not be unfamiliar, there is a lot of documents with DLL suffix in Windows, which guarantees important guarantees for the normal operation and maintenance of Windows. (For example, there are more than 500 DLL files in the Win95 System directory.) Actually, the DLL is a special executable. It is especially mainly because it is generally not possible to run directly, and the host program can be used, such as a dynamic call of the * .exe program or other DLL. Simply put, in general, the DLL is a collection of compiled functions and processes.

There are several reasons for using DLL technology:

First, reduce the executable file size.

A large part of the production of DLL technology is to reduce the size of the executable. When the operating system enters the Windows era, its size has reached several tens of trillions or even hundreds. Imagine if a single executable file system using the DOS era may reach tens of megabytes, this is unacceptable. The solution is to use dynamic link technology to divide a large executable into many small executables.

Second, achieve resource sharing.

The resource sharing here includes many aspects, the most is the memory sharing, code sharing, and more. Early programmers often encounter such things and write the same code in different programming tasks. This approach is clearly wasting for many times, in order to solve this problem, people have written a variety of libraries. However, due to the different libraries of programming languages ​​and the environment generally universal, and users need these libraries when running the program, it is extremely inconvenient. The appearance of the DLL is like a standard that has been developed, so that these libraries have a unified specification. In this way, the programmer with different programming languages ​​can easily use the DLL written in other programming languages. In addition, the DLL has a prominent feature that is only loaded in memory, which can save limited memory and can serve multiple processes at the same time.

Third, easy to maintain and upgrade.

A careful friend may find that some DLL files are released. (check

Seeing the properties of the DLL file can be seen, but not every DLL file is available)

Easy to maintain and upgrade. For example, there is a bug in the early Win95.

It is not possible to display this day on February 29 in the leap year. Later, Microsoft was released.

A patch corrected this bug. It is worth mentioning that we have not reloaded

Win95, but use the new version of the DLL instead of the old version of the DLL. (Which one is

A DLL file author can't think of it. Another common example is a driving

Order upgrade. For example, the famous DirectX will upgrade many times, and now it has developed

The version is 6.0. What is even better, when we try to install a lower version of the DLL, the system

Will give us a prompt to avoid human operation errors. For example, we upgrade a hardware

When moving, frequent Windows prompts our currently installed driver than original

The driver is old.

Fourth, more secure.

The security mentioned here also includes many aspects. For example, the DLL file suffers from viruses.

The rate of violation is much lower than the ordinary EXE file. In addition, since it is a dynamic link,

This brings some anti-assessment of "master" "masters" engaged in destruction.

difficult.

Chapter II writes a DLL TOP in Delphi

Note: Here the author assumes that the reader is Delphi 3 or Delphi 4

The opening of the white said so much, the general is correct. Preparation DLL is actually not

A very difficult thing, just pay attention to some matter is enough. For the convenience of explanation,

Let's give an example. Library Delphi;

Uses

SYSUTILS,

Classes;

Function TestDLL (I: Integer): Integer; stdcall;

Begin

Result: = i;

END;

Exports

TestDLL;

Begin

End.

Is it very simple? Friends who are familiar with Delphi can see the above

The code and the general Delphi program are basically the same, just in TestDLL

After the function, there is a stdcall parameter and declare TestDLL with the exports statement.

function. As long as you compile the above code, you can get a named delphi.dll.

Dynamic link library. Now let's take a look at what you need to pay attention to.

First, the function or procedure written in the DLL must add the stdcall call parameters.

This call parameter is FAR in the Delphi 1 or Delphi 2 environment. From Delphi

3 will change this parameter for stdcall after the purpose is to use standard Win32.

Parameter transfer technology instead of optimized Register parameters. Forgot to use stdcall

The number is a common mistake, this error does not affect the compilation and generation of the DLL, but call

A very serious mistake occurs when using this DLL, causing the deadlock of the operating system. the reason

Is the register parameter is the default parameter of Delphi.

Second, the written function and process should be declared using an Exports statement as an external function.

As everyone see, the TestDLL function is declared as an external function. This

Make the function to see the function outside, the specific method is the right mouse button

"Quick View" function View the DLL file. (if there is not

The "Quick View" option can be installed from the Windows CD. ) TestDLL function

The appearance in the Export Table column. Another very reason is that if not like this

Declaration, the function we have written will not be called, this is everyone unwilling to see.

Third, when using a long string type, the variable is referenced to ShareMem.

The String type in Delphi is very powerful, we know the length of the normal string

Maximum is 256 characters, but the String type in Delphi is the length of the default.

To reach 2G. (Yes, you don't have a mistake, it is indeed two megabytes.) At this time, if you

The parameters of the String type are used, and when the variable is even recorded, it will be referenced.

The ShareMem unit and must be the first reference. Nothing after the Uses statement

A referenced unit. Such examples:

Uses

ShareMem,

SYSUTILS,

Classes;

Also, in your project file (* .dpr) is not a unit file (* .pas

) Also do the same job, this point of Delphi comes with the help files do not know

Chu, causing a lot of misunderstandings. If you don't do this, you are very likely to pay the generation.

price. Avoid using String types is to use the parameters, variables of the String type, etc.

Declare the type of PCHAR or ShortString (such as: S: String [10]). same

The problem will appear when you use a dynamic array, the method solution is described above.

Chapter III Static call DLL TOP in Delphi

Calling a DLL is easier than writing a DLL. First introduce you to everyone is

Static calling method, will introduce the dynamic calling method, and do one by two ways

Comparison. Similarly, we first raised an example of static calls.

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics,

Controls, Forms, Dialogs, Stdctrls; Type

TFORM1 = Class (TFORM)

EDIT1: TEDIT;

Button1: tbutton;

Procedure Button1Click (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMentation

{$ R * .dfm}

// The following code is really handwritten for us.

Function TestDLL (I: Integer): Integer; stdcall;

EXTERNAL 'DELPHI.DLL';

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Edit1.text: = INTSTR (TestDLL (1));

END;

End.

In the above example, we placed a edit box (Edit) on the form and one

Button, and write very little code to test we have just written

Delphi.dll. Everyone can see that our only work is to let TestDLL letters

The description portion of the number is placed in the Implementation and refers to the external statement.

Set the location of Delphi.dll. (In this example, the calling program and Delphi.dll are in the same

Directory. What is exciting is that the TestDLL function we have written now is very fast.

I was recognized by Delphi. You can do such an experiment: enter "TestDLL (",

Soon Delphi will use the fly-by prompt to prompt what you should enter,

As we are as simple as we are using other functions defined in Delphi. Precautions

Next:

First, call the parameters with stdcall.

As mentioned earlier, when references the functions in the DLL and procedures,

The stdcall parameters, the cause is the same as mentioned earlier.

Second, use the External statement to specify the path and name of the called DLL file.

As everyone saw, we specify what to call in the External statement.

The name of the DLL file. No write path is because the DLL file and the main program called it

Under the same directory. If the DLL file is in C: / U65292, we can use the above reference.

The sentence is written for external 'c: .dll'. Note the suffix of the file. DLL must be written

on.

Third, the global variable cannot be called from the DLL.

If we declare some global variables in the DLL, such as: Var S: byte.

In this way, the global variables in the DLL can be used normally, but s cannot be called

The program is used, and both S cannot pass it as a global variable to the calling program. But in the call

The variable declared in the program can be passed to the DLL as a parameter.

Fourth, the DLL called the call must exist.

This is very important, requiring the DLL file called when using a static call method

And the functions or processes to be called must exist. If there is no or designated road

If the diameter and file name are incorrect, the system will prompt when running the main program.

Error or "No * .dll file" and other running errors.

The fourth chapter is moving in Delphi DLL TOP

Dynamic call DLL is relatively complicated, but very flexible. For a comprehensive description

This issue, this time we raise an example of a DLL called by C .

First, compile the following DLL source program in C .

#include

Extern "C" _Declspec (DLLEXPORT)

Int WinAPI TestC (INT I)

{

Return I;

}

After compiling, generate a DLL file, here we call this file as cpp.dll, only one function TESTC returns an integer type in this DLL. For convenience, I

They still refer to the above call procedure, just in the original Button1Click process

The statement is replaced by the following code.

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Type

TintFunc = Function (i: integer): integer; stdcall;

VAR

TH: THANDLE;

TF: TINTFUNC;

TP: TFARPROC;

Begin

TH: = loadingLibrary ('cpp.dll'); {loaded DLL}

IF TH> 0 THEN

Try

TP: = GetProcaddress (TH, PCHAR ('TESTC'));

IF TP <> NIL

Then Begin

TF: = TintFunc (TP);

Edit1.text: = INTTOSTR (TF (1)); {Call TESTC function}

end

Else

ShowMessage ('TestC function is not found);

Finally

Freeelibrary (TH); {Release DLL}

end

Else

ShowMessage ('cpp.dll is not found);

END;

Everyone has seen, this dynamic calling technology is very complicated, but as long as the participation

Number, such as modifying the DLL name in LoadLibrary ('cpp.dll') 'delphi.dll'

Dynamically change the called DLL.

1. Define the type of function or process to be called.

In the above code we defined a TintFunc type, this is my corresponding me.

The function TESTC will be called. Do the same setting in other calls

Righteous work. And also add the stdcall call parameters.

Second, release the called DLL.

We use LoadLibrary dynamics to call a DLL, but to remember that

After using Freelibrary, it will be released by FreeLibrary, otherwise the DLL will

Direct space is stored until you quit Windows or shutdown.

Now let's evaluate the advantages and disadvantages of two ways to call DLL. Static party

The law is simple, easy to master and generally a little more fast, and more secure

Rely on some; but the static method cannot flexibly loading the DLL required for running time.

It is the release of the specified DLL until the program is started until the program starts running.

The DLL, which only has a system based on a compiler and a linker (such as Delphi).

Use this method. The dynamic method solves the deficiencies in the static method.

It can easily access the functions and processes in the DLL, even some old version DLLs.

Plus function or process; but the dynamic method is difficult to fully grasp, because of different

Functions or processes must define a lot of complicated types and call methods.

For beginners, the author recommends that you use a static method to use it after being skilled.

Normal modifier.

Chapter 5 Using DLL Practical Tips TOP

First, write skills.

1. In order to ensure the correctness of the DLL, you can write a general application.

Division, after debugging, then separated from the main program, compile into a DLL.

2, in order to ensure the versatility of the DLL, it should be defective in the DLL written in yourself.

The name of the control, such as: edit1.text; or customize

Non-Windows definition types, such as some record.

3, in order to facilitate debugging, each function and process should be as short as possible, and cooperate

Detailed detailed comments.

4, you should use TRY-Finally to handle possible errors and exceptions, pay attention to the SYSUTILS unit.

5, use less units to reduce the size of the DLL, especially not to reference

The unit, such as the Dialogs unit. For example, in general, we can not quote

The Classes unit, which allows the compiled DLL to reduce approximately 16 kB.

Second, call the skill.

1. When using a static method, you can rename the called function or process. in front of

In the DLL example mentioned in the C , if the Extern "C" statement, C will

Compile some strange function names, the original TestC function is named @ Testc $ S

Wait a lacker, this is because C uses C Name Mangling technology.

This function name is illegal in Delphi, we can solve this problem like this:

Rewriting the reference function

Function TestC (i: integer): integer; stdcall;

EXTERNAL 'CPP.DLL'; Name '@ TestC $ S';

Where Name's role is renamed.

2, put the DLL we have written in the Windows directory or Windows

Under contents. Do you can don't be in an external statement or a loadLibrary statement?

Write the path to write only the name of the DLL. But this is something wrong, in these two directories have

A large number of important system DLLs, if your DLL is renamed, the consequences are simply

Unimagically, and your programming technology does not reach a DLL that you write yourself.

To the point in the system directory!

Third, debugging skills.

1. We know that the DLL cannot run and single-step debugging when writing. Have a office

Method, then set a host program in the Run | Parameters menu.

Advance the name of the host program in the Host Application column of the local page

Bring a single step debugging, breakpoint observation and running.

2. Add a DLL version information. The release of the version information is very good for the DLL.

Important, if the version information is included, the size of the DLL adds 2KB. Add this

A little space is worth it. Unfortunately use Project | Options

The version of the Version option is not line in the menu, this point Delphi has no

It will be mentioned that the scriptist is found that as long as the code is added. Such examples:

Library Delphi;

Uses

SYSUTILS,

Classes;

{$ R * .res}

// Note that the above line of code must be added to this location

Function TestDLL (I: Integer): Integer; stdcall;

Begin

Result: = i;

END;

Exports

TestDLL;

Begin

End.

3, in order to avoid the name of the DLL, when the DLL written to himself starts with the name

It is best to use characters and underscore mixing. Such as: JL_TRY16.DLL.

4, if you have compiled some DLLs in Delphi 1 or Delphi 2

If you have a DLL you originally compiled is 16. As long as the source code is in the new Delphi 3

Or recompilation in the Delphi 4 environment, you can get 32-bit DLL.

[Post ": In addition to the most commonly used method described above, DLL can also be used

The carrier of the resource. For example, change the icon in Windows is in the DLL used.

H. In addition, skilled in grasping DLL design technology, more advanced use

OLE, COM, and ActiveX programming have a lot of benefits.

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

New Post(0)