What is the preferred way of passing data between

2019-01-14 04:48发布

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?

6条回答
\"骚年 ilove
2楼-- · 2019-01-14 05:11

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-14 05:11

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.

查看更多
来,给爷笑一个
4楼-- · 2019-01-14 05:12
对你真心纯属浪费
5楼-- · 2019-01-14 05:13

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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-14 05:21

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.

查看更多
成全新的幸福
7楼-- · 2019-01-14 05:24

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

  • disk files
  • pipes
  • shared memory
  • messages
  • RPC
  • CORBA
  • COM
  • etc.
查看更多
登录 后发表回答