简体   繁体   English

从另一个程序运行一个程序

[英]Running a program from another program

I have a C# program(call it A) that takes a photo and transforms it in a 2d array of values.我有一个 C# 程序(称为 A),它拍摄一张照片并将其转换为二维值数组。 I also have a c++(call it B) program that use as input that type of matrix.我还有一个 C++(称之为 B)程序,它使用该类型的矩阵作为输入。 What I want to do is to call A by code written in B. I have no idea how to specify input for A (don't know if it's possible in any way).我想要做的是通过用 B 编写的代码调用 A。我不知道如何为 A 指定输入(不知道是否有可能)。 The only solution I found is to have a directory in which I have the photo(s) and a file that first is opened written in and closed by A when it is called and than B open it again and read it.我找到的唯一解决方案是有一个目录,其中包含照片和文件,该文件首先在 A 被调用时写入并关闭,然后 B 再次打开并读取它。 The question is: Is there a way to give input to a program called from another program?问题是:有没有办法向从另一个程序调用的程序提供输入?

The simplest way to do it is by using a pipeline.最简单的方法是使用管道。 In terminal it would look like this:在终端它看起来像这样:

programA < image | programB

There are many ways to pass input from one program to another.多种方法可以将输入从一个程序传递到另一个程序。

  • Write a file in one program and read it in another.在一个程序中写入一个文件并在另一个程序中读取它。
  • Create a shared memory region and store the data there (use for example boost::shared_memory , or shm_open on Posix, or CreateFileMapping on Windows.创建一个共享内存区域并将数据存储在那里(例如在 Posix 上使用boost::shared_memoryshm_open ,或在 Windows 上使用CreateFileMapping

  • Write the data to stdout in one program, read from stdin in the other and pipe the two programs together.在一个程序中将数据写入 stdout,在另一个程序中从 stdin 读取并将两个程序连接在一起。

  • Create a pipe and write the data to one end of the pipe, and read from the other end.创建一个管道并将数据写入管道的一端,并从另一端读取。
  • Have one program listen on a socket, and the other send data to it.让一个程序监听一个套接字,另一个程序向它发送数据。
  • More更多的

Depending on usage, all of these can be appropriate, but I would stick to writing a file to start with.根据使用情况,所有这些都可以是合适的,但我会坚持写一个文件开始。 It makes it much easier to separately test and debug the two programs.这样可以更容易地分别测试和调试这两个程序。 Rather than using a fixed directory and filename, I would have A write to a file, and then invoke B with the name of the file on the command line.我不会使用固定的目录和文件名,而是让 A 写入文件,然后在命令行上使用文件名调用 B。

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

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