C++没有控制台的 popen 命令
当我使用 popen 获取命令的输出(例如 dir)时,它将提示出控制台。
但是,我可以在不出现控制台的情况下获得命令的输出吗?
我正在使用 Visual C++,想要创建一个库来返回某些命令(例如 dir)的输出。
when I use popen to get the output of a command, say dir, it will prompt out a console.
however, can I get the output of a command without the appearance of the console?
I am using Visual C++ and want to make an Library to return the output of some command, say, dir.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 Windows(因为这是唯一普遍存在此行为的平台):
CreatePipe() 创建通信所需的管道,并且 CreateProcess 创建子进程。
这应该不仅仅能让你朝着你想要完成的目标前进。
Assuming Windows (since this is the only platform where this behavior is endemic):
CreatePipe() to create the pipes necessary to communicate, and CreateProcess to create the child process.
This should more than get you on your way to doing what you wish to accomplish.
也许是这样的?
此代码将返回输出,但必须等待该过程完成。
Maybe something like this?
This code will return the output but it has to wait for the process to finish.
对于 POSIX,它应该是这样的:
你当然应该检查返回值等......
With POSIX it should be something like this:
You should off course check return values etc...
我需要为我的全屏 OpenGL Windows 应用程序解决这个问题,但无法阻止控制台窗口弹出。相反,在短暂的延迟后收回焦点似乎足以避免看到它。
更新:如果程序是从资源管理器启动的,这显然不起作用。从 Visual Studio 启动时它可以工作。
I needed to solve this for my full screen OpenGL Windows application, but was unable to prevent the console window popping up. Instead, taking back focus after a short delay seems to work well enough to avoid seeing it.
Update: this apparently doesn't work if the program is launched from Explorer. It is working when launched from Visual Studio.