Search code examples
delphirttidwscript

Call a procedure of a class from DWScript


How can I call a procedure from a class that is created in main Form. Can it be done like this pseudo code shows?

type
  TDemo = class
    procedure test;
    constructor Create;
    destructor Destroy; override;
  end;

var
  Form28: TForm28;
  Demo:TDemo;

implementation

{$R *.dfm}

procedure TForm28.Button1Click(Sender: TObject);
var
   prog : IdwsProgram;
   exec : IdwsProgramExecution;
begin
   Demo := TDemo.Create;
   prog := DelphiWebScript1.Compile('Demo.Test;');
   exec := prog.Execute;
end;

Solution

  • To do this, you first have to expose your native class to the script engine. Have a look at the TdwsUnit component. It declares a script unit that interfaces with external code. You'd drop one on your form, define the class, define its methods, and hook up event handlers on the OnEval events that call the external routines.