没有控制台的 C++ popen 命令

C++ popen command without console(没有控制台的 C++ popen 命令)
本文介绍了没有控制台的 C++ popen 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 popen 获取命令的输出时,比如 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?

我正在使用 Visual C++,并希望创建一个库来返回某些命令的输出,例如 dir.

I am using Visual C++ and want to make an Library to return the output of some command, say, dir.

推荐答案

假设 Windows(因为这是唯一普遍存在这种行为的平台):

Assuming Windows (since this is the only platform where this behavior is endemic):

CreatePipe() 来创建通信所需的管道,以及 CreateProcess创建子进程.

CreatePipe() to create the pipes necessary to communicate, and CreateProcess to create the child process.

HANDLE StdInHandles[2]; 
HANDLE StdOutHandles[2]; 
HANDLE StdErrHandles[2]; 

CreatePipe(&StdInHandles[0], &StdInHandles[1], NULL, 4096); 
CreatePipe(&StdOutHandles[0], &StdOutHandles[1], NULL, 4096); 
CreatePipe(&StdErrHandles[0], &StdErrHandles[1], NULL, 4096); 


STARTUPINFO si;   memset(&si, 0, sizeof(si));  /* zero out */ 

si.dwFlags =  STARTF_USESTDHANDLES; 
si.hStdInput = StdInHandles[0];  /* read handle */ 
si.hStdOutput = StdOutHandles[1];  /* write handle */
si.hStdError = StdErrHandles[1];  /* write handle */

/* fix other stuff in si */

PROCESS_INFORMATION pi; 
/* fix stuff in pi */


CreateProcess(AppName, commandline, SECURITY_ATTRIBUTES, SECURITY_ATTRIBUTES, FALSE, CREATE_NO_WINDOW |DETACHED_PROCESS, lpEnvironment, lpCurrentDirectory, &si, &pi); 

这不仅仅是让你走上你希望完成的道路.

This should more than get you on your way to doing what you wish to accomplish.

这篇关于没有控制台的 C++ popen 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)