Passing Sockets Between Processes

by Warren Young and Frank Schmied

If you want to pass a socket from one process to another, the only offically sanctioned way to do this is with Winsock 2’s new WSADuplicateSocket() function. The FAQ originally covered this issue this way:

The original FAQ item then said that you can’t do this with Winsock 1.1. As it turns out, Frank Schmied showed how you can do this under Microsoft’s Winsock 1.1 stack on Win32, with a bit of trickery. Frank wrote:

As I understand it, the source process converts its local SOCKET handle to a real handle with OpenProcess(), then passes that value and its process ID to the destination process. The destination process then calls Frank’s ConvertProcessSocket() function to convert the real handle to a local handle which you can use with Winsock. Notice that the DuplicateHandle() call closes the source process’s handle, and then the CloseHandle() call closes the real handle you passed to the destination process.

Caveats: Frank’s technique probably only works with Microsoft stacks. It does not work on Win16, and may not work on WinCE. It may or may not work in the presence of Layered Service Providers, except on Windows NT 4.0 SP 4+, which patches the Installable FileSystems (IFS) layer to make this sort of thing work. It may not work on the second Tuesday of the month, when the moon is in its third quarter, unless you chant "oompa hoozah" four times slowly before running the program. In short, this is trickery: you have been warned. :)

Copyright © 2000 by Warren Young and Frank Schmied. All rights reserved.

<< CSocket Considered Harmful
Dealing with Firewalls >>
Updated Sun Jan 18 2015 04:24 MST   Go to my home page