0

i have a created one console application and in that i had use Jint - JavaScript Interpreter to run javascript function . but when i use activexobject in javascript it give me a error the code is:

string script= @" function square() { MyObject = new ActiveXObject( 'WScript.Shell' ); MyObject.Run('file:///D:/test/Tools/ofc.exe D:/test/Tools/ofc.ini') ; return 2 * 2; }; return square(); "; var result = new JintEngine() .Run(script); 

Can anyone tell me how can i do this?

1
  • i got the error : ActiveXObject is not defined Line: 3 Char: 20 No source code available Commented Mar 2, 2011 at 8:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.