1

I want to develop an application in MFC which could start a lengthy console process and give its output concurrently on a text area of 32bit windows application.

I used pipes, but it display the output only after the process has terminated. I tried _popen, it works for console based application, but not compatible with win32 application.

while searching internet, I found numerous code using CLR, but I need some way in MFC, without use of .Net.. Plz help.

THANKS in Advance :-)

Here's my code that start the application:

void CAppMgr_BackupsDlg::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);
            DisplayToTextArea(m_csOutput);
}
while(res);
}

PS: I am using Visual studio 2010 on x86 windows 7. I am making this code to be integrated with winPE, therefore strongly need MFC.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Konark
  • 31
  • 2

1 Answers1

0

The problem seems the loop to read the pipe is blocking the UI main thread of your application so that your dialog is not updated until the loop finished.

There are some things you can do to solve this problem but the easiest way would be to add a message processing loop to your do-while loop after calling DisplayToTextArea:

 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0)) 
 {
    TranslateMessage(&msg);
    DispatchMessage(&msg);  // send to window proc
 } 
Javier De Pedro
  • 2,219
  • 4
  • 32
  • 49
  • thanks for reply! but I used the above code. again the output of the process is displayed after its termination.. Need to get a simultaneous output. – Konark Feb 28 '12 at 11:56
  • I agree with kids_fox that a worker thread would be the right apporach. But I think that a message processing loop should make the trick. Maybe you should use PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) instead of GetMessage...it's been a long time. But it may be other problems...are you sure the pipe is getting the data by chunks? – Javier De Pedro Feb 28 '12 at 14:02
  • Thanks @JavierDePedro for reply, but I used a separate worker thread to read the rpipe. I used the window message and handler, but the output is still the same. output is displayed only when the background process terminates. plz help. – Konark Mar 05 '12 at 11:46