Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a "nul" file?

Tags:

winapi

I need to create a new process with redirected standard error stream to some file. The code from which child process is being created has no console available, so there are cases when GetStdHandle(any) will return 0. Child process will try to duplicate all of its standard IO handles for some reason (source code for child process is unavailable) so all of it's handles should be valid.

So I need to run that process in the same manner as it's can be ran from the console with:

someproc <nul >nul 2>err

I see some ways for this: 1. Create two pair of pipes. This is possibly good solution, but it will be too complex for me. 2. Open "nul" file with CreateFile("nul", ...) function call. No file is being created by this call, but this looks weird too me. 3. Use INVALID_HANDLE_VALUE. This works too, but I think there can be different problems with another child processes.

I believe there are better ways.

like image 299
okutane Avatar asked Jan 13 '09 06:01

okutane


People also ask

What is a nul file?

The null device is a special file that discards all data written to it, but reports that the write operation succeeded. Nul is often used to hide the output (or error output) of a command. It is called NUL rather than NULL for historical reasons, many other devices have 3 character names: AUX, PRN, CON, etc.

What is null file in Android?

This is an empty or corrupted file in an operating system. They are easily identifiable by the . null extension they end with. They cannot be read or opened by any program or application on your phone or computer. You may have come across a NULL file but did not recognise it.


2 Answers

As originally phrased, you have already answered your own question. To open a "nul" file, you simply specify "nul" when you call CreateFile. It only looks weird because hardly anyone ever uses that file name. (I don't see it used nearly as often as I see /dev/null.) It's perfectly valid, though.

But if you've found that Invalid_Handle_Value works, too, then go ahead and use that instead. It's certainly easiest. I wouldn't have expected it to work, initially, since I wouldn't expect it to be duplicable.

like image 192
Rob Kennedy Avatar answered Oct 20 '22 22:10

Rob Kennedy


Yes, "nul" is doing what you think. If you move to unix, it will be "/dev/null". The funky name is a holdover from DOS days, along with "prn" and "com1", etc.

like image 3
Mark Harrison Avatar answered Oct 20 '22 23:10

Mark Harrison