Click here to Skip to main content
6,595,854 members and growing! (19,591 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Howto     Intermediate License: The Code Project Open License (CPOL)

Execute a Console Application From VC++

By Smith_TVPM

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.
VC6, Windows, Visual Studio, Dev
Posted:16 Apr 2005
Views:63,113
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
38 votes for this article.
Popularity: 5.79 Rating: 3.66 out of 5
5 votes, 13.2%
1
2 votes, 5.3%
2
2 votes, 5.3%
3
8 votes, 21.1%
4
21 votes, 55.3%
5

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)

About the Author

Smith_TVPM


Member
Software Engineer,
Technopark, Kerala.

Rx 135 Owner
Yamaha Fan.
Occupation: Software Developer
Location: India India

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralProblem with buffering Pinmembertoschu722:37 20 Nov '08  
GeneralParser 2 Pinmembersuhi4:37 22 Apr '08  
GeneralParser Pinmembersuhi4:22 22 Apr '08  
GeneralHow to receive numeric results Pinmembersmzhaq19:10 26 Sep '07  
GeneralRe: How to receive numeric results PinmemberEvan Lin17:06 4 Sep '08  
QuestionPossible with MFC application Pinmemberkazim bhai3:00 30 Aug '07  
GeneralNice one. Thanks Pinmemberhungrytom0:59 28 Aug '07  
GeneralGreat job, but seem not compatible with unicode ? PinmemberOlivier Booklage10:57 22 Feb '07  
GeneralRe: Great job, but seem not compatible with unicode ? PinmemberOlivier Booklage21:57 22 Feb '07  
GeneralGreat! Keep it up Pinmemberrm_pkt20:29 7 Feb '07  
Generalcool Pinmemberzoz11:44 18 Jan '07  
GeneralCool n Nice implementation Pinmemberrassg19:36 17 Jan '07  
QuestionCan we keep the output in the console as well? Pinmemberc.sunita8:58 3 Jan '07  
GeneralCool! PinmemberSteve_pqr10:00 14 Dec '06  
Generalexcellent Pinmemberstephen(china)20:42 12 Oct '06  
GeneralExcellent article-just what I wanted Pinmemberdba0:16 4 Sep '06  
Generalsystem call Pinmemberhimakiran12:42 25 Aug '06  
QuestionProcess waiting answer PinmemberRickyC2:42 10 Apr '06  
AnswerRe: Process waiting answer PinmemberSmith_TVPM22:16 4 Sep '06  
GeneralRe: Process waiting answer Pinmember12kaunas14:25 29 Mar '07  
GeneralJust the one i was looking for PinmemberQuickDeveloper2:09 8 Apr '06  
GeneralRe: Just the one i was looking for PinmemberSmith_TVPM3:04 8 Apr '06  
GeneralRe: Just the one i was looking for PinmemberRahulOP21:14 21 Jun '06  
GeneralRe: Just the one i was looking for Pinmemberellolazo10:08 15 Aug '06  
GeneralSo Nice! PinsussAnonymous5:29 29 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Apr 2005
Editor: Sumalatha K.R.
Copyright 2005 by Smith_TVPM
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project