Jint doesn't know what ActiveXObject is, so you have to tell Jint. I don't know if you can pass something into a JintEngine which can be treated as a JavaScript constructor, but you can register a custom function which creates an instance for a given COM ProgId:
private delegate object CreateActiveXObjectDelegate(string progId); private static object CreateActiveXObject(string progId) { var type = Type.GetTypeFromProgID(progId); if(type!=null) { return Activator.CreateInstance(type); } else { return null; // alternatively throw an exception or whatever is suitable in your situation } }
Then you can register this method as a function in your JintEngine which then can be called from your script:
string script = @" function square() { MyObject = createActiveXObject('WScript.Shell' ); MyObject.Run('whatever'); return 2 * 2; }; return square(); "; var jintEngine = new JintEngine(); jintEngine.DisableSecurity(); // required, cannot tell why exactly jintEngine.SetFunction("createActiveXObject", (CreateActiveXObjectDelegate)CreateActiveXObject); var result = jintEngine.Run(script);
Jint uses reflection when it tries to call MyObject.Run, which fails in my example with an error saying Method isn't defined: Run. I suspect that calling Run doesn't work because COM-Interop and reflection can get tricky (I think MethodInfos cannot be retrieved from the underlying COM type).
A different approach might be to register a function which simply calls WScript.Shell.Run (or maybe even better: System.Diagnostics.Process.Start):
private delegate void Runner(string progId); private static void ShellRun(string command) { var type = Type.GetTypeFromProgID("WScript.Shell"); if(type!=null) { var shell = Activator.CreateInstance(type); type.InvokeMember("Run", BindingFlags.InvokeMethod, null, shell, new object[] {command}); } }
Then call ShellRun from you script (registered as run):
string script = @" function square() { run('whatever'); return 2 * 2; }; return square(); "; var jintEngine = new JintEngine(); jintEngine.DisableSecurity(); // required, cannot tell why exactly jintEngine.SetFunction("run", (Runner)ShellRun); var result = jintEngine.Run(script);
Hope that helps.