fastscript调用delphi方法和DELPHI调用FASTSCRIPT方法

fastscript调用Delphi过程:  
1. 先创建事件处理方法:TfsCallMethodEvent 
2. 然后再用调用TfsScript.AddMethod方法,第一个参数为Delphi方法的语法,第二个参数为TfsCallMethodEvent链接的一个句柄。 如在Delphi有一个过程为DelphiFunc, ….. 
procedure TForm1.DelphiFunc(s: String; i: Integer);  begin  
  ShowMessage(s + ', ' + IntToStr(i));  end;   
{TfsCallMethodEvent} 
function TForm1.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String;  
  var Params: Variant): Variant;  begin  
 if MethodName = 'DELPHIFUNC' then  //注意方法名称都为大写比较。   DelphiFunc(Params[0], Params[1]);  end;  
procedure TForm1.Button1Click(Sender: TObject); begin 
  { clear all items }   

fsScript1.Clear;   

{ script text } 

  fsScript1.Lines := Memo1.Lines; 
  { frGlobalUnit contains standard types and functions }  

 fsScript1.Parent := fsGlobalUnit; 
  { make DelphiFunc procedure visible to a script }   

fsScript1.AddMethod('procedure DelphiFunc(s: String; i: Integer)', CallMethod);  
  { compile the script }   

if fsScript1.Compile then     

fsScript1.Execute else  

 { execute if compilation was succesfull }    

 ShowMessage(fsScript1.ErrorMsg);

 { show an error message } 

end; 

 

 

DELPHI调用FASTSCRIPT脚本

例如:如果在动态脚本里面有一个'ScriptFunc'的一个过程,则在delphi代码里需要写如下: 
  fsScript1.Clear;   { script text } 
  fsScript1.Lines := Memo1.Lines; 
  { frGlobalUnit contains standard types and functions }   fsScript1.Parent := fsGlobalUnit; 
  { make DelphiFunc procedure visible to a script }  
  { compile the script }   if fsScript1.Compile then 
    { Call script function with one string parameter and one integer param }     fsScript1.CallFunction('ScriptFunc', VarArrayOf(['Call ScriptFunc', 1])) else     ShowMessage(fsScript1.ErrorMsg); { show an error message } end;  
例如动态脚本内容如下: 
procedure ScriptFunc(Msg: String; Num: Integer); begin 
  ShowMessage('1st param: ' + Msg +      '  2nd param: ' + IntToStr(Num)); end;  
begin 
  DelphiFunc('Call DelphiFunc', 1);  end. 

posted @   delphi中间件  阅读(2150)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示