2012-03-14 11 views
5

próbuję wykonanie tego kodu (jest to minimalna próbka w celu wykorzystania CreateOleObject) od wewnątrz dwscriptJak wywołać funkcję CreateOleObject za pomocą dwscript?

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

Do tej pory próbowałem to

{$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. 

ale jestem ten komunikat o błędzie

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

pytanie brzmi, jak mogę wykonać funkcję CreateOleObject użyciu dwscript?

UPDATE

Po sugestii Linas mogłem wreszcie rozwiązać problem.

To jest przykład aplikacja działa

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. 
+0

Używa dwsComConnector? –

+0

Właśnie dodałem tę jednostkę, z tymi samymi wynikami :(. – Salvador

+0

Zgaduję, ale umieść tę dyrektywę uses w twoim pliku: 'używa Windows, Classes, SysUtils, dwsComp, dwsCompiler, dwsExprs, dwsComConnector, Warianty, ActiveX , ComObj, dwsXPlatform, dwsUtils; ' –

Odpowiedz

2

Można to zrobić na dwa sposoby.

1 sposób: Musisz upuścić TdwsComConnector (z jednostki dwsComConnector) do modułu danych lub formy (lub utworzyć je ręcznie) i przypisać instancji skryptu do niego. np .:

dwsComConnector1.Script := LScript; 

2 sposób:

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

To narazi funkcję CreateOleObject do DWScript więc można go używać.

Ponadto należy zadeklarować V jak OleVariant zamiast Variant i zmienić linię do Result := VarToStr(V.GetFileVersion(FileName)); aby to działało poprawnie.

+0

Wielkie dzięki, że jestem. – Salvador