Delphi 系统[28]关键字和保留字 index、near、far、export、exports、external、name、resident

Delphi 系统[28]关键字和保留字  index、near、far、export、exports、external、name、resident

1、定义:

  • index :用于在属性中标识序号,以便用相同的属性方法(Get,Set)对不同的属性进行操作。index 关键字也用于在属性中指出多个元素。
  • near :标明函数的调用协定,指出函数可以被本地调用。其他程序可以用 dll 的形式调用程序内的函数,保留它是为了向下兼容。
  • far :标明了函数调用协定,指出函数可以被远程调用。其他程序可以用 dll 的形式调用程序内的函数,保留它是为了向下兼容。
  • export :标明了函数的调用协定,指出函数可以被输出,输出的函数能被本地或远程调用。其他程序可以用 dll 的形式调用程序内的函数,保留它是为了向下兼容。
  • exports :用于输出对象,它必须被用在接口和实现之间,可以同时输出多个项,项与项之间用逗号分开。
  • external :用于引用一个外部的或是 OBJ 内的方法。使用 external 关键字时,代码必须注意大小写,否则将出现错误。
  • name :用于指出方法的别名,对于一个要被外部引用的方法,建议用 name 申请方法别名,以避免外部程序改动方法的实体内容。从外部引用一个方法时,如果该方法有别名,则必须用 name 进行标识。
  • resident :使用 resident,则当 DLLs 装入时,特定的输出信息始终保持在内存中。这样当其它应用程序调用该过程时,可以比利用名字扫描 DLL 入口降低时间开销。对于那些其它应用程序常常要调用的过程或函数,使用 resident 指示是合适的。这个关键字已经被废弃不用了。

2、示例:

{ index:属性序号 }
type
  TMyObject = class(TObject)
  private
    FLeft: Integer;
    FTop: Integer;
    FWidth: Integer;
    FHeight: Integer;
    function GetInfo(const Index: Integer): Longint;
    procedure SetInfo(const Index: Integer; const Value: Longint);
  public
    property iLeft: Longint index 0 read GetInfo write SetInfo;
    property iTop: Longint index 1 read GetInfo write SetInfo;
    property iWidth: Longint index 2 read GetInfo write SetInfo;
    property iHeight: Longint index 3 read GetInfo write SetInfo;
  end;

function TMyObject.GetInfo(const Index: Integer): Longint;
begin
  case Index of
    0: Result := FLeft;
    1: Result := FTop;
    2: Result := FWidth;
    3: Result :=FHeight;
  end;
end;

procedure TMyObject.SetInfo(const Index: Integer; const Value: Longint);
begin
  case Index of
    0: FLeft := Value;
    1: FTop := Value;
    2: FWidth := Value;
    3: FHeight :=Value;
  end;
end;

----------------------------------------------------------------------------------------
{ index:属性的多个元素 }
type
  TMyObject = class(TObject)
  private
    FList: TStringList;
    function GetItem(Index: Integer): string;
    procedure SetItem(Index: Integer; const Value: string);
  public
    constructor Create;
    destructor Destroy; override;
    property Items[Index: Integer]: string read GetItem write SetItem;
  end;

constructor TMyObject.Create;
begin
  inherited;
  FList := TStringList.Create;
  FList.Add('星期一');
  FList.Add('星期二');
  FList.Add('星期三');
  FList.Add('星期四');
  FList.Add('星期五');
  FList.Add('星期六');
  FList.Add('星期日');
end;

destructor TMyObject.Destroy;
begin
  FList.Free;
  inherited;
end;

function TMyObject.GetItem(Index: Integer): string;
begin
  if (Index >= 0) and (Index <= (FList.Count - 1)) then
    Result := FList[Index]
  else
    Result := 'Out of Index';
end;

procedure TMyObject.SetItem(Index: Integer; const Value: string);
begin
  if (Index >= 0) and (Index <= (FList.Count - 1)) then
    FList[Index] := Value;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  MyObj: TMyObject;
begin
  MyObj := TMyObject.Create;
  try
    Caption := MyObj.Items[2];
    MyObj.Items[2] := 'Wednesday';
    for I := 0 to 6 do
      ShowMessage(MyObj.Items[I]);
  finally
    MyObj.Free;
  end;
end;

---------------------------------------------------------------------------------------
{ near }
function Add(A, B: Integer): Integer; near;
{ 如果这个程序被编译为 Test.exe,并且另一个处于本地的程序需要调用这个函数,可以使用以下语句 }
function Add(A, B: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ far }
function Add(a,b: Integer): Integer; far;
{ 如果这个程序被编译为 Test.exe, 并且另一个处于其他计算机的程序需要调用这个函数, 可以使用以下语句 }
function Add(a,b: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ export }
function Add(a,b: Integer): Integer; export;
{ 如果这个程序被编译为 Test.exe, 而另一个程序需要调用这个函数,可以使用以下语句 }
function Add(a,b: Integer): Integer; stdcall; external 'Test.exe';

----------------------------------------------------------------------------------------
{ exports }
library Test;

function TestFunc(I: Integer): string; stdcall;
begin
  Result := IntToStr(I);
end;

exports TestFunc;

begin

end.

{ name 如果输出的对象被重载,则必须给对象起个别名,并注明参数 }
library Test;

function TestFunc(I: Integer): string; overload; stdcall;
begin
  Result := IntToStr(I); 
end;

function TestFunc(S: string): Integer; overload; stdcall;
begin
  Result := StrToInt(S);
end;

exports
  TestFunc(I: Integer) name 'TestFunc1',
  TestFunc(S: string)  name 'TestFunc2';

begin

end.

----------------------------------------------------------------------------------------
{ external }

{$L Test.OBJ}
procedure TestFunc(I:Integer); external;

{ 如果是从 dll 或外部程序中引用,则可以使用以下代码 }
function TestFunc(FileName: string): string; external 'Test.dll';

{ 如果被引用的函数被重载,则必须另外指出引用的名称 }
function MyFunc1(Code: Integer): string; overload; stdcall; external 'Test.dll' name 'TestFunc1';
function MyFunc2(Name: string): Integer; overload; stdcall; external 'Test.dll' name 'TestFunc2';

----------------------------------------------------------------------------------------
{ name }
function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';

----------------------------------------------------------------------------------------
{ resident }
function Test: string;exports Test name 'MyTest' resident;   //编译时会给出警告:Symbol 'RESIDENT' is deprecated

  

 

 

 

 

 

 

创建时间:2021.08.16  更新时间:

posted on 2021-08-16 15:52  滔Roy  阅读(560)  评论(0编辑  收藏  举报

导航