简体   繁体   English

节点-子进程生成路径

[英]Node - child process spawn path

I would like to run an exe in Windows from my Node Webkit app. 我想从Node Webkit应用程序在Windows中运行一个exe。

I am trying the below code but it is not working. 我正在尝试以下代码,但无法正常工作。

document.getElementById('play').onclick = function()
{
    var spawn = require('child_process').spawn;
    var child = spawn(__dirname + '/core.exe', ['/arg1']);

    var snd = new Audio("min.wav"); 
    snd.play();
    win.minimize();

    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);
        var snd = new Audio("restore.wav"); 
        snd.play();
        win.restore();
    });
}

Am I getting the path wrong? 我走错了路吗? I need it to be current directory and run the exe with that name and the example arg. 我需要它是当前目录,并使用该名称和示例arg运行exe。

The output SHOULD be a messagebox, but nothing loads. 输出应该是一个消息框,但不会加载任何内容。

Managed to figure it out, it wasn't defined because I was using it in browser context. 设法弄清楚了,它没有定义,因为我是在浏览器上下文中使用它的。 I didn't get the nw.js SDK version for some reason, found that __DIRNAME was undefined. 由于某种原因,我没有获得nw.js SDK版本,发现__DIRNAME未定义。 Came up with this solution instead. 提出了这个解决方案。

    var path = require('path');
    var nwDir = path.dirname(process.execPath);
    var spawn = require('child_process').spawn;
    var child = spawn(nwDir + '/app/core.exe', ['/arg1']);

Now working as intended. 现在按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM