简体   繁体   English

使用C ++在同一系统上运行的2个服务之间的通信

[英]Communication between 2 Services running on same system using C++

In C++, I've 2 services (say windows services) that are running on a system. 在C ++中,我有2个服务(例如Windows服务)在系统上运行。 Obviously both have their corresponding EXEs A.EXe & B.EXE. 显然,两者都有其对应的EXE A.EXe和B.EXE。

If i want to query & receive some data between these 2 windows services can it be done as a function call by passing a pointer of a struct from 1 service & have the other service fill the struct using the pointer passed. 如果我想查询和接收这两个Windows服务之间的数据,可以通过从1个服务传递结构的指针作为函数调用来完成,并让其他服务使用传递的指针填充该结构。

Common header file for both A & B: A和B的通用头文件:

struct Abc
    {
       int a;
    }

Now say service A sends this struct object with its pointer or as pass by reference by calling a function in service B.EXE. 现在说服务A使用其指针或通过调用服务B.EXE中的函数以引用方式传递此结构对象。 Will this function call work across 2 services & can the data be transferred between A & B this way? 此函数调用可以跨2个服务工作吗,这样可以在A&B之间传输数据吗?

Or do i need an IPC mechanism like sockets. 还是我需要IPC机制,例如套接字。

Whats the best way for achieving the data transfer between 2 services running on same system 在同一系统上运行的两个服务之间实现数据传输的最佳方法是什么?

IPC stands for "inter process communication". IPC代表“进程间通信”。 You have two services (each running in their own process) and you want to communicate between them. 您有两个服务(每个服务都在各自的进程中运行),并且您希望在它们之间进行通信。 You are absolutely going to need an IPC mechanism. 绝对需要IPC机制。

Your choices are: 您的选择是:

  • sockets (layering boos::asio on top would be an excellent idea) 套接字(将boos::asio放在顶部是一个好主意)
  • Shared memory mapping and signalling events (don't forget to use a mutex or similar, and don't forget to establish a lock hierarchy so you don't get deadlocks). 共享内存映射和信令事件(不要忘记使用互斥锁或类似的东西,也不要忘记建立锁层次结构,这样就不会出现死锁)。 This will be limited to a single machine, but that may be acceptable. 这将限于一台机器,但这是可以接受的。
  • DCOM (Windows only, but it looks like you are limited to Windows anyway). DCOM(仅Windows,但无论如何您似乎都限于Windows)。
  • Some other IPC package. 其他一些IPC软件包。

My personal preference would be sockets, but any of them could be appropriate. 我个人的喜好是套接字,但是它们中的任何一个都是合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM