![]() |
Languages »
C / C++ Language »
Howto
Intermediate
License: The Code Project Open License (CPOL)
Execute a Console Application From VC++By Smith_TVPMTo 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
|
|
Advanced Search |
|
|
|
||||||||||||||||
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:
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.
General
News
Question
Answer
Joke
Rant
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 Web11 | Advertise on the Code Project |