views:

177

answers:

5

I have an application (A) that needs to launch another application (B). I need to pass data between the applications. I can think of two approaches. The first is to open a socket. The second is to share data via a dll.

The opening socket approach is straight forward.

The dll approach I have some questions? I can load plug-in dlls into B. I want to create a dll that A can use to pass data to B. When loading dlls, is only one instance of the dll loaded? If so, does this mean that data can be shared between applications that load the dll?

What is the better choice?

Are there other ways of doing this?

+10  A: 

You can't effectively share data via a DLL. Other ways:

  • disk files
  • pipes
  • shared memory
  • messages
  • RPC
  • CORBA
  • COM
  • etc.
anon
What is the issue of sharing data via a dll?
zooropa
@zoo It is very difficult to control, doesn't work if the DLL gets unloaded, and requires special compilation of the DLL - data in DLLs is not shared by default.
anon
look into #pragma data_seg for more info on sharing data between dlls
Greg Domjan
A: 

You can have a shared cache (example a windows service or hidden process) that can be listening - returning data to all subscribers. This using a Observer pattern approach.

Juan Zamora M
+4  A: 

The simplest method (assuming Windows since you mention a DLL) is probably to use CreateProcess and open a pipe to the child process, as described in simplified form here: http://msdn.microsoft.com/en-us/library/ms682499.aspx

Named Pipes can be an alternative, especially if you aren't in control of the lifetime of all of the processes. http://msdn.microsoft.com/en-us/library/aa365590.aspx

For simple cases, mailslots may be a sufficient alternative.

http://msdn.microsoft.com/en-us/library/aa365574.aspx#base.using_a_mailslot_for_ipc

Here's a longer list of various Interprocess Communication techniques for Windows. http://msdn.microsoft.com/en-us/library/aa365574.aspx

For something happening locally, using sockets seems sort of overkill. Plus you have to implement your own security mechanism to prevent spoofing attacks, rather than depending on the integrated security mechanism of most of the other IPC methods.

JasonTrue
@Jason: did you know you posted old links?
John Saunders
No, they all came from fresh Google queries when I posted... ah, I see that the VS version is specified in them. I don't think the content has changed though.
JasonTrue
A: 

Its always good to explore alternative possible solutions, but I personally believe that using sockets as a transport layer for data between applications is not only future proof, but scalable as well. Using sockets will eliminate the need for you to write copious amounts of OS specific code, which could proclude you from porting your application in the future to non-Windows operating systems.

I would suggest sockets.

carribus
It will, however, require that you write copious amounts of OS-neutral code, and then maintain it.
John Saunders
Yes. Point taken :)
carribus
A: 

I would agree somewhat with Juan Zamora M except that the service providing the data should have an API that can be requested when needed not pushed when changed via listeners.

Brian K Blain