C# (.NET 2.0) 中 Windows 的进程间通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:03:06  来源:igfitidea点击:

Interprocess communication for Windows in C# (.NET 2.0)

提问by Vinko Vrsalovic

I've never had to do IPC on Windows before. Currently I'm developing a pair of programs, a standard GUI/CLI app and a windows service. The app has to tell the service what to do. So, assuming the communication is local only, what would be the best communication method for these two processes?

我以前从未在 Windows 上进行过 IPC。目前我正在开发一对程序、一个标准的 GUI/CLI 应用程序和一个 Windows 服务。该应用程序必须告诉服务该做什么。那么,假设通信仅在本地进行,那么这两个进程的最佳通信方法是什么?

Where best is defined as more robust and less error prone, not the most performant nor the easiest to code.

最好的定义为更健壮且不易出错,而不是性能最好的,也不是最容易编码的。

Code examples will be very welcome, but aren't required :-)

代码示例将非常受欢迎,但不是必需的:-)

Note I'm asking about what to use, a standard TCP socket, named pipes, or some other means of communication only.

注意我问的是要使用什么,标准的 TCP 套接字、命名管道或其他一些通信方式。

Thanks!

谢谢!

采纳答案by Edward Wilde

IPC in .Net can be achieved using:

.Net 中的 IPC 可以使用:

WCF

周转基金

using named pipes requires .Net 3.0and above.

使用命名管道需要 .Net 3.0及更高版本。

Code example

代码示例



Remoting

远程处理

The original IPC framework released with .Net 1.0. I believe remoting is no longer being actively developed, and you are encouraged to use WCF instead

与 .Net 1.0 一起发布的原始 IPC 框架。我相信远程处理不再被积极开发,并且鼓励您改用 WCF

Code example

代码示例

Inter-process communication via Remoting- uses a tcp channel

通过远程处理的进程间通信- 使用 tcp 通道

Resources

资源



Win32 RPC using csharptest-net RpcLibrary

Win32 RPC 使用 csharptest-net RpcLibrary

I came across a project recently that has wrapped the Win32 RPC library and created a .net class library that can be used for local and remote RPC

最近遇到一个项目,它封装了Win32 RPC库,创建了一个.net类库,可以用于本地和远程RPC

Project home page: http://csharptest.net/projects/rpclibrary/

项目主页http: //csharptest.net/projects/rpclibrary/

MSDN references:

MSDN参考资料:

Also has a google protocol buffers rpc client that runs on top of the library: https://code.google.com/p/protobuf-csharp-rpc/

还有一个运行在库之上的 google protocol buffers rpc 客户端:https: //code.google.com/p/protobuf-csharp-rpc/



WM_COPYDATA

WM_COPYDATA

For completeness it's also possible to use the WIN32 method with the WM_COPYDATAmessage. I've used this method before in .Net 1.1 to create a single instance application opening multiple files from windows explorer.

为了完整起见,还可以将 WIN32 方法与WM_COPYDATA消息一起使用。我之前在 .Net 1.1 中使用过这种方法来创建一个从 Windows 资源管理器打开多个文件的单实例应用程序。

Resources

资源

Sockets

插座

Using a custom protocol (harder)

使用自定义协议(更难)

回答by Guy Starbuck

For local only, we have had success using Named Pipes. Avoids the overhead of TCP, and is pretty much (at least for .NET) as efficient as you can get while also having a decent API to work with.

仅对于本地,我们已成功使用命名管道。避免了 TCP 的开销,并且几乎(至少对于 .NET 而言)尽可能高效,同时还有一个不错的 API 可以使用。

回答by Brian Ensink

Since you are limited to .Net 2.0 WCF is perhaps not an option. You could use .Net remoting with shared memory as the underlying communication mechanism between app domains on the same machine. Using this approach you can easily put your processes on different machines and replace the shared memory protocol with a network protocol.

由于您仅限于 .Net 2.0 WCF 可能不是一个选择。您可以使用带有共享内存的 .Net 远程处理作为同一台机器上应用程序域之间的底层通信机制。使用这种方法,您可以轻松地将您的进程放在不同的机器上,并用网络协议替换共享内存协议。

回答by Larry Foulkrod

Your best bet is to use WCF. You will be able to create a service host in the windows service and expose a well defined interface that the GUI application can consume. WCF will let you communicate via named pipes if you choose, or you can choose any other communication protocal like TCP, HTTP, etc. Using WCF you get great tool support and lots of available information.

最好的办法是使用 WCF。您将能够在 windows 服务中创建服务主机并公开 GUI 应用程序可以使用的明确定义的接口。如果您愿意,WCF 将允许您通过命名管道进行通信,或者您可以选择任何其他通信协议,如 TCP、HTTP 等。使用 WCF,您可以获得出色的工具支持和大量可用信息。

回答by Shafqat Ahmed

The standard method of communicating with a windows service is to use service control codes. Windows services can receive codes from 0 to 255. 0-127 is reserved for system. 128 to 255 can be used for custom commands.

与 Windows 服务通信的标准方法是使用服务控制代码。Windows 服务可以接收从 0 到 255 的代码。0-127 是为系统保留的。128 到 255 可用于自定义命令。

If you need to send complex objects to the service use database, xml, file, tcp, http etc. Other than that for sending control commands like reload configuration, process items etc this control codes should be used.

如果您需要向服务使用数据库、xml、文件、tcp、http 等发送复杂对象。除了发送控制命令(如重新加载配置、处理项目等)之外,还应使用此控制代码。

There are additional functionalities available such as querying the service. See Windows service documentation and api.

还有其他功能可用,例如查询服务。请参阅 Windows 服务文档和 api。

http://arcanecode.com/2007/05/30/windows-services-in-c-sending-commands-to-your-windows-service-part-7/

http://arcanecode.com/2007/05/30/windows-services-in-c-sending-commands-to-your-windows-service-part-7/

回答by Nicholas Hill

I'd like to add to this discussion. Please rebuke me if this is way out there - but couldn't a semaphore (or multiple semaphores) be used for rudimentary communication?

我想补充一下这个讨论。如果这是出路,请责备我 - 但不能使用信号量(或多个信号量)进行基本通信吗?