home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / CAJSCRPT.ZIP / ifps3 / help / sample3.dpr < prev    next >
Text File  |  2002-06-20  |  3KB  |  94 lines

  1. program sample3;
  2.  
  3. uses
  4.   ifpscomp,
  5.   ifps3,
  6.   ifps3lib_std,
  7.   ifps3lib_stdr,
  8.   ifpidll2,
  9.   ifpidll2runtime
  10.  
  11.  
  12.   ;
  13.  
  14. function ScriptOnUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  15. { the OnUses callback function is called for each "uses" in the script. 
  16.   It's always called with the parameter 'SYSTEM' at the top of the script. 
  17.   For example: uses ii1, ii2;   
  18.   This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
  19. }
  20. begin
  21.   if Name = 'SYSTEM' then
  22.   begin
  23.     RegisterStandardLibrary_C(Sender);
  24.     { Register the standard library. The standard library is in the 
  25.       ifps3lib_std.pas unit. This will register all standard functions like 
  26.       Copy, Pos, Length. }
  27.  
  28.     Sender.OnExternalProc := @DllExternalProc;
  29.     { Assign the dll library to the script engine. This function can be found in the ifpidll2.pas file.
  30.       When you have assigned this, it's possible to do this in the script:
  31.     
  32.         Function FindWindow(c1, c2: PChar): Cardinal; external 'FindWindow@user32.dll stdcall';
  33.         
  34.         The syntax for the external string is 'functionname@dllname callingconvention'.
  35.     }
  36.  
  37.     Result := True;
  38.   end else
  39.     Result := False;
  40. end;
  41.  
  42. procedure ExecuteScript(const Script: string);
  43. var
  44.   Compiler: TIFPSPascalCompiler;
  45.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  46.     translate a Pascal script into a compiled for the executer understands. } 
  47.   Exec: TIFPSExec;
  48.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  49.     the compiler to run a script. }
  50.   Data: string;
  51. begin
  52.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  53.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  54.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  55.   begin
  56.     Compiler.Free;
  57.      // You could raise an exception here.
  58.     Exit;
  59.   end;
  60.  
  61.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  62.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  63.  
  64.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  65.   RegisterStandardLibrary_R(Exec);
  66.   { The functions registered at compile time also need to be registered at runtime. These
  67.     functions can be found in the ifps3lib_stdr.pas unit. }
  68.  
  69.   RegisterDLLRuntime(Exec);
  70.   { Register the DLL runtime library. This can be found in the ifpidll2runtime.pas file.}
  71.  
  72.   if not  Exec.LoadData(Data) then // Load the data from the Data string.
  73.   begin
  74.     { For some reason the script could not be loaded. This is usually the case when a 
  75.       library that has been used at compile time isn't registered at runtime. }
  76.     Exec.Free;
  77.      // You could raise an exception here.
  78.     Exit;
  79.   end;
  80.  
  81.   Exec.RunScript; // Run the script.
  82.   Exec.Free; // Free the executer.
  83. end;
  84.  
  85.  
  86. const
  87.   Script =
  88.     'function MessageBox(hWnd: Longint; lpText, lpCaption: PChar; uType: Longint): Longint; external ''MessageBoxA@user32.dll stdcall'';'#13#10 +
  89.     'var s: string; begin s := ''Test''; MessageBox(0, s, ''Caption Here!'', 0);end.';
  90.  
  91. begin
  92.   ExecuteScript(Script);
  93. end.
  94.