Suresh Chaudhary
Suresh Chaudhary

Reputation: 1639

execute exe file using Jint - Javascript Interpreter

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?

Upvotes: 0

Views: 1232

Answers (1)

Chaquotay inactive
Chaquotay inactive

Reputation: 2232

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.

Upvotes: 1

Related Questions