【问题标题】:How to run external exe in Node Webkit?如何在 Node Webkit 中运行外部 exe?
【发布时间】:2016-03-14 10:47:20
【问题描述】:

我正在将 Node Webkit 用于我的 Web 应用程序,而且我真的是使用 Node Webkit 的新手。我想在我的应用程序中运行我的 exe,但我什至无法使用“child_process”打开简单的记事本。我在网站上看到了一些示例,但我仍然发现很难运行 notepad.exe,请帮助并提前非常感谢。

var execFile = require 
('child_process').execFile, child;

child = execFile('C:\Windows\notepad.exe',
function(error,stdout,stderr) { 
if (error) {
            console.log(error.stack); 
            console.log('Error code: '+ error.code); 
            console.log('Signal received: '+ 
            error.signal);
           } 
console.log('Child Process stdout: '+ stdout);
console.log('Child Process stderr: '+ stderr);
 }); 
child.on('exit', function (code) { 
console.log('Child process exited '+
'with exit code '+ code);
});

我还尝试使用 meadco-neptune 插件运行 exe 并添加插件,我将代码放在 package.json 文件中,但它显示无法加载插件。我的 package.json 文件是这样的

 {
   "name": "sample",
   "version": "1.0.0",
   "description": "",
   "main": "index.html",
   "window": {
   "toolbar": false,
   "frame": false,
   "resizable": false,
   "show": true,

   "title": " example"
             },
   "webkit": {
   "plugin": true
             },
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
             },
    "author": "xyz",
    "license": "ISC"
 }

【问题讨论】:

  • Docs 表示“callback - 进程终止时使用输出调用的函数”。 记事本不会自行终止

标签: node.js webkit exe node-webkit


【解决方案1】:

在 node.js 中有两种方法可以使用标准模块 child_process 启动外部程序:execspawn

使用exec 时,您会在外部程序退出时获得标准输出和标准错误信息。数据才返回到 node.js,正如米克布在 cmets 中正确指出的那样。

但如果你想以交互方式从外部程序接收数据(我怀疑你不会真的启动 notepad.exe),你应该使用另一种方法 - spawn

考虑这个例子:

var spawn = require('child_process').spawn,
    child    = spawn('C:\\windows\\notepad.exe', ["C:/Windows/System32/Drivers/etc/hosts"]);

child.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

child.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

child.on('close', function (code) {
  console.log('child process exited with code ' + code);
});

您还需要在路径名中使用双反斜杠:C:\\Windows\\notepad.exe,否则您的路径将被评估为
C:windows notepad.exe (带回车)当然不存在。

或者您可以只使用正斜杠,如示例中的命令行参数。

【讨论】:

  • 我无法使用 spawn 和 execFile 打开此文件 C:\\Windows\\System32\\osk.exe。试试这个child.spawn('c:\\Windows\\System32\\osk.exe')child.spawn('c:\\Windows\\System32\\osk.exe', []);
  • 可能是一个安全问题。
猜你喜欢
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多