Batch standard error redirection (2>NUL) causes .exe to crash?

Viewed 87

When I run a program called KLRINT.EXE from the windows command prompt, not all standard output is redirected to terminal-output.txt as instructed:

KLRINT.EXE  0<terminal-input.txt 1>terminal-output.txt
    
  ...some output goes here...

The remaining 90% of the program's output is redirected successfully to terminal-output.txt. The program itself executes successfully and exits with error code 0:

echo %errorlevel%
0

The source code for KLRINT.EXE is long gone, so, where is ...some output goes here... coming from? My initial guess was that it was standard error. If I try to redirect standard error to 2>NUL or 2>terminal-error.txt, the program crashes with error code 255 at the point where ...some output goes here... is to be printed to standard output:

KLRINT.EXE  0<terminal-input.txt 1>terminal-output.txt 2>terminal-error.txt

echo %errorlevel%
255

Note nothing appears in terminal-error.txt.

Bottom line is that any attempt to access or redirect standard error causes the problem to crash. Is there another way to access or spoof standard error without causing the program to crash? If so, I believe I can solve for my larger / resulting problem described below.

The ultimate goal of this project is to call KLRINT.EXE from a Node.js app. When doing so, Node.js, rightfully so, always tries to access and return standard error. This, in turn, causes the program to crash with error code 255. FWIW, I'm using the native Node.js module / function child_process.exec().

exec('KLRINT.EXE  0<terminal-input.txt', (error, stdout, stderr) => {
  if (error){console.log(error);}
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

Here is the error object returned from exec():

Error: Command failed: KLRINT.EXE  0<terminal-input.txt

    at ChildProcess.exithandler (node:child_process:326:12)
    at ChildProcess.emit (node:events:369:20)
    at maybeClose (node:internal/child_process:1067:16)
    at Socket.<anonymous> (node:internal/child_process:453:11)
    at Socket.emit (node:events:369:20)
    at Pipe.<anonymous> (node:net:665:12) {
  killed: false,
  code: 255,
  signal: null,
  cmd: 'KLRINT.EXE  0<terminal-input.txt'
}

I'm left with no way to invoke KLRINT.EXE from a runtime such as Node.js.

1 Answers

I was able to employ a workaround and wanted to close-out on this thread. I noticed the application would crash if called directly from Java or Node, but would execute successfully if called from Windows Task Scheduler. As such, decided to create a windows task and invoke it from my Node application by way of the application event logger using node-windows module.

Related