Click here to Skip to main content
Click here to Skip to main content

Execute a Console Application From VC++

, 16 Apr 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
To execute a console application from VC++ and retrieve the messages shown in the console. Get output messages from a DOS or console application to our MFC Application.


To Execute a Console Application From VC++ and retrieve the messages shown in the console.

In many situations we may need to execute a console application or a DOS application from within our MFC application. ShellExcecute can be used for this purpose, but can only be used to run the application. Messages shown in the console is not reachable. In such cases the following procedure can help.

Here we create a read write pipe (two separate pipes, one for reading and one for writing).

Then we use CreateProcess to execute the process. Createprocess must be supplied with pointers to variables of STARTUPINFO and PROCESS_INFORMATION structures.

The pipes created are assigned to STARTUPINFO structure before it is passed to the CreateProcess function.

The CreateProcess function is used to run a new program.

        LPCWSTR lpszImageName, 
        LPCWSTR lpszCmdLine, 
        LPSECURITY_ATTRIBUTES lpsaProcess, 
        LPSECURITY_ATTRIBUTES lpsaThread, 
        BOOL fInheritHandles, 
        DWORD fdwCreate, 
        LPVOID lpvEnvironment, 
        LPWSTR lpszCurDir, 
        LPSTARTUPINFOW lpsiStartInfo, 
        LPPROCESS_INFORMATION lppiProcInfo);
  • lpszImageName

    Pointer to a null-terminated string that specifies the module to execute.

  • lpszCmdLine

    Pointer to a null-terminated string that specifies the command line to execute. The system adds a null character to the command line, trimming the string if necessary, to indicate which file was actually used.

The function ExecuteExternalFile, takes two arguments:

  1. the application to be executed.
  2. the arguments.

It executes the application and returns the messages that are printed into the console as a CString.

CString ExecuteExternalFile(CString csExeName, CString csArguments)
  CString csExecute;
  csExecute=csExeName + " " + csArguments;
  secattr.nLength = sizeof(secattr);
  secattr.bInheritHandle = TRUE;

  HANDLE rPipe, wPipe;

  //Create pipes to write and read data
  char command[1024]; strcpy(command,  

  //Create the process here.
  CreateProcess(0 command,0,0,TRUE,

  //now read the output pipe here.
  char buf[100];
  DWORD reDword; 
  CString m_csOutput,csTemp;
  BOOL res;
  return m_csOutput;

Hope this code will be useful for you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
Software Engineer,
Technopark, Kerala.

Rx 135 Owner
Yamaha Fan.

Comments and Discussions

Questionsome time ReadFile not responding PinmemberLe@rner15-Apr-13 2:53 
GeneralMissing calls to CloseHandle, causes memory leak PinmemberMember 15897532-May-11 5:42 
GeneralI found another method, by searching in msdn.... PinmemberSteppenwolf19-Jan-11 22:48 
GeneralMy vote of 2 PinmemberSteppenwolf19-Jan-11 22:43 
QuestionAdd write functionality PinmemberMarco Fiaschi2-Aug-10 9:07 
GeneralProblem with buffering Pinmembertoschu7220-Nov-08 2:37 
GeneralParser 2 Pinmembersuhi22-Apr-08 4:37 
GeneralParser Pinmembersuhi22-Apr-08 4:22 
Hi All,
I am new to MFC.
Can anyone provide me the sample code in MFC for writing parser for
text file.

QuestionHow to receive numeric results Pinmembersmzhaq26-Sep-07 19:10 
AnswerRe: How to receive numeric results PinmemberEvan Lin4-Sep-08 17:06 
QuestionPossible with MFC application Pinmemberkazim bhai30-Aug-07 3:00 
GeneralNice one. Thanks Pinmemberhungrytom28-Aug-07 0:59 
QuestionGreat job, but seem not compatible with unicode ? PinmemberOlivier Booklage22-Feb-07 10:57 
AnswerRe: Great job, but seem not compatible with unicode ? PinmemberOlivier Booklage22-Feb-07 21:57 
GeneralGreat! Keep it up Pinmemberrm_pkt7-Feb-07 20:29 
Generalcool Pinmemberzoz18-Jan-07 11:44 
GeneralCool n Nice implementation Pinmemberrassg17-Jan-07 19:36 
QuestionCan we keep the output in the console as well? Pinmemberc.sunita3-Jan-07 8:58 
GeneralCool! PinmemberSteve_pqr14-Dec-06 10:00 
Generalexcellent Pinmemberstephen(china)12-Oct-06 20:42 
GeneralExcellent article-just what I wanted Pinmemberdba4-Sep-06 0:16 
Generalsystem call Pinmemberhimakiran25-Aug-06 12:42 
QuestionProcess waiting answer PinmemberRickyC10-Apr-06 2:42 
AnswerRe: Process waiting answer PinmemberSmith_TVPM4-Sep-06 22:16 
GeneralRe: Process waiting answer Pinmember12kaunas29-Mar-07 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 16 Apr 2005
Article Copyright 2005 by Smith_TVPM
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid