home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1999 March
/
Chip_1999-03_cd.bin
/
zkuste
/
delphi
/
INFO
/
DI9802TS.ZIP
/
ScriptDemo.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1997-08-27
|
7KB
|
244 lines
{ Tom Stickle - tstix@dancris.com }
{ Simple ActiveX Scripting demo }
unit ScriptDemo;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScriptSite, Activscp, ActiveX, ComObj, Buttons, Menus, ExtCtrls,
About;
type
TScriptingDemo = class(TForm)
tmEditor: TMemo;
dlgOpen: TOpenDialog;
dlgSave: TSaveDialog;
Panel1: TPanel;
btnFire: TButton;
teEventName: TEdit;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Save1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Language1: TMenuItem;
tmVBScript: TMenuItem;
tmJScript: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
procedure tmOpenClick(Sender: TObject);
procedure tmSaveClick(Sender: TObject);
procedure btnFireClick(Sender: TObject);
procedure tmVBScriptClick(Sender: TObject);
procedure About1Click(Sender: TObject);
private
{ Private declarations }
ActiveScript : IActiveScript;
ActiveScriptParse : IActiveScriptParse;
ActiveScriptSite : IActiveScriptSite;
function GetScriptFilter: String;
function InitEngine : Boolean; safecall;
procedure FireMethod(const EventName : WideString); safecall;
function ParseScript(const ScriptText : WideString): Boolean; safecall;
procedure StopEngine; safecall;
public
{ Public declarations }
end;
var
ScriptingDemo: TScriptingDemo;
implementation
{$R *.DFM}
{ Opens a script file }
procedure TScriptingDemo.tmOpenClick(Sender: TObject);
begin
tmEditor.Lines.Clear;
dlgOpen.Filter := GetScriptFilter;
if dlgOpen.Execute then
tmEditor.Lines.LoadFromFile(dlgOpen.FileName);
end;
{ Saves a script file }
procedure TScriptingDemo.tmSaveClick(Sender: TObject);
begin
dlgSave.Filter := GetScriptFilter;
if dlgSave.Execute then
tmEditor.Lines.SaveToFile(dlgSave.FileName);
end;
{ Returns File Filter }
function TScriptingDemo.GetScriptFilter: String;
begin
if tmVBScript.Checked then
Result := 'VBScript|*.bas|JScript|*.js'
else
Result := 'JScript|*.js|VBScript|*.bas';
end;
procedure TScriptingDemo.tmVBScriptClick(Sender: TObject);
begin
(Sender as TMenuItem).Checked := True;
end;
{ This Initializes Script Engine and declares internal objects }
function TScriptingDemo.InitEngine : Boolean;
var
Flags : Word;
CatID : TGuid;
begin
Result := False;
{ Retrieve activeScript interface via IUnknown }
{ By changing the CatID, we can easily change scripting engines from VBScript
to any other installed scripting engine, ie. JavaScript etc... }
if tmVBScript.Checked then
CatID := CatID_VBScript
else
CatID := CatID_JScript;
ActiveScript := IActiveScript(CreateComObject(CatID));
{ Get Interface pointer for IActiveScriptParse Interface }
if (ActiveScript.QueryInterface(IID_IActiveScriptParse,
ActiveScriptParse) <> S_OK) then
Exit;
{ COM Host Object Implementation }
ActiveScriptSite := IActiveScriptSite(TActiveScriptSite.Create);
{ Register Site Object with the ActiveScript Engine }
if ActiveScript.SetScriptSite(ActiveScriptSite) <> S_OK then
Exit;
{ initialize the engine }
if (ActiveScriptParse.InitNew <> S_OK) then
exit;
Flags := SCRIPTITEM_ISVISIBLE;
{ Add our custom COM Object to script engine }
if (ActiveScript.AddNamedItem( 'MyObj', Flags ) <> S_OK) then
exit;
{ Now fire up the scripting engine }
if ActiveScript.SetScriptState(SCRIPTSTATE_CONNECTED) <> S_OK then
Exit;
{ If we made it this far then we have succesfully initialized the engine! }
Result := true;
end;
{ This parses the Script }
function TScriptingDemo.ParseScript(const ScriptText : WideString) : Boolean;
var
Ei: TExcepInfo;
Flags: DWord;
VarOut: OleVariant;
begin
Flags := SCRIPTTEXT_NULL;
Result := (activeScriptParse.ParseScriptText(ScriptText, nil, nil, nil,
0, 0, Flags, VarOut, Ei) = S_OK);
end;
{ This calls the Main Subroutine/Function in the Script }
procedure TScriptingDemo.FireMethod(const EventName : WideString);
var
Disp: Integer;
DispParams: TDispParams;
Ei: TExcepInfo;
InvKind: Integer;
ReturnVal: POleVariant;
ScriptDispatch : IDispatch;
begin
if ActiveScript.GetScriptDispatch(nil, ScriptDispatch) <> S_OK then
Exit;
{ Initialize dispatch id }
Disp := -1;
{ Get a dispatch id number that corresponds the eventName name }
ScriptDispatch.getIDsOfNames(GUID_NULL, @eventName, 1, LOCALE_USER_DEFAULT, @disp);
{ See if anything was found for the EventName - Spelling Error will fail! }
if disp = -1 then begin
ShowMessage(Format('The method %s was not found in the script...', [EventName]));
exit;
end;
{ Set the type of invocation to method }
InvKind := DISPATCH_METHOD;
{ This structure can contain up to 32 arguments to pass in }
ReturnVal := nil;
DispParams.rgvarg := nil;
DispParams.rgdispidNamedArgs := nil;
DispParams.cArgs := 0;
DispParams.cNamedArgs := 0;
{ Fire the Event via ID Binding }
ScriptDispatch.Invoke(Disp, GUID_NULL, 0, InvKind, DispParams,
ReturnVal, @ei, nil);
end;
{ Resets Engine Clearing the Script and all Objects }
procedure TScriptingDemo.StopEngine;
begin
ActiveScript.SetScriptState(SCRIPTSTATE_UNINITIALIZED);
end;
{ Parse Script and Fire the "OnEvent" Sub/Function }
procedure TScriptingDemo.btnFireClick(Sender: TObject);
begin
try
InitEngine;
except
on Exception do
{ Present a real error message }
Raise Exception.Create('Unable to initialize scripting engine.');
end;
{ Once the engine is initialized, errors will be reported to the user
either by our Scripting Site object or in the FireMethod routine.
We can then just catch them here to prevent the application from halting. }
try
ParseScript(tmEditor.Text);
FireMethod(teEventName.Text);
except
on Exception do { Error Message displayed in IActiveScriptSite }
end;
StopEngine;
end;
procedure TScriptingDemo.About1Click(Sender: TObject);
var
AboutBox : TAboutBox;
begin
AboutBox := TAboutBox.Create(self);
try
AboutBox.ShowModal;
finally
AboutBox.Free;
end;
end;
end.