简体   繁体   English

C ++下的跨进程交互

[英]Cross-process interaction under C++

Please help, how can I organize the process-process data exchange (in Windows, if that matters)? 请帮忙,我如何组织流程 - 流程数据交换(在Windows中,如果重要)?

I have process1.exe which calls process2.exe with a few command line arguments. 我有process1.exe这就要求process2.exe有一些命令行参数。 I want to track "progress" of process2 from process1 (say, some int value). 我想跟踪的“进步” process2process1 (比如说,有的int值)。 It (that int value) can be accessed from process1 permanently or each X ms - doesn't matter. 它( int值)可以从process1永久访问,或者每个X ms - 无关紧要。

Will be useful any solution: WinApi or Qt. 将是有用的任何解决方案:WinApi或Qt。

Thank you everybody! 谢谢大家! All answers are very useful! 所有答案都非常有用! :) Many thanks!! :) 非常感谢!!

OTOH: OTOH:

  • stdin/stdout redirection stdin / stdout重定向
  • Named pipe (CreateNamedPipe) 命名管道(CreateNamedPipe)
  • Anonymous pipe (CreatePipe) 匿名管道(CreatePipe)
  • Sockets (socket, connect, bind) 套接字(套接字,连接,绑定)
  • Shared memory (CreateFileMapping, MapViewOfFile) 共享内存(CreateFileMapping,MapViewOfFile)
  • Windows messages (eg WM_APP) Windows消息(例如WM_APP)

Pick one - Windows messages or shared memory may be the easier ones. 选择一个 - Windows消息或共享内存可能更容易。

There are many options here: 这里有很多选择:

  1. You can redirect the standard output of process2 and have it output updates however often you like 您可以重定向process2的标准输出,并让它经常输出更新
  2. If stdout is being used for something else, you can use a named pipe between the processes 如果将stdout用于其他内容,则可以在进程之间使用命名管道
  3. You can also use named shared memory , which will require less overhead and probably be easier to implement (the downside is that you may need to also do cross-process synchronization) 您还可以使用命名共享内存 ,这将需要更少的开销,并且可能更容易实现(缺点是您可能还需要执行跨进程同步)
  4. If process1 is running a message pump, then you can also use normal Windows messages (look at WM_COPYDATA) 如果process1正在运行消息泵,那么您也可以使用正常的Windows消息(查看WM_COPYDATA)

There are several methods: 有几种方法:

  • Sockets 套接字
  • Messages 消息
  • Shared Memory (files) 共享内存(文件)

The issue is that Process2 will be broadcasting and Process1 will be listening. 问题是Process2将进行广播,Process1将进行监听。 Process1 will need to know when Process2 is finished and maybe the percentage complete. Process1需要知道Process2何时完成,可能还有完成的百分比。

I believe Sockets would be the better route, but that depends on the application, development schedule and familiarity of concepts. 我相信套接字将是更好的途径,但这取决于应用程序,开发时间表和概念的熟悉程度。

您只需使用Windows Api(SendMessage)发送消息即可。

An obvious omission in the options presented so far is COM. 到目前为止,在选项中明显遗漏的是COM。 I'm expecting the usual flurry of "COM is crap" responses, but in my experience this hasn't been the case. 我期待通常的“COM是废话”反应,但根据我的经验,情况并非如此。

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

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