Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework


Writing Dynamically Loaded Libraries



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə209/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   205   206   207   208   209   210   211   212   ...   294
DelphiLanguageGuide

Writing Dynamically Loaded Libraries
The following topics describe elements of writing dynamically loadable libraries, including
The exports clause.
Library initialization code.
Global variables.
Libraries and system variables.
Using Export Clause in Libraries
The main source for a dynamically loadable library is identical to that of a program, except that it begins with the
reserved word library (instead of program).
Only routines that a library explicitly exports are available for importing by other libraries or programs. The following
example shows a library with two exported functions, 
Min
 and 
Max
.
library MinMax;
    function Min(X, Y: Integer): Integer; stdcall;
 begin
   if X < Y then Min := X else Min := Y;
 end;
 function Max(X, Y: Integer): Integer; stdcall;
 begin
   if X > Y then Max := X else Max := Y;
 end;
   exports
      Min,
   Max;
 begin
 end.
If you want your library to be available to applications written in other languages, it's safest to specify stdcall in the
declarations of exported functions. Other languages may not support Delphi's default register calling convention.
Libraries can be built from multiple units. In this case, the library source file is frequently reduced to a uses clause,
an exports clause, and the initialization code. For example,
181


library Editors;
  uses EdInit, EdInOut, EdFormat, EdPrint;
  exports
    InitEditors,
    DoneEditors name Done,
    InsertText name Insert,
    DeleteSelection name Delete,
    FormatSelection,
    PrintSelection name Print,
    .
                .
                .
    SetErrorHandler;
  begin
    InitLibrary;
  end.
You can put exports clauses in the interface or implementation section of a unit. Any library that includes such a unit
in its uses clause automatically exports the routines listed the unit's exports clauses without the need for an exports
clause of its own.
The directive local, which marks routines as unavailable for export, is platform-specific and has no effect in Windows
programming.
On Linux, the local directive provides a slight performance optimization for routines that are compiled into a library
but are not exported. This directive can be specified for stand-alone procedures and functions, but not for methods.
A routine declared with localfor example,
function Contraband(I: Integer): Integer; local;
does not refresh the EBX register and hence
cannot be exported from a library.
cannot be declared in the interface section of a unit.
cannot have its address taken or be assigned to a procedural-type variable.
if it is a pure assembler routine, cannot be called from another unit unless the caller sets up EBX.
A routine is exported when it is listed in an exports clause, which has the form
exports entry1, ..., entryn;
where each entry consists of the name of a procedure, function, or variable (which must be declared prior to the
exports clause), followed by a parameter list (only if exporting a routine that is overloaded), and an optional name
specifier. You can qualify the procedure or function name with the name of a unit.
(Entries can also include the directive resident, which is maintained for backward compatibility and is ignored by the
compiler.)
On the Win32 platform, an index specifier consists of the directive index followed by a numeric constant between 1
and 2,147,483,647. (For more efficient programs, use low index values.) If an entry has no index specifier, the routine
is automatically assigned a number in the export table.
Note:
Use of index specifiers, which are supported for backward compatibility only, is discouraged and may cause
problems for other development tools.
182


A name specifier consists of the directive name followed by a string constant. If an entry has no name specifier, the
routine is exported under its original declared name, with the same spelling and case. Use a name clause when you
want to export a routine under a different name. For example,
exports
DoSomethingABC name 'DoSomething';
When you export an overloaded function or procedure from a dynamically loadable library, you must specify its
parameter list in the exports clause. For example,
exports
Divide(X, Y: Integer) name 'Divide_Ints',
Divide(X, Y: Real) name 'Divide_Reals';
On Win32, do not include index specifiers in entries for overloaded routines.
An exports clause can appear anywhere and any number of times in the declaration part of a program or library, or
in the interface or implementation section of a unit. Programs seldom contain an exports clause.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   205   206   207   208   209   210   211   212   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin