我以这种方式从 Javascript 调用批处理文件:
function runBatch(){
var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
exe.initWithPath("C:\\test.bat");
var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess);
run.init(exe);
var parameters = ["hi"];
run.run(false, parameters,parameters.length);
}
我的测试批处理文件是:
echo on
echo %1
pause
exit
但是,每次我调用批处理文件时,都不会显示命令提示符,就像我只是从桌面运行批处理文件一样。我该如何解决这个问题并显示批处理文件的命令提示符?
编辑 明确地说,cmd.exe 进程已启动 - 我可以在任务栏中看到它。但是没有窗口显示。此代码段的行为类似:
function runCmd(){
var exe = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
exe.initWithPath("C:\\WINDOWS\\system32\\cmd.exe");
var run = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess);
run.init(exe);
run.run(false, null,0);
}
最佳答案
到目前为止我听到的唯一解决方案(应该有效,虽然我还没有这样做,来自 Mozilla xulrunner IRC channel 中的 Mook:
创建一个临时批处理文件,在批处理文件中写入调用和传递参数。然后执行临时批处理文件。
例如伪代码:
f = fopen("temp.bat");
fprintf(f, "other.bat 1 2 3 4 5");
fclose(f);
exec("temp.bat");
不是很优雅,但应该可以。
关于javascript - 从 Javascript/XPCOM 调用的批处理文件不显示命令提示符窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/310996/