0

We have a legacy vb6 automation application that communicate over a sockets on need basis. But opening and establishing connection (only when required) to the remote port taking more time frequently.

So,i am planning to write other application (say a socket server) that opens the required sockets and keep the connections alive.This application will write connected socket handle values to a file or database.

Is it possible in vb6 to create a socket object using socket handle from the already opened socket that was owned by other process (socket server application in this case)?

ramu
  • 1,019
  • 3
  • 15
  • 41

2 Answers2

2

No, Windows sockets cannot be shared cross-process, not even through handle inheritance (this is because although it is usually a handle, an LSP might return something that is not a handle and thus not inherited). You should make one process open and maintain the connection and the others talk to that process to communicate with the server.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
2

This is exactly the type of situation that WSADuplicateSocket() is intended for.

Your "server" can create a socket and use WSADuplicateSocket() to fill a WSAPROTOCOL_INFO record that describes the socket. The "server" can then expose the WSAPROTOCOL_INFO to your VB app using any IPC mechanism you want. The VB app can pass the WSAPROTOCOL_INFO to WSASocket() to access the socket and use it as needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Interesting answer! Unfortunately there are minimum OS requirements for the API, and it would require the OP to be using socket APIs directly. Given the language is VB6 I suspect they are using a COM component to access TCP/IP sockets. – tcarvin Oct 02 '14 at 12:40
  • Despite what MSDN claims, `WSADuplicateSocket()` is available on XP (`DuplicateHandle()` can be used instead on Win2000 and earlier). And VB6 can access Winsock directly (see http://www.thevbzone.com/modWINSOCK.bas). – Remy Lebeau Oct 02 '14 at 15:37
  • Oh yes, I'm sure it can. But I'd still bet a nickle they are currently using a COM object to do it now, and the effort in converting over might be more than what the OP bargained for. (And thanks for the additional info on `WSADuplicateSocket`, good to know). – tcarvin Oct 02 '14 at 15:52
  • @Remy Lebeau,Thank you for the informative answer. I think your answer requires accesing win32 API Functions,i will try with your suggested approach. Actually,existing app is using winsock control.Can it be possible create the winsock control from the handle provided by socket server?. – ramu Oct 03 '14 at 06:53
  • @ramu: no, VB's [Winsock control](http://msdn.microsoft.com/en-us/library/aa228119.aspx) does not support that, you would have to access the Winsock API directly. – Remy Lebeau Oct 03 '14 at 16:30