document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // simple .NET Script Host
  2. // De Plancke Ronny
  3.  
  4. // credits
  5. // C# Interfaces for the Windows Scripting Host
  6. // by Uwe Keim , http://www.codeproject.com/KB/cs/ZetaScriptingHost.aspx
  7. // based on C++ Script Host
  8. // by Ladislav Nevery , http://www.codeproject.com/KB/cpp/ScriptYourApps.aspx
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14.  
  15. using ActiveScript;
  16. using System.Runtime.InteropServices;
  17. using ComTypes = System.Runtime.InteropServices.ComTypes;
  18.  
  19. namespace ScriptHost
  20. {
  21.     /// <summary>
  22.     /// instance of this class will be created to represent the named item declared with the SCRIPTITEMFLAGS.SCRIPTITEM_NOCODE flag.
  23.     /// this instance will be visible to the script.
  24.     /// </summary>
  25.     [ComVisible(true)]
  26.     [ClassInterface(ClassInterfaceType.AutoDispatch /*.AutoDual */)]  // automaticly implements com interface IDispatch ( and IUnknown )
  27.     public class ScriptInterface
  28.     {
  29.         /// <summary>
  30.         /// allows scripts to write to the console
  31.         /// </summary>
  32.         public void WriteLine(string s) { Console.WriteLine(s); }
  33.  
  34.     }
  35.  
  36.     /// <summary>
  37.     /// declare JavaScript as a COM coclass  
  38.     /// </summary>
  39.     [ComImport, Guid("f414c260-6ac0-11cf-b6d1-00aa00bbbb58")]
  40.     class JavaScript
  41.     {
  42.     }
  43.  
  44.     /// <summary>
  45.     /// declare VbScript as a COM coclass  
  46.     /// </summary>
  47.     [ComImport, Guid("b54f3741-5b07-11cf-a4b0-00aa004a55e8")]
  48.     class VbScript
  49.     {
  50.     }
  51.  
  52.     public static class ScriptHost
  53.     {
  54.         public static void run()
  55.         {
  56.  
  57.             IActiveScript script;
  58.             IActiveScriptParse parse;
  59.  
  60.             script = (IActiveScript)new JavaScript();
  61.             parse = (IActiveScriptParse)script;
  62.  
  63.             ComTypes.EXCEPINFO excepinfo;
  64.  
  65.             ScriptSite host = new ScriptSite();
  66.  
  67.             script.SetScriptSite(host);
  68.             script.AddNamedItem(@"app", (uint)(SCRIPTITEMFLAGS.SCRIPTITEM_ISVISIBLE | SCRIPTITEMFLAGS.SCRIPTITEM_NOCODE));
  69.  
  70.             // Hello world java script
  71.             string source = @"app.WriteLine(\'hello world.\'); ";
  72.  
  73.             try
  74.             {
  75.                 parse.InitNew();
  76.                 parse.ParseScriptText(source, "", IntPtr.Zero, "", 0, 0, 0, IntPtr.Zero, out excepinfo);
  77.                 script.SetScriptState(SCRIPTSTATE.SCRIPTSTATE_CONNECTED);
  78.                 script.Close();
  79.             }
  80.             catch (COMException e)
  81.             {
  82.                 Console.WriteLine("Exception thrown by the ScriptEngine : {0}", e.Message);
  83.             }
  84.         }
  85.     }
  86.  
  87.     /// <summary>
  88.     /// Site for the Windows Script engine.
  89.     /// </summary>
  90.     /// <see cref="http://msdn.microsoft.com/library/en-us/script56/html/4d604a11-5365-46cf-ab71-39b3dbbe9f22.asp"/>
  91.     class ScriptSite : IActiveScriptSite
  92.     {
  93.  
  94.         ScriptInterface s_interface = new ScriptInterface();
  95.  
  96.         #region IActiveScriptSite Members
  97.  
  98.         /// <summary>
  99.         /// Retrieves the locale identifier associated with the host\'s user interface.
  100.         /// </summary>
  101.         public void GetLCID(out uint id)
  102.         {
  103.             id = 2048; // use default locale
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Allows the scripting engine to obtain information about an item added with the IActiveScript::AddNamedItem method.
  108.         /// </summary>
  109.         public void GetItemInfo(string name, uint returnMask, out object item, IntPtr ppti)
  110.         {
  111.             item = null;
  112.             if ((returnMask & (uint)SCRIPTINFOFLAGS.SCRIPTINFO_IUNKNOWN) != 0)
  113.                 item = s_interface;
  114.             else
  115.                 item = null;
  116.  
  117.             // we dont\'t provide type information
  118.             // the item object cannot source events and name binding must be realized with the IDispatch::GetIDsOfNames method
  119.             ppti = IntPtr.Zero; // no events on our item class ,
  120.  
  121.         }
  122.  
  123.         /// <summary>
  124.         /// Informs the host that an execution error occurred while the engine was running the script.
  125.         /// </summary>
  126.         public void OnScriptError(object err)
  127.         {
  128.             ComTypes.EXCEPINFO e;
  129.             ((IActiveScriptError)err).GetExceptionInfo(out e);
  130.             Console.WriteLine("Exception = {0} , source = {1}.", e.bstrDescription, e.bstrSource);
  131.         }
  132.  
  133.  
  134.         /// <summary>
  135.         /// Retrieves a host-defined string that uniquely identifies the current document version
  136.         /// </summary>
  137.         public void GetDocVersionString(out string v)
  138.         { v = string.Empty; }
  139.  
  140.  
  141.         /// <summary>
  142.         /// Informs the host that the script has completed execution.
  143.         /// </summary>
  144.         /// <param name="result">script results</param>
  145.         /// <param name="info">EXCEPINFO structure that contains exception information generated when the script terminated, or NULL if no exception was generated. </param>
  146.         public void OnScriptTerminate(ref object result, ref ComTypes.EXCEPINFO info)
  147.         { }
  148.  
  149.         /// <summary>
  150.         /// Informs the host that the scripting engine has changed states.
  151.         /// </summary>
  152.         public void OnStateChange(SCRIPTSTATE state)
  153.         { }
  154.  
  155.         /// <summary>
  156.         /// Informs the host that the scripting engine has begun executing the script code.
  157.         /// </summary>
  158.         public void OnEnterScript()
  159.         { }
  160.  
  161.         /// <summary>
  162.         /// Informs the host that the scripting engine has returned from executing script code.
  163.         /// </summary>
  164.         public void OnLeaveScript()
  165.         { }
  166.  
  167.         #endregion
  168.     }
  169. }
');