delphidwscript

Delphi Web Script: How to call global TForm object's property in script?


I've registered TForm class and its 'caption' property, then I register a global variable:

NewGlobal := DwsUnit.Variables.Add;
NewGlobal.Name := 'MainForm';
NewGlobal.DataType := 'TForm';
NewGlobal.OnReadVar := GlobalReadProc;

GlobalReadProc code:

GlobalReadProc(Info: TProgramInfo;var Value: Variant);
begin
    TVarData(Value).VType := varUnknown;
    IUnknown(TVarData(Value).VUnknown) := TForm(Form1);
end;

In the script I call my MainForm variables property

MainForm.Caption := ''DWS Script in work'';

DWScript shows exception:

interface not supported.

What am I doing wrong? (I use IUnknown because I've seen that typecast in VarCopySafe procedure, and I get an exception when VType is not varUnknown)


Solution

  • The return value should be the script representation of your object; An IScriptObj.

    The following works for me:

    GlobalReadProc(ProgramInfo: TProgramInfo; var Value: Variant);
    var
      Info: IInfo;
    begin
      Info := ProgramInfo.ResultVars.GetConstructor('Create', Form1).Call;
      Value := Info.ScriptObj;
    end;
    

    Personally I would use an Instance or a function instead of a global variable.