【问题标题】:Communication between two applications using Environment Variables使用环境变量在两个应用程序之间进行通信
【发布时间】:2013-05-27 18:25:16
【问题描述】:

问题

如何通过环境变量(不是系统或用户变量)与另一个程序(例如,Windows 服务)进行通信?

我们有什么

好吧,我有以下数据记录器方案:

-------------------------          --------------------------------
| the things to measure |          | the things that do something |
-------------------------          --------------------------------
     |                                  ^
     | sensors                          | switches
     V                                  |
-------------------------------------------------------------------
|                     dedicated hardware                          |
-------------------------------------------------------------------
     | ^
     | |  serial communication 
     V |
---------------                                       -------------
|   Windows   | ------------------------------------> |   user    |
|   service   | <------------------------------------ | interface |
---------------           udp communication           -------------
                                                            |^  keyboard
                                                            V| and screen
                                                         --------
                                                         | user |
                                                         --------

关于目前的发展:

  • Windows 服务在 Windows 运行时始终在运行
  • 用户可以打开和关闭用户界面(当然:p)
  • Windows 服务从传感器获取数据
  • 用户界面每100ms自动向windows服务请求数据,并通过一些实现的协议通过udp通信向用户显示(我们称之为GetData()命令和响应)
  • 用户可以通过实现的协议发送一些其他命令来改变要获取的数据(我们称之为SetSensors()命令和响应)

用户界面和 Windows 服务都是在 Borland C+ Builder 6 上开发的,并使用 FastNet 选项卡中的 NMUDP 组件进行 UDP 通信。

我们想做什么

由于一些缓冲区问题和释放 udp 通道仅用于发送 SetSensors()command 和响应它,我们正在考虑使用 GetData() 代替:

  • Windows 服务会从传感器获取数据并将其放入环境变量中
  • 用户界面会读取它们以显示给用户

做我们想做的事后的计划

-------------------------          --------------------------------
| the things to measure |          | the things that do something |
-------------------------          --------------------------------
     |                                  ^
     | sensors                          | switches
     V                                  |
-------------------------------------------------------------------
|                     dedicated hardware                          |
-------------------------------------------------------------------
     | ^
     | |  serial communication 
     V |
---------------                                       -------------
|             | ------------------------------------> |           |
|             |         environment variables         |           |
|             |         (get data from sensors)       |           |
|   Windows   |                                       |   user    |
|   service   |                                       | interface |
|             |                                       |           |
|             | ------------------------------------> |           |
|             | <------------------------------------ |           |
---------------           udp communication           -------------
                        (send commands to service)          |^  keyboard
                                                            V| and screen
                                                         --------
                                                         | user |
                                                         --------

有什么办法吗?

我们不会使用系统和用户环境变量,因为它会写入 Windows 注册表,也就是说,它会保存到硬盘驱动器并且变得更慢...

【问题讨论】:

  • 你不能和环境变量通信,一个进程只能更新他们的本地副本。使用 Windows 中可用的任何进程互操作机制,如套接字、命名管道或内存映射文件。使用 UDP 没有意义,使用 TCP 以便您获得交付保证。当 UDP 不在网络上时,它不会更快。

标签: windows windows-services environment-variables c++builder


【解决方案1】:

正如@HansPassant 所说,我不能直接这样做。虽然我看到了一些通过内存映射文件来做到这一点的方法,但只需通过其他端口添加一个额外的 udp 通信通道就很容易了。所以:

-------------------------          --------------------------------
| the things to measure |          | the things that do something |
-------------------------          --------------------------------
     |                                  ^
     | sensors                          | switches
     V                                  |
-------------------------------------------------------------------
|                     dedicated hardware                          |
-------------------------------------------------------------------
     | ^
     | |  serial communication 
     V |
---------------                                       -------------
|             | ------------------------------------> |           |
|             |           udp communication (port 3)  |           |
|             |         (get data from sensors)       |           |
|   Windows   |                                       |   user    |
|   service   |                                       | interface |
|             |                             (port 1)  |           |
|             | ------------------------------------> |           |
|             | <------------------------------------ |           |
---------------           udp communication (port 2)  -------------
                        (send commands to service)          |^  keyboard
                                                            V| and screen
                                                         --------
                                                         | user |
                                                         --------

如果有人提供更好的解决方案,我会在以后将其标记为解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2011-08-03
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多