Call DLL in Delphi

xiaoxiao2021-03-06  34

Static call DLL TOP in Delphi

Calling a DLL is easier than writing a DLL. First, give you a static call method, will introduce the dynamic call method and make a comparison on both ways. The same, I

They first give examples 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}

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: = INTTOSTR (Testdll (1));

End.

In the above example, we placed a edit box (Edit) and a button (Button) on the form, and a little code was written to test our Delphi.dll we have just prepared. Big

Home can see our unique work is to put the test portion of the TestDll function in the Implementation and use the External statement to specify the location of Delphi.dll. (In this case

The calling program and Delphi.dll are in the same directory. ) Excited is that the TestDLL function we have written quickly was quickly recognized by Delphi. You can do such an experiment: enter "

TestDLL (", very fast Delphi will use the fly-by prompt to prompt what the parameters you should enter is as simple as the other functions we defined in Delphi. Notes are below:

First, call the parameters with stdcall. As mentioned earlier, the STDCALL parameters should be used when the functions in the DLL are referenced, the causes are the same as those mentioned above.

Second, use the External statement to specify the path and name of the called DLL file. As everyone saw, we specify the name of the DLL file to be called in the External statement. There is no write path because the DLL file and the main program called it in the same directory. Such as

If the DLL file is in C: /, we can write the above reference statement as External 'c: /delphi.dll'. Note the suffix of the file. DLL must be written.

Third, the global variable cannot be called from the DLL. If we declare some global variables in the DLL, such as: Var S: byte. This is in the DLL, the global variable can be used normally, but the s cannot be used by the call, both cannot

Pass as a global variable to the calling program. However, the variable declared in the calling program can be passed to the DLL as a parameter.

Fourth, the DLL called the call must exist. This is very important. You must exist if you request the DLL file called when using a static call method and the function or process to be called. If there is no or the specified path and file name is incorrect

If you run the main program, the system prompts to "start the program" or "No * .dll file" and other running errors.

<2> Diversity Call DLL TOP in Delphi

Dynamic call DLL is relatively complicated, but very flexible. In order to fully explain the problem, this time we will give 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, we still refer to the above

The calling program is just replacing the statement in the original Button1Click process with the following code.

procedure TForm1.Button1Click (Sender: TObject); type TIntFunc = function (i: integer): integer; stdcall; var Th: Thandle; Tf: TIntFunc; Tp: TFarProc; begin Th: = LoadLibrary ( 'Cpp.dll'); {Loading DLL} if TH> 0 THEN TRY TP: = GetProcaddress (th, pchar ('testc')); if TP <> nil dam tf: = TintFunc (TP); Edit1.Text: = INTOSTR (TF (1 )); {Call TESTC function} END ELSE ShowMessage ('TestC function is not found "; Finally Freelibrary (TH); {Release DLL} end else showMessage (' cpp.dll did not find '); end;

Everyone has seen, this dynamic calling technology is very complicated, but as long as the parameters are modified, if the DLL name in LoadLibrary ('cpp.dll') is 'delphi.dll', you can change

The DLL calling is called.

1. Define the type of function or process to be called. In the above code we defined a TintFunc type, which corresponds to the function TESTC we want to call. Do the same definition work in other calls. And also add

STDCALL call parameters.

Second, release the called DLL. We use LoadLibrary dynamics to call a DLL, but to remember that the DLL must be released in manually after use, otherwise the DLL will always occupy the memory until you retire

Windows or shutdown.

Now let's evaluate the advantages and disadvantages of two ways to call DLL. The static method is simple, easy to master and generally a little faster, but also more secure and reliable; but static side

The method cannot be flexibly loaded and unloaded at runtime, but the specified DLL is loaded at the main program to release the DLL until the program ends, only the compiler and linker

The system (such as Delphi) can use this method. The dynamic method solves the shortcomings in the static method, which can easily access functions and processes in the DLL, and even some old versions.

Newly added functions or processes in DLL; however, the dynamic approach is difficult to fully grasp. Different functions or processes must define a lot of complicated types and call methods due to different functions or processes. For beginners, the author

It is recommended that you use a static method to use a dynamic calling method after you are skilled.

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

New Post(0)