How to call the CreateOleObject function using dwscript?

I am trying to execute this code (this is the minimum sample to use CreateOleObject) from inside dwscript

function GetFileVersion(const FileName: string): string;
var
  V : OleVariant;
begin
  V  := CreateOleObject('Scripting.FileSystemObject');
  Result := V.GetFileVersion(FileName);
end;

So far i tried this

{$APPTYPE CONSOLE}

{$R *.res}


uses
  SysUtils,
  ComObj,
  ActiveX,
  dwsComp,
  dwsCompiler,
  dwsExprs,
  dwsCoreExprs;


procedure Execute;
var
  LScript: TDelphiWebScript;
  LUnit: TdwsUnit;
  LProg: IdwsProgram;
  LExec: IdwsProgramExecution;
begin
  LScript := TDelphiWebScript.Create(NIL);
  LUnit := TdwsUnit.Create(NIL);
  try
    LUnit.UnitName := 'Foo';
    LUnit.Script := LScript;
    //  compile a simple script
    LProg := LScript.Compile(
      'function GetFileVersion(const FileName: string): string;'+sLineBreak+
      'var'+sLineBreak+
      '   V : Variant;'+sLineBreak+
      'begin'+sLineBreak+
      ' V  := CreateOleObject(''Scripting.FileSystemObject'');'+sLineBreak+
      ' Result := V.GetFileVersion(FileName);'+sLineBreak+
      'end;'+sLineBreak+
      ''+sLineBreak+
      'PrintLn(GetFileVersion(''Foo''));'+sLineBreak+
      ''
    );

    if LProg.Msgs.HasErrors then begin
      Writeln(LProg.Msgs.AsInfo);
      Exit;
    end;

    try
      LExec := LProg.Execute;
    except
      on E: Exception do
        WriteLn(E.Message + sLineBreak + LExec.Msgs.AsInfo );
    end;
    Writeln(LExec.Result.ToString);
  finally
    LScript.Free;
  end;
end;

begin
  try
    Execute;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

But I get this error message

Syntax error: Unknown name "CreateOleObject" [row: 5, column: 8]

The question is, how can I execute CreateOleObjectusing dwscript?

UPDATE

Following the recommendations of Linas , I could finally solve the problem.

This is an example of a working application.

uses
  SysUtils,
  ComObj,
  ActiveX,
  dwsComp,
  dwsCompiler,
  dwsExprs,
  dwsComConnector,
  dwsCoreExprs;


procedure Execute;
var
  LScript: TDelphiWebScript;
  LUnit: TdwsUnit;
  LProg: IdwsProgram;
  LExec: IdwsProgramExecution;
  LdwsComConnector : TdwsComConnector;
begin
  LScript := TDelphiWebScript.Create(NIL);
  LdwsComConnector:=TdwsComConnector.Create(nil);
  LdwsComConnector.Script:=LScript;
  LUnit := TdwsUnit.Create(NIL);
  try
    LUnit.UnitName := 'Foo';
    LUnit.Script := LScript;
    //  compile a simple script
    LProg := LScript.Compile(
      'function GetFileVersion(const FileName: string): string;'+sLineBreak+
      'var'+sLineBreak+
      '   V : OleVariant;'+sLineBreak+
      'begin'+sLineBreak+
      ' V  := CreateOleObject(''Scripting.FileSystemObject'');'+sLineBreak+
      ' Result := VarToStr(V.GetFileVersion(FileName));'+sLineBreak+
      'end;'+sLineBreak+
      ''+sLineBreak+
      'PrintLn(GetFileVersion(''C:\Bar\Foo.exe''));'+sLineBreak+
      ''
    );

    if LProg.Msgs.HasErrors then begin
      Writeln(LProg.Msgs.AsInfo);
      Exit;
    end;

    try
      LExec := LProg.Execute;
    except
      on E: Exception do
        WriteLn(E.Message + sLineBreak + LExec.Msgs.AsInfo );
    end;
    Writeln(LExec.Result.ToString);
  finally
    LScript.Free;
    LdwsComConnector.Free;
  end;
end;

begin
 try
    CoInitialize(nil);
    try
      Execute;
      Readln;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
end.
+5
source share
1 answer

There are two ways to do this.

1 . TdwsComConnector ( dwsComConnector) ( ) script. :.

dwsComConnector1.Script := LScript;

2 :

interface

uses
  dwsFunctions, dwsSymbols, dwsExprs;

type
  TCreateOleObjectFunc = class(TInternalFunction)
    procedure Execute(info : TProgramInfo); override;
  end;

implementation

uses 
  OleAuto;

{ TCreateOleObjectFunc }

procedure TCreateOleObjectFunc.Execute(info : TProgramInfo);
begin
  Info.ResultAsVariant := CreateOleObject(Info.ValueAsString['ClassName']);
end;

initialization
  RegisterInternalFunction(TCreateOleObjectFunc, 'CreateOleObject', ['ClassName', cString], cVariant, True);

CreateOleObject DWScript, .

V OleVariant Variant Result := VarToStr(V.GetFileVersion(FileName));, .

+2

All Articles