Mixed Programming of Delphi and Fortran Language (2)

zhaozj2021-02-16  45

2. Call Dynamic Link Library (DLL) mode

The second method is more troublesome than the first method. In this method, the Fortran program is first compiled into a dynamic link library file (DLL, DYNAMIC-LINK LIBRARY) of the Windows standard, and then calls in Delphi. In the Fortran language program design, this paper uses the Compaq Visual Fortran6.6 compiler to easily generate a dynamic link library.

In this manner, in this way, due to data exchange in memory between two different languages, the data type must be in one by one. Due to the same storage mode corresponding to the data type of different languages, the data transfer method is not the same, and the program debugging needs to be carried out in two different compilers, so this method is compiled and commissioned, and it is not easy to solve some problems in the compilation. .

In the Fortran subroutine generating a dynamic link library, the following methods must be used:

! Dec $ Attributes Dllexport :: SUB_NAME! Dec $ Attributes Alias: 'Sub_AliasName' :: SUB_NAME

In the first sentence above, the keyword DLLEXPORT indicates that this subroutine can be called outside the dynamic link library, and SUB_NAME is the program name of this subroutine in the dynamic link library; the second sentence in the second sentence gives the program name Assign an alias because Fortran defaults to compiler named uppercase letters, and the alias can be changed.

In the call of the function and program, there are two ways to pass. One is the way to pass the parameter address, that is, the call by Reference, the other is the way to pass the value, ie the Call by value. When the CVF generates a dynamic link library, the default communication protocol is '_stdcall', and its parameter transmission is the first. In Delphi, the transmission method of the parameter is related to the type of the parameter itself. If there is a process definition:

Procedure sub_name (Const X1, X2: Double; VAR X3, X4: Double);

Here, X1, X2 is defined as a double-precision real number of constant types, and its parameter transmission mode is the second, that is, Call By Value, its value remains unchanged in the transfer; X3, X4 is a double precision integer of the variable type, The transfer method is the first, ie the call by reference, and its value can be changed in the calculation *. By default, that is, the parameters that belong to the parameter are not explained, the default is the constant type.

You can also use the pointer variables in Delphi to get the same call mode as the FORTRAN default condition:

Procedure sub_name (x1, x2: Pointer; var x3, x4: double);

Pointer indicates that X1, X2 is a non-type pointer (there is also a type pointer, here is omitted), if four double precision numbers A1, A2, A3, A4 are defined in another program, as follows:

Sub_name (@ a1, @ A2, A3, A4)

The values ​​in A1, A2, A3, and A4 can be changed before and after the calculation.

The transfer of pointer variables as virtual gaminous for Delphi and Fortran is very meaningful. Because Delphi's array and FORTRAN are different, the way the fixed array and dynamic arrays in Delphi are different. With a pointer, there is no matter if it is. As in the above example, if A1, A2 is an array, then its call mode is:

SUB_NAME (@ a1 [0], @ a2 [0], A3, A4)

That is to pass the address of the first array element. If it is not the address of the first element, it is the address of a certain element, and the combination of the virtual factor group starts with this element. There are two ways of delivery if a string occurs in virtual ginseng. One is based on standards in Fortran, while transmitting strings content and length, at this time, the substruction in Fortran is defined as:

Subroutine Sub_name (x1, x2, str, x3, x4)

! Dec $ Attributes Dllexport :: SUB_NAME! Dec $ attributes alias: 'sub_name' :: SUB_NAME

Real (8), DIMENSION (:) :: x1, x2

Character (len = *) STR

Real (8), INTENT (OUT) :: x3, x4

The corresponding interface process in Delphi is defined as:

Procedure sub_name (const x1, x2: Pointer;

Str: string;

Len: integer;

Var x3, x4: double);

Then call as follows:

SUB_NAME (@ a1 [0], @ a2 [0], STR, Length (STR), A3, A4);

Another way is easier, because in Delphi, the string is treated as a dynamic array, passed in the call by Refence mode, so before the compilation word will be used to transfer the strings into address delivery in Fortran. which is:

Subroutine Sub_name (x1, x2, str, x3, x4)

! Dec $ Attributes Dllexport :: SUB_NAME! Dec $ attributes alias: 'sub_name' :: SUB_NAME

Real (8), DIMENSION (:) :: x1, x2

Character (len = *) STR

Real (8), INTENT (OUT) :: x3, x4

! Dec $ Attributes Refence :: Str

At this time, the corresponding interface process in Delphi is defined as:

Procedure sub_name (const x1, x2: Pointer; str: string; var x3, x4: double);

Then call as follows:

SUB_NAME (@ a1 [0], @ a2 [0], str, A3, A4);

The implementation process of the second mixed programming method is: First, the Fortran subroutine is defined in the above method, and compile into dynamic link library Forsub.dll; then define the dynamic link library subsection procedure interface in Delphi as follows. Define the header of the process in the interface area:

Procedure sub_name (const x1, x2: Pointer;

Str: string;

Var x3, x4: double); stdcall;

Add the following statement in the Implementation:

Procedure sub_name; external 'forsub.dll' Name 'Sub_Name';

This way, in the Delphi program design, you can call Sub_Name as in calling your own subroutine.

Since the FOR90 introduces the module (Module) unit, some associated data and method can be packaged in the module, so the different subroutines in one module are defined, and more than one can be obtained in a dynamic link library. Subprints called by external programs. In Delphi, these subroutines are called, and each of the sub-process interfaces in Delphi are required. * In the FOR90, the definition method of distinguishing the input output parameter is introduced, and the keyword INTEN is introduced. In the parameter definition, INTENT (in) indicates that the parameter does not change during the transfer process, INTENT (OUT) is indicated by Changed, and the FOR90 is more stringent.

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

New Post(0)