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、示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | { 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 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
分类:
Delphi A基础
标签:
Delphi
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报