Edwin Kurniawan
Edwin Kurniawan

Reputation: 31

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?

Upvotes: 2

Views: 6140

Answers (1)

zero298
zero298

Reputation: 26878

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.

Upvotes: 2

Related Questions