There’s a technique in Unix where you can pass a file descriptor (FD) from one process to another through Unix domain sockets on BSD or pipes on SysV. However, how do you do it on Win32? The “Unix emulation” that Windows provides, called Interix, says in its UNIX Application Migration Guide:
The two sockets functions recvmsg and sendmsg appear in many network applications but are not supported by Interix. These functions are the only way to pass an open file descriptor from one running process to another running process.
This doesn’t make it sound very promising. Can you really not pass FD’s between processes on Win32? In the hopes that someone else out there who knows Win32 much better than I do has already solved this, I’m asking for help. If you know of a way to pass FDs on Win32, please leave me a note in the comments or send me an email. Remember, I know how to do it on Unix already. I’m asking specifically for how to do it on Win32, if it’s even possible. Thanks.
Have you tried a sequence like:
_get_osfhandle -> DuplicateHandle -> _open_osfhandle?
You know, I looked at _get_osfhandle and _open_osfhandle, but I totally missed DuplicateHandle.
On the MSDN Library page for DuplicateHandle, it says:
“If the process that calls DuplicateHandle is not also the target process, the source process must use interprocess communication to pass the value of the duplicate handle to the target process.”
So, I’d use a pipe and write the value of the duplicated handle across to the child process? I guess coming from Unix, the notion that one process can duplicate a filehandle into another process’s space is … unsettling. :-)
Thanks for the pointer, Jamie. You’re the best.
I think several forms of interprocess communication would work, it depends on your situation. Named pipe is an option. I’m assuming the processes are unrelated, since a child process can inherit handles from the parent – it’s a parameter of CreateProcess. I haven’t ever needed to do this with unrelated processes, so I hope it works. :-)