Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How call the CreateOleObject function using dwscript?

I'm trying to execute this code (this is a minimal sample in order to use CreateOleObject) from inside of a 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'm getting this error message

Syntax Error: Unknown name "CreateOleObject" [line: 5, column: 8]

the question is how i can execute the CreateOleObject function using dwscript?

UPDATE

Following the Linas suggestions I could finally resolve the issue.

This is a sample 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.
like image 504
Salvador Avatar asked Nov 05 '22 03:11

Salvador


1 Answers

This can be accomplished in two ways.

1 way: You must drop TdwsComConnector (from unit dwsComConnector) to your data module or form (or create it manually) and assign your script instance to it. e.g.:

dwsComConnector1.Script := LScript;

2 way:

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);

This will expose CreateOleObject function to DWScript so you could use it.

Also you should declare your V as OleVariant instead of Variant and change the line to Result := VarToStr(V.GetFileVersion(FileName)); to make it work properly.

like image 88
Linas Avatar answered Nov 15 '22 05:11

Linas