C# 应用程序和非托管 C++ 之间的进程间通信 应用

发布于 2024-07-13 17:00:55 字数 70 浏览 5 评论 0原文

我有两个 Windows 服务,第一个用 C# 编写,第二个用 C# 编写 非托管C++,我想知道如何进行双向进程间通信。

I have two Windows services, the first one written in C# and the second written in
unmanaged C++, I want to know how can I do two-way interprocess communication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

_畞蕅 2024-07-20 17:00:55

如果进程间通信始终在同一台计算机上完成,则命名管道是最佳选择,因为它们比其他选项更快。

但是,如果这种通信在某个时刻跨机器边界发生的可能性很小,请采用套接字方法。 对于C++,您需要winsock2.h 头文件。 在 C# 中,使用 System.Net.Sockets< /code>命名空间。

自从我完成非托管 C++ 以来已经有一段时间了,但我的记忆是,如果您在 C++ 端创建服务器,然后使用 C# 端的TcpClient 类。

If the interprocess communication is always going to be done on the same machine, named pipes is the way to go because they are faster than other options.

However, if there is even the slightest chance that this communication might occur across machine boundaries at some point, go with the socket approach. For C++, you'll need the winsock2.h header file. In C#, use the System.Net.Sockets namespace.

It's been a while since I've done unmanaged C++, but my recollection is that you'll have to write less C++ code if you create the server on the C++ side and then use the TcpClient class on the C# side.

终止放荡 2024-07-20 17:00:55

套接字可能是您最好的选择。

使用套接字,您不必将两个程序绑定在同一台机器上。

而且,它可能是最便携的选项(哎呀,Windows 甚至有用于套接字的 select())。

Sockets are probably your best bet.

With sockets your not necessarily tied to both programs being on the same machine.

Also, it's likely to be the most portable option (heck, Windows even has select() for sockets).

风和你 2024-07-20 17:00:55

套接字和命名管道是托管和非托管环境中得到良好支持的两个选项。

Sockets and Named Pipes are two options well supported in the managed and unmanaged environments.

乖乖公主 2024-07-20 17:00:55

有多种方法可以做到这一点,但我认为最好的方法是使用 WCF 和 COM+。 如果您在 COM+ 中托管服务,则可以通过 .NET 服务中的 WCF 以及非托管代码中的 COM 接口来访问它。

您可能需要查看 MSDN 文档的以下部分来开始:

将 WCF 服务与 COM+ 集成:
http://msdn.microsoft.com/en-us/library/bb735856。 aspx

与 COM+ 应用程序集成概述:
http://msdn.microsoft.com/en-us/library/ms734723。 ASPX

There are a number of ways to do this, but I think that the best way would be to use WCF and COM+. If you host a service in COM+, you can access it through WCF in your .NET service, and through COM interfaces in your unmanaged code.

You might want to check out the following sections of the MSDN documentation to get started:

Integrating WCF Services with COM+:
http://msdn.microsoft.com/en-us/library/bb735856.aspx

Integrating with COM+ Applications Overview:
http://msdn.microsoft.com/en-us/library/ms734723.aspx

神妖 2024-07-20 17:00:55

我想说的是套接字和消息系统。 查看我们的 Google Protocol Buffers

I would say sockets and a messaging system. Check our for Google Protocol Buffers.

oО清风挽发oО 2024-07-20 17:00:55

使用 DCOM/RPC 或命名管道 - 其他任何方式要么不安全,要么黑客攻击,或两者兼而有之。

Use either DCOM/RPC or named pipes - anything else is either insecure, hacky, or both.

抚笙 2024-07-20 17:00:55

创建一个单例 COM 对象。 维护此 COM 对象中的数据,C++ 和 C# 应用程序都可以读取这些数据。

Create a Singleton COM object. Maintain data in this COM object, which can be read by both C++ and C# applications.

那支青花 2024-07-20 17:00:55

我想说 Redis 将是任何类型的进程间通信的最佳解决方案

I would say redis would be the best solution for any kind of interprocess communication

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文