Question:
What free or commercial scripting host for C# can run VBScript in a C# app targeting a 64 bit architecture?
Background:
It is Q3 2017 - this subject has been raised many times before but with few conclusions and many of those older code samples no longer work.
I am migrating a legacy vb6 COM dll to c# service (Dot Net framework 4.6.1+). A feature of the vb6 dll was extensibility via the MS Scripting control (msscript.ocx). This allowed the users to write small VB sripts to modify the behaviour of the application within strict constraints. It has worked well so lets not get into justifying it.
The new c# service must be a 64 bit application. msscript.ocx is 32 bit AFAIK. Ideally we will replace use of msscript.ocx with as little fuss as possible.
Notes:
- Powershell is probably not a good replacement candidate as it would give users access to a Pandoras box of capabilities.
- I have seen reference to codefluent client as a potential direct replacement but cannot find sample code. Is this product supported for vbscript?
- Executing the vbscript as a file via some shell approach is not considered viable because the script must be able to callback to the C# host to ask for variables.
- ActiveXScriptLib is a potential. It has a NuGet package that installs via VisualStudio, and the examples given here work for me in x64, including the more complicated example of having the script access nominated methods of the hosting c#.
Research notes:
This has been a well-trodden path since MS introduced the first 64 bit OS, however much of the sample code no longer runs in current C#.
- Will msscript.ocx stop working on Windows 8?
- Run VBScript using C#
- CodeFluent vs Interop.MSScriptControl.dll
Sample code
As a temp fix, can anyone provide sample code to be used in a C# 64 bit app that can utilise msscript.ocx? This sample 'would' work if the msscript.ocx was accessible - I get the error:
System.Runtime.InteropServices.COMException: 'Retrieving the COM class factory for component with CLSID {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).'
public void TestScript()
{
Type scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
dynamic obj = Activator.CreateInstance(scriptType, false);
obj.Language = "vbscript";
string vbscript = "2 * 2";
var res = obj.Eval(vbscript);
System.Diagnostics.Trace.WriteLine(res);
}
Edit: See item 4 added to list in notes - showing some potential.