如何将变量参数传递给exe?
好吧,假设我有 b.exe,它接受一个字符串参数。 我想在a.cpp中调用b.exe,使用system:
string s1 = "hallo";
system("b.exe s1");
printf("s1 after invoke = %s",s1);
这是b.cpp中的代码:
int main(string s)
{
s = "hello world";
return 0;
}
我想要的是,当我运行a.exe时,输出将是:
s1 after invoke = hello world
可以这样做吗?基本上,我只想将变量传递给 exe,但它必须通过引用,而不仅仅是通过值,因为我希望在我调用的 exe 中处理和修改该变量。我已经在互联网上搜索了解决方案,但它只为我提供了按值传递变量给exe的方法,而不是通过引用。
任何建议将非常感激,但如果可能的话,我希望在上述更正代码的形式和包含文件(如果有)。感谢您的帮助 :)
Okay, let's say that I have b.exe, which takes a string argument.
I want to invoke b.exe within a.cpp, with system:
string s1 = "hallo";
system("b.exe s1");
printf("s1 after invoke = %s",s1);
and this is the code in b.cpp:
int main(string s)
{
s = "hello world";
return 0;
}
what I want is, when I run a.exe, the output will be:
s1 after invoke = hello world
is it possible to do that? basically, i just want to pass a variable to an exe, but it must be by reference, not only by value because I want that variable to be processed and modified within the exe that I invoked. I've already searched the solution on the internet, but it only provides me tha way to pass a variable by value to the exe, not by reference..
any suggestion will be very appreciated, but if possible, I want the suggestion in the form of the above correction code and include files, if any. thanks for your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
无法在不同进程之间修改命令行参数。
s1
仅 A.CPP 已知,s
仅 B.CPP 已知。It is not possible to modify command line arguments among different processes.
s1
is known only to A.CPP, ands
is only known to B.CPP.大家好,我知道这是一篇旧文章,但每一点都有帮助:)
使用变量向外部调用的 .exe 发送参数可以很容易地通过使用 .c_str() 附加一个用于系统调用的字符串变量来完成
。最初的问题似乎是这个人想从 .exe 中获取一个变量,这是不可能的,但对于我正在搜索的内容来说,因为这是返回的第一个 Google 链接,我认为许多其他人会从中受益,所以..
示例:
// 声明一些变量
// 为系统调用编译你的字符串
// 调用你的 .exe
// 下面的完整源代码
Regards
Alphanu
Hi guys I know this is an old post but every little helps :)
Sending a argument to an externally called .exe using a variable can easily be done by appending a string variable to use for the system call using .c_str()
After reading back the original question its seems the the guy wants to take a variable from within an .exe which is not possible but for what I was searching for and since this is the first Google link that is returned and I think many others will benefit from this so..
Example:
// declare some variables
// compile your string for the system call
//call your .exe
//full source below
Regards
Alphanu
不,那不可能。另外,main 不接受字符串作为参数。
您的字符串 s1 位于保存 a.cpp 的进程的地址空间中。您无法在进程之间通过引用传递项目,而这正是您在将 s1 传递给运行 b.exe 的进程时尝试实现的目标。当您修复语法错误时,将会发生以下情况: b.exe 复制字符串的内容,并将它们保存在自己的地址空间中。
您可能希望研究进程间通信来实现类似的目标,但没有简单的方法可以做到这一点。
No, that's not possible. Also, main does not accept string as a parameter.
your string s1 is in the address space of the process which holds a.cpp. You cannot pass items by reference between processes, which is what you're trying to achieve when passing s1 to process running b.exe. Here's what will happen when you fix your syntax errors: b.exe copies the contents of the string, saving them in its own address space.
You'll want to look into inter-process communication to achieve something similar, but there's no easy way of doing it.
这是不可能的。您能做的最好的事情就是按值传递字符串。我所说的字符串实际上是指字符数组。
事实上,main的官方原型更像是
如果你想让你的程序用C++编译,你没有太多选择
It is not possible to do that. The best you can do is pass strings by value. And by strings, I really mean char arrays.
In fact, the official prototype for main is more like
If you want your program to compile in C++, you don't have much choice
您的
main(string)
语法不正确。在 C++ 中,main
可以有两个有效签名:其中
argc
包含传递给可执行文件的参数数量,argv
指向这些参数的数组。您可以将多个参数传递给
b.exe
并基于该参数编写您的程序。有关更多信息,您可以搜索main()
的签名及其使用方式。Your
main(string)
syntax is not correct. In C++main
can have two valid signatures:Where
argc
contains the number of argument passed to the executable andargv
points to the array of those arguments.You can pass several argument to
b.exe
and write your program based on that. For more info you can search for signature ofmain()
and how it is used.一个简单但当然不是最漂亮的解决方案是使用临时文件:
伪代码:
致电A)
在致电 B) 之前,完成 A) 中所需的所有操作。
从 A) 开始,将变量输出到临时文件。
从 A) 呼叫 B。
从临时文件导入变量。清除该文件。
完成 B 中需要做的一切
将 B) 中的变量输出到临时文件
B出口)
从临时文件输入变量到A)
做标准输出
退出 A)
否则,您将需要研究消息传递接口 (MPI),这并不是最简单的解决方案。
A simple, but of course, not the prettiest solution, would be to use a temporary file:
Pseudo code:
Call A)
Do everything in A) that you need before you call B).
From A), Output variables to a temporary file.
From A), call B.
Import variables from temporary file. Clear this file.
Do everything that you need to do in B)
Output variables from B) to temporary file
Exit B)
Input variables from temporary file to A)
Do standard output
Exit A)
Otherwise you'll need to look into Message Passing Interface (M.P.I.), which is not exactly the easiest solution.
不可能直接在两个进程之间修改变量,但您可以执行类似的操作来获得相同的最终结果。
最简单的方法可能是使用 _popen()。基本思想是这样的:
要将字符串参数从 A 传递到 B,可以将其作为命令行参数传递。 B 可以从 main() 中的 argv[] 数组中读取此内容。
但是要将信息返回给 B,您需要一些其他技术。 _popen() 在内部使用管道,并为您完成大部分设置工作,因此 A 可以使用 fgets() 或任何其他基于 FILE* 的函数从 B 读回输出。B 然后可以执行 printf( “你好世界”); “返回”字符串,A 可以从 _popen() 返回的 FILE* 中读取该字符串。
有关更多详细信息和示例,请参阅 关于 _popen() 的 MSDN 页面。
It's not possible to modify a variable directly between two processes, but you can do something similar to get the same end result.
Simplest way of doing this might be to use _popen(). Basic idea is this:
To pass a string argument from A to B, you can pass it as a command line parameter. B can read this from the argv[] array in main().
But to return information back to B, you need some other technique. _popen() uses pipes internally, and does much of the work of setting it up for you, so A can then use fgets() or any other FILE*-based function to read the output back from B. B can then do printf("hello world"); to 'return' the string, and A can read that string from the FILE* that _popen() returns.
See the MSDN page on _popen() for more details and an example.