Is it possible to call a Delphi method with a variable number of arguments from IActiveScript?

I am using Delphi 2006 and psvActiveScript .

Example:

unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ObjComAuto, ComObj, psvActiveScript; type TForm1 = class(TForm) btnExecute: TButton; procedure btnExecuteClick(Sender: TObject); procedure ASWError(Sender: TObject; Line, Pos: Integer; ASrc, ADescription: String); end; var Form1: TForm1; ASW: TpsvActiveScriptWindow; implementation {$R *.DFM} type {$METHODINFO ON} TMySriptableClass = class(TObjectDispatch) public constructor Create; procedure Alert(msg: string); // THIS OR SOMETHING SIMILAR NOT POSSIBLE? ********************** function FnWithVarNumOfArgs(const args: array of string): string; // ************************************************************** end; {$METHODINFO OFF} constructor TMySriptableClass.Create; begin inherited Create(Self, False); end; procedure TMySriptableClass.Alert(msg: string); begin ShowMessage(msg); end; function TMySriptableClass.FnWithVarNumOfArgs(const args: array of string): string; begin Result := 'OK'; end; procedure TForm1.btnExecuteClick(Sender: TObject); var MyObj: TMySriptableClass; begin ASW := TpsvActiveScriptWindow.Create(self); ASW.ScriptLanguage := 'JScript'; ASW.OnError := ASWError; MyObj := TMySriptableClass.Create; ASW.AddNamedItem('MyObj', MyObj); try ASW.Execute( 'MyObj.Alert("Warning: Here comes Error");'+ 'MyObj.FnWithVarNumOfArgs("1","2","3")' ); finally ASW.Free; end; end; procedure TForm1.ASWError(Sender: TObject; Line, Pos: Integer; ASrc, ADescription: String); begin Showmessage(ADescription + ': ' + ASrc); end; end. 
+4
source share
2 answers
 [...] uses activex, objcomauto, comobj; type {$METHODINFO ON} TMySriptableClass = class(TObjectDispatch) public [...] function FnWithVarNumOfArgs(const args: OleVariant): string; [...] function TMySriptableClass.FnWithVarNumOfArgs(const args: OleVariant): string; var dispParams: activex.DISPPARAMS; vtRet, Element: OleVariant; Enum: IEnumVARIANT; Fetched: LongWord; begin if TVarData(args).VType = varDispatch then begin OleCheck(IDispatch(args).Invoke(DISPID_NEWENUM, GUID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, dispParams, @vtRet, nil, nil)); Enum := IUnknown(vtRet) as IEnumVARIANT; while (Enum.Next(1, Element, Fetched) = S_OK) do ShowMessage(Element); end; Result := 'OK'; end; [...] ASW.Execute('var myArray=["myarg1", 5, true];' + 'MyObj.FnWithVarNumOfArgs(myArray);'); [...] 
+2
source

Now, as you configured it, you need to pass an array of strings, since you have declared the parameter as such. In Delphi itself, which can be done using

 MyObj.FnWithVarNumOfArgs(Array("1","2","3")) 

To create a dynamic arry with the given values, and then pass it to FnWithVarNumOfArgs.

From a scripting language, the Delphi Array function will certainly not be available and you will need to do something smart with pointers, and I don’t know if it can even be made to work.

Perhaps best to use what is known as the Variant Open Array Parameters.

Using: http://docwiki.embarcadero.com/RADStudio/en/Parameters_(Delphi)#Variant_Open_Array_Parameters

The parameters of an open array allow you to pass an array of differently typed expressions for a single procedure or function. To define a routine with a variant of an open array parameter, specify an array of constants as the type of parameter. In this way,

DoSomething procedure (A: array Const);

announces a procedure called DoSomething that can run heterogeneous arrays.

The const construction array is equivalent to the TVarRec array. TVarRec, declared in the system unit, is a record with a variant part, which may contain integer values, Boolean, character, real, string, pointer, class, class reference, interface and variants. The TVarRec VType field indicates the type of each element in the array. Some types are passed as pointers and not values; in particular, strings are passed as a pointer and must be a typecast for the string.

+1
source

Source: https://habr.com/ru/post/1342509/


All Articles