windows 使用 ShellExecuteEx 并捕获标准输入/输出/错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/469152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:52:41  来源:igfitidea点击:

Using ShellExecuteEx and capturing standard in/out/err

c++cwindowswinapi

提问by alrashedf

I'm using ShellExecuteExto execute a command in C. Is there a way to use ShellExecuteExand capture standard in/out/err?

我正在使用ShellExecuteExC 执行命令。有没有办法使用ShellExecuteEx和捕获标准输入/输出/错误?

Note: I don't want to use CreateProcess.

注意:我不想使用CreateProcess.

采纳答案by KPexEA

As mentioned by pilif and Bob, you need to use CreateProcess.

正如 pilif 和 Bob 所提到的,您需要使用CreateProcess.

If you want code that wraps it all up for you, I do have a class for this exact issue at:

如果您想要为您包装所有内容的代码,我确实有一个针对这个确切问题的课程:

http://code.google.com/p/kgui/source/browse/trunk/kguithread.cpp.

http://code.google.com/p/kgui/source/browse/trunk/kguithread.cpp

The class (kGUICallThread) handles Linux, macOS and Windows versions. The code is licensed LGPL.

类 ( kGUICallThread) 处理 Linux、macOS 和 Windows 版本。该代码已获得 LGPL 许可。

回答by alrashedf

I use to found the problem like you.

我曾经像你一样发现了这个问题。

Suppose, You want to capture the output from STDOUT that it's generated by dircommand and save the captured into out.txt.

假设,您想从dir命令生成的 STDOUT 中捕获输出并将捕获的内容保存到out.txt 中

  1. Use text editor and type dir > out.txtand save it with mybat.bat(*.bat, don't *.txt)

  2. In your c/c++ program, type WinExec("mybat.bat", SW_HIDE);and run your application.

  3. Open the out.txtyou will see the name of folders and files in current directory.

  1. 使用文本编辑器输入dir > out.txt并用mybat.bat(*.bat, 不要 *.txt) 保存

  2. 在你的 c/c++ 程序中,输入WinExec("mybat.bat", SW_HIDE); 并运行您的应用程序。

  3. 打开out.txt,您将看到当前目录中的文件夹和文件的名称。

Also, you can run any executable files (*.exe) at the same way as follow.

此外,您可以按照以下相同的方式运行任何可执行文件 (*.exe)。

xxx.exe > out.txt

xxx.exe > 输出.txt

I hope it can be helps you. Sorry, my English really not good.

我希望它可以帮助你。对不起,我的英语真的不好。

回答by pilif

That's not possible. ShellExecute(Ex) basically executes the application in the context of the shell - so you are basically doing what explorer does.

那是不可能的。ShellExecute( Ex) 基本上是在 shell 的上下文中执行应用程序 - 所以你基本上是在做资源管理器所做的。

Capturing STDIN and STDOUT is something the shell generally doesn't do, you you will have to go the CreateProcessroute (which, after all, is what ShellExecuteeventually calls if the file to execute is a program and the verb is 'open').

捕获 STDIN 和 STDOUT 是 shell 通常不会做的事情,你将不得不走这CreateProcess条路线(毕竟,ShellExecute如果要执行的文件是一个程序并且动词是“打开” ,最终会调用它)。

回答by Bob Moore

No. The only way to do this is to use CreatePipeand CreateProcess. See the MSDN article here

不可以。唯一的方法是使用CreatePipeCreateProcess。请参阅此处的 MSDN 文章

回答by Jason

CreateProcess is what most people use.

CreateProcess 是大多数人使用的。

You may also want to consider using _popen

您可能还想考虑使用 _popen

http://msdn.microsoft.com/en-us/library/96ayss4b%28VS.80%29.aspx

http://msdn.microsoft.com/en-us/library/96ayss4b%28VS.80%29.aspx