C++ popen command without console

c++, command, console, popen

Solution

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.

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.

Problem

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.

Original source

Related problems