简体   繁体   中英

Run .EXE in Node.JS

I am having trouble running a external executable in Node.Js. My code looks like this:

function executeFile(m, cb) {
    var urlTarget = "D:/thesis_node/upload/1.jpeg";
    var urlScene = "D:/thesis_node/upload/scene.jpeg";
    exec(__execDirName+'/FeatureDetection.exe', [urlTarget, urlScene], function(error, stdout, stderr) {
        if(error) return cb(error);
        cb(null, stdout);
    });
}

When I run the script, it did nothing and it seems It's doing a process but it never ends. If I run my EXE file using Command Prompt, it works. The .exe file returns value. I need to get that value.

Update

Actually I started to think something might be wrong with my C++ code in returning the value.

int main(int argc, char* argv[]) {
   int a = 5 + 10;
   return a; //Will this a can be received by Node.Js?
}

Is this the correct way to do that?

I don't believe you provide the arguments you want to be provided to the child process as the second argument to child_process.exec() . Instead you concatenate the arguments directly into the first argument of exec() .

See the documentation here: child_process.exec(command[, options][, callback])

Specifically:

command <String> The command to run, with space-separated arguments

So for your case, you would want something like:

var cmdToExec = (__execDirName + '/FeatureDetection.exe' + ' ' + urlTarget + ' ' + urlScene);

exec(cmdToExec, function(){...});

Alternatively, you could try child_process.execFile(file[, args][, options][, callback])

execFile() as apposed to exec() , which you are using now, does take an arguments array as a second parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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