program ScriptingTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Win.ComObj,
System.Types,
System.Variants,
Winapi.ActiveX,
h5u.ActiveScripting.Interfaces;
procedure Main;
var
ClassID: TGUID;
Engine: IActiveScript;
ASite: IActiveScriptSite;
Parser: IActiveScriptParse;
ErrInfo: EXCEPINFO;
Flags: DWORD;
OResult: OleVariant;
Name: WideString;
Disp: IDispatch;
HR: HResult;
begin
// https://stackoverflow.com/questions/7491868/how-to-load-call-a-vbscript-function-from-within-c
OleCheck(CLSIDFromProgID(PWideChar('
VBScript'), ClassID));
Engine := CreateComObject(ClassID)
as IActiveScript;
ASite := TDummyNonInteractiveScriptSite.Create;
// ODER: ASite := TDummyActiveScriptSite.Create(Self.Handle)
OleCheck(Engine.SetScriptSite(ASite));
OleCheck(Engine.QueryInterface(IActiveScriptParse, Parser));
OleCheck(Parser.InitNew);
OleCheck(Parser.ParseScriptText(PWideChar('
WScript.Echo "abc"'),
nil,
nil,
nil, 0, 0, SCRIPTTEXT_ISVISIBLE,
nil, ErrInfo));
//
HR := Parser.AddScriptlet(
nil, PWideChar('
a = 123'),
nil,
nil,
nil,
nil, 0, 0, SCRIPTTEXT_ISVISIBLE,
Name, ErrInfo);
OleCheck(HR);
// <---- EOleSysError: Ungültiger Zeiger
OleCheck(Parser.ParseScriptText(PWideChar('
a * 2 + 3'),
nil,
nil,
nil, 0, 0, SCRIPTTEXT_ISEXPRESSION, @OResult, ErrInfo));
Writeln(VarToStrDef(OResult, '
(NULL)'));
//
OleCheck(Parser.ParseScriptText(PWideChar('
MsgBox "Hello World! The current time is " & Now'),
nil,
nil,
nil, 0, 0, SCRIPTTEXT_ISVISIBLE,
nil, ErrInfo));
// Engine.GetScriptDispatch(nil, Disp);
end;
begin
try
CoInitializeEx(
nil, 0);
try
Main;
finally
CoUninitialize;
end;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
Readln;
end.