使用Jint - Javascript解释器执行exe文件

时间:2022-09-29 17:07:13

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:

我创建了一个控制台应用程序,并使用Jint - JavaScript解释器运行JavaScript函数。但是当我在javascript中使用activexobject时它会给我一个错误代码是:

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 个解决方案

#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:

Jint不知道ActiveXObject是什么,所以你必须告诉Jint。我不知道您是否可以将某些东西传递给JintEngine,它可以作为一个JavaScript构造函数来处理,但是您可以注册一个自定义函数,它为给定的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:

然后你可以将这个方法注册为JintEngine中的一个函数,然后可以从你的脚本中调用:

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).

Jint在尝试调用MyObject时使用反射。运行,在我的示例中失败了,错误提示方法没有定义:运行。我怀疑调用Run不起作用,因为COM- interop和反射可能会比较棘手(我认为无法从底层COM类型中检索到方法fos)。

A different approach might be to register a function which simply calls WScript.Shell.Run (or maybe even better: System.Diagnostics.Process.Start):

另一种方法可能是注册一个只调用WScript.Shell的函数。运行(或者更好: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):

然后从您的脚本调用ShellRun(注册为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.

希望有帮助。

#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:

Jint不知道ActiveXObject是什么,所以你必须告诉Jint。我不知道您是否可以将某些东西传递给JintEngine,它可以作为一个JavaScript构造函数来处理,但是您可以注册一个自定义函数,它为给定的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:

然后你可以将这个方法注册为JintEngine中的一个函数,然后可以从你的脚本中调用:

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).

Jint在尝试调用MyObject时使用反射。运行,在我的示例中失败了,错误提示方法没有定义:运行。我怀疑调用Run不起作用,因为COM- interop和反射可能会比较棘手(我认为无法从底层COM类型中检索到方法fos)。

A different approach might be to register a function which simply calls WScript.Shell.Run (or maybe even better: System.Diagnostics.Process.Start):

另一种方法可能是注册一个只调用WScript.Shell的函数。运行(或者更好: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):

然后从您的脚本调用ShellRun(注册为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.

希望有帮助。