I have been working on updating a .NET script engine originally
written by Rama K. Vavilala called "NScript" originally posted on
CodeProject. I have recently re-compiled this program for the
Microsoft .NET Framework 2.0. With permission from the original
author I have re-packaged the program and have called it Windows
Script Host .NET (WSH.NET). It is available as an open-source project
at Google Code from the following link:
http://code.google.com/p/windowsscripthostdotnet
This version of the script engine support C#, JScript.NET, VB.NET, and
J#. It requires the .NET Framework 2.0 as well as the J# 2.0
extension. I have a few small examples but I would like to build up
some more.
I would like to hear your feedback and suggestions to improve this
project.
TIA...
By the way, you may want to headline it somewhat differently than as an
"alternative to PowerShell", though - this isn't a shell.
"Blue Streak" <rdleb...@hotmail.com> wrote in message
news:6648dd76-4473-4826...@w7g2000hsa.googlegroups.com...
> This version of the script engine support C#, JScript.NET, VB.NET, and
> J#. It requires the .NET Framework 2.0 as well as the J# 2.0
Is it interoperable with the ActiveX/WSH stuff? For example, I tend to
prefer applications, that come with an ActiveX host (see klient.com,
zeusedit.com, SpeedCommander12, etc.) Now I would be glad to access .NET
from ActiveX hosts like these. Is that possible? Why not dispose your
scripting-host as a COM object to system? Or is it?
--
Bye,
Andreas M.
Andreas,
Yeah, I've been working on ActiveX / COM access.
The focus of this script engine is to run .NET code as a
"script". I have put quotes around the word script for a reason
because the code is not interpreted but compiled (in memory) then
executed. I'm not sure it would make much sense to access this as a
COM object because any advanced features (e.g. handling text files,
ADO, advanced / user-defined data structures) are really coming from
the .NET framework itself. The WSH script engine, on the other hand,
can be handy when you are manipulating text files from VB6, for
example.
However, you can access ActiveX / COM objects from .NET. If you
were using VS.NET you would simply select: Project >> Add Reference >>
(and check off the ActiveX object you want) then just reference that
in your code. It is not so simple from my (can I say "my"?) script
engine. I managed to dig an example of late binding from Microsoft
and make it work:
----Late Binding.ncs----
using System;
using System.Reflection;
using System.Windows.Forms;
namespace Test
{
class LateBinding
{
//This example uses late binding to call an ActiveX object
public static void Main(string[] args)
{
object objApp_Late;
object objBook_Late;
object objBooks_Late;
object objSheets_Late;
object objSheet_Late;
object objRange_Late;
object[] Parameters;
try
{
// Get the class type and instantiate Excel.
Type objClassType =
Type.GetTypeFromProgID("Excel.Application");
objApp_Late = Activator.CreateInstance(objClassType);
//Get the workbooks collection.
objBooks_Late =
objApp_Late.GetType().InvokeMember("Workbooks",
BindingFlags.GetProperty, null, objApp_Late, null);
//Add a new workbook.
objBook_Late =
objBooks_Late.GetType().InvokeMember("Add", BindingFlags.InvokeMethod,
null, objBooks_Late, null);
//Get the worksheets collection.
objSheets_Late =
objBook_Late.GetType().InvokeMember("Worksheets",
BindingFlags.GetProperty, null, objBook_Late, null);
//Get the first worksheet.
Parameters = new Object[1];
Parameters[0] = 1;
objSheet_Late =
objSheets_Late.GetType().InvokeMember("Item",
BindingFlags.GetProperty, null, objSheets_Late, Parameters);
//Get a range object that contains cell A1.
Parameters = new Object[2];
Parameters[0] = "A1";
Parameters[1] = Missing.Value;
objRange_Late =
objSheet_Late.GetType().InvokeMember("Range",
BindingFlags.GetProperty, null, objSheet_Late, Parameters);
//Write "Hello, World!" in cell A1.
Parameters = new Object[1];
Parameters[0] = "Hello, World!";
objRange_Late.GetType().InvokeMember("Value",
BindingFlags.SetProperty, null, objRange_Late, Parameters);
//Return control of Excel to the user.
Parameters = new Object[1];
Parameters[0] = true;
objApp_Late.GetType().InvokeMember("Visible",
BindingFlags.SetProperty, null, objApp_Late, Parameters);
objApp_Late.GetType().InvokeMember("UserControl",
BindingFlags.SetProperty, null, objApp_Late, Parameters);
}
catch (Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage,
ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
MessageBox.Show(errorMessage, "Error");
}
}
}
}
------
This opens up a copy of Excel and writes "Hello World!" into cell A1.
I'm still trying to get the example of early binding to work.