Spawning a .lnk windows shortcut with node.js gives "UNKNOWN" error

1.5k views Asked by At

I'm having a game spawned as a child process. It all works nice and well with the game's original .exe file. However, I'd like it to execute minimized, and the only way to do it as far as I can tell is to create a windows shortcut (.lnk file, in the same directory) and set the launch options to minimized. Node.js doesn't like that:

    internal/child_process.js:313
    throw errnoException(err, 'spawn');
    ^

Error: spawn UNKNOWN
    at exports._errnoException (util.js:1026:11)
    at ChildProcess.spawn (internal/child_process.js:313:11)
    at exports.spawn (child_process.js:380:9)
    at ExecDom4Server (D:\Google Drive\Clockwork Hounds stuff\MrClockworkBot\v10
\MrClockworkBot.js:568:42)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5)

The game itself has a few specific arguments on graphic settings but none to launch it minimized either. Here's the particular code:

function execDom4Server(name, args)
{
    var spawn = require('child_process').spawn;
    childrenProcesses[name.toLowerCase()] = spawn(dom4root + "dom4.exe.lnk", args);
    updateDom4Games();
}

I've looked everywhere for this, but I suppose it's fairly specific. I don't know if it's just node.js being unable to handle .lnk files. Any pointers that you can give me would be greatly appreciated!

2

There are 2 answers

2
David Jones On BEST ANSWER

This should work if you use the shell option of spawn()

spawn('application.lnk', args, { shell: true })
0
NNSkelly On

Another possible issue: I haven't tested whether it's unique to .lnk and .bat files, but even with {shell:true}, child process spawning seems to immediately exit with code 1 if you try to launch the full path to the file e.g.

const child_process = require('child_process');
let child = child_process.spawn('path/to/shortcut.bat');

Instead, looks like you need to run only the final filename of the shortcut, but set the cwd option to the path at which it resides:

const child_process = require('child_process');
let child = child_process.spawn('shortcut.bat', [], {cwd:'path/to'});

If you're using an actual .lnk (tl;dr: my experiments initially led me to fs write the .lnk file into a corresponding .bat file, which is an entirely viable option for further customizing process launching), then you still need {shell:true} to avoid, at least in my case at present, a errno:-4094 code:'UNKNOWN' syscall: 'spawn' error.

const child_process = require('child_process');
let child = child_process.spawn('shortcut.lnk', [], {cwd:'path/to', shell:true});