简体   繁体   English

使用环境变量的两个应用程序之间的通信

[英]Communication between two applications using Environment Variables

Question

How to communicate with another program (for instance, a windows service one) through environment variables (not system or user ones)? 如何通过环境变量(而不是系统或用户变量)与另一个程序(例如,Windows服务程序)通信?

What do we have 我们有什么

Well, I have the following scheme for a data logger: 好吧,对于数据记录器,我有以下方案:

-------------------------          --------------------------------
| 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 |
                                                         --------

On current development: 关于当前的发展:

  • windows service is always running when Windows is running Windows运行时Windows服务始终运行
  • user can open and close user interface (of course :p) 用户可以打开和关闭用户界面(当然:p)
  • windows service acquires data from sensors Windows服务从传感器获取数据
  • user interface automatic requests data to windows service every 100ms and shows it to user via udp communication through some implemented protocol (we call it GetData() command and response to it) 用户界面每100毫秒自动向Windows服务请求数据,并通过一些已实现的协议通过udp通信向用户显示数据(我们称其为GetData()命令并对其进行响应)
  • user can send some other commands to change the data to acquire through implemented protocol (we call it SetSensors() command and response to it) 用户可以发送其他命令来更改要通过已实现的协议获取的数据(我们将其SetSensors()命令并对其进行响应)

Both user interface and windows service are developed on Borland C+ Builder 6 and use NMUDP component, from FastNet tab, for UDP communication. 用户界面和Windows服务都在Borland C + Builder 6上开发,并使用FastNet选项卡中的NMUDP组件进行UDP通信。

What we are thinking to do 我们在想做什么

Because of some buffer issues and to free udp channel only for sending SetSensors() command and response to it, we are considering that instead of using GetData() : 由于某些缓冲区问题,并且为了释放udp通道而仅用于发送SetSensors()命令并对其进行响应,我们正在考虑代替使用GetData()

  • Windows service would get data from sensors and put them on environment variables Windows服务将从传感器获取数据并将其放在环境变量中
  • the user interface would read them to show to user 用户界面将读取它们以显示给用户

Scheme after doing what we are thinking 做完我们想过的计划

-------------------------          --------------------------------
| 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 |
                                                         --------

Any way to do that? 有什么办法吗?

We would not use system and user environment variables, because it writes on Windows Registry, ie, it will save to hard drive and it gets more slow... 我们不会使用系统和用户环境变量,因为它是在Windows注册表上编写的,即它将保存到硬盘驱动器并且变得越来越慢...

As @HansPassant said, I cannot do that directly. 正如@HansPassant所说,我不能直接这样做。 Although I saw some ways to do that via memory mapped file, it is so easy only to add one more udp communication channel through other port. 尽管我看到了一些通过内存映射文件执行此操作的方法,但仅通过另一个端口添加一个udp通信通道非常容易。 So: 所以:

-------------------------          --------------------------------
| 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 |
                                                         --------

If someone provide a better solution, I'll mark it as solution in future. 如果有人提供更好的解决方案,我会在以后将其标记为解决方案。

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

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