tags:

views:

393

answers:

3

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 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.

+2  A: 

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.

Rob Kennedy
+2  A: 

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.

Mark Harrison
A: 

Using INVALID_HANDLE_VALUE with DuplicateHandle is improper: the documentation states that you need PROCESS_DUP_HANDLE access right on the handle. You don't have that righton INVALID_HANDLE_VALUE. Device NUL (symbolic link to /device/null) will work fine, though.

MSalters