Click here to Skip to main content
11,639,765 members (59,601 online)
Click here to Skip to main content

Execute a Console Application From VC++

, 16 Apr 2005 CPOL 110.2K 57
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.

Introduction

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.

CreateProcess( 
        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;
  
  SECURITY_ATTRIBUTES secattr; 
  ZeroMemory(&secattr,sizeof(secattr));
  secattr.nLength = sizeof(secattr);
  secattr.bInheritHandle = TRUE;

  HANDLE rPipe, wPipe;

  //Create pipes to write and read data
  CreatePipe(&rPipe,&wPipe,&secattr,0);
  //
  STARTUPINFO sInfo; 
  ZeroMemory(&sInfo,sizeof(sInfo));
  PROCESS_INFORMATION pInfo; 
  ZeroMemory(&pInfo,sizeof(pInfo));
  sInfo.cb=sizeof(sInfo);
  sInfo.dwFlags=STARTF_USESTDHANDLES;
  sInfo.hStdInput=NULL; 
  sInfo.hStdOutput=wPipe; 
  sInfo.hStdError=wPipe;
  char command[1024]; strcpy(command,  
          csExecute.GetBuffer(csExecute.GetLength()));

  //Create the process here.
  CreateProcess(0 command,0,0,TRUE,
          NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
  CloseHandle(wPipe);

  //now read the output pipe here.
  char buf[100];
  DWORD reDword; 
  CString m_csOutput,csTemp;
  BOOL res;
  do
  {
                  res=::ReadFile(rPipe,buf,100,&reDword,0);
                  csTemp=buf;
                  m_csOutput+=csTemp.Left(reDword);
  }while(res);
  return m_csOutput;
}

Hope this code will be useful for you.

License

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

Share

About the Author

Smith_TVPM
Software Developer
India India
Software Engineer,
Technopark, Kerala.

Rx 135 Owner
Yamaha Fan.

You may also be interested in...

Comments and Discussions

 
Questionsome time ReadFile not responding Pin
Le@rner15-Apr-13 1:53
memberLe@rner15-Apr-13 1:53 
GeneralMissing calls to CloseHandle, causes memory leak Pin
Member 15897532-May-11 4:42
memberMember 15897532-May-11 4:42 
GeneralI found another method, by searching in msdn.... Pin
Steppenwolf19-Jan-11 21:48
memberSteppenwolf19-Jan-11 21:48 
GeneralMy vote of 2 Pin
Steppenwolf19-Jan-11 21:43
memberSteppenwolf19-Jan-11 21:43 
QuestionAdd write functionality Pin
Marco Fiaschi2-Aug-10 8:07
memberMarco Fiaschi2-Aug-10 8:07 
GeneralProblem with buffering Pin
toschu7220-Nov-08 1:37
membertoschu7220-Nov-08 1:37 
GeneralParser 2 Pin
suhi22-Apr-08 3:37
membersuhi22-Apr-08 3:37 
GeneralParser Pin
suhi22-Apr-08 3:22
membersuhi22-Apr-08 3:22 
QuestionHow to receive numeric results Pin
smzhaq26-Sep-07 18:10
membersmzhaq26-Sep-07 18:10 
Dear. Thank you very much for sharing such usefull information. The code you gave recieves characters from console.

I have one question. I run 1 console program from my MFC application. The console program writes to console the results which are double precision floating values but are psesented to console as ASCII strings. Further if I attempt to read data before there is some data available, newline characters are received. Can you please help me how to receive the numeric results of that program into my MFC application?

Thank you very much.
AnswerRe: How to receive numeric results Pin
Evan Lin4-Sep-08 16:06
memberEvan Lin4-Sep-08 16:06 
QuestionPossible with MFC application Pin
kazim bhai30-Aug-07 2:00
memberkazim bhai30-Aug-07 2:00 
GeneralNice one. Thanks Pin
hungrytom27-Aug-07 23:59
memberhungrytom27-Aug-07 23:59 
QuestionGreat job, but seem not compatible with unicode ? Pin
Olivier Booklage22-Feb-07 9:57
memberOlivier Booklage22-Feb-07 9:57 
AnswerRe: Great job, but seem not compatible with unicode ? Pin
Olivier Booklage22-Feb-07 20:57
memberOlivier Booklage22-Feb-07 20:57 
GeneralGreat! Keep it up Pin
rm_pkt7-Feb-07 19:29
memberrm_pkt7-Feb-07 19:29 
Generalcool Pin
zoz18-Jan-07 10:44
memberzoz18-Jan-07 10:44 
GeneralCool n Nice implementation Pin
rassg17-Jan-07 18:36
memberrassg17-Jan-07 18:36 
QuestionCan we keep the output in the console as well? Pin
c.sunita3-Jan-07 7:58
memberc.sunita3-Jan-07 7:58 
GeneralCool! Pin
Steve_pqr14-Dec-06 9:00
memberSteve_pqr14-Dec-06 9:00 
Generalexcellent Pin
stephen(china)12-Oct-06 19:42
memberstephen(china)12-Oct-06 19:42 
GeneralExcellent article-just what I wanted Pin
dba3-Sep-06 23:16
memberdba3-Sep-06 23:16 
Generalsystem call Pin
himakiran25-Aug-06 11:42
memberhimakiran25-Aug-06 11:42 
QuestionProcess waiting answer Pin
RickyC10-Apr-06 1:42
memberRickyC10-Apr-06 1:42 
AnswerRe: Process waiting answer Pin
Smith_TVPM4-Sep-06 21:16
memberSmith_TVPM4-Sep-06 21:16 
GeneralRe: Process waiting answer Pin
12kaunas29-Mar-07 13:25
member12kaunas29-Mar-07 13:25 
GeneralJust the one i was looking for Pin
QuickDeveloper8-Apr-06 1:09
memberQuickDeveloper8-Apr-06 1:09 
GeneralRe: Just the one i was looking for Pin
Smith_TVPM8-Apr-06 2:04
memberSmith_TVPM8-Apr-06 2:04 
GeneralRe: Just the one i was looking for Pin
RahulOP21-Jun-06 20:14
memberRahulOP21-Jun-06 20:14 
GeneralRe: Just the one i was looking for Pin
ellolazo15-Aug-06 9:08
memberellolazo15-Aug-06 9:08 
GeneralSo Nice! Pin
Anonymous29-Jun-05 4:29
sussAnonymous29-Jun-05 4:29 
GeneralUseful for legacy codes. Pin
WREY18-Apr-05 8:20
memberWREY18-Apr-05 8:20 
GeneralRe: Useful for legacy codes. Pin
Muneeb-ur-Rehman Baig20-Feb-06 18:01
memberMuneeb-ur-Rehman Baig20-Feb-06 18:01 

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
Web03 | 2.8.150731.1 | Last Updated 16 Apr 2005
Article Copyright 2005 by Smith_TVPM
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid