1

我想在 MFC 中开发一个应用程序,它可以启动一个冗长的控制台进程并在 32 位 Windows 应用程序的文本区域上同时提供其输出。

我使用了管道,但它仅在进程终止后才显示输出。我试过 _popen,它适用于基于控制台的应用程序,但与 win32 应用程序不兼容。

在搜索互联网时,我发现了许多使用 CLR 的代码,但我需要在 MFC 中使用一些方法,而不使用 .Net .. 请帮助。

提前致谢 :-)

这是我启动应用程序的代码:

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:我在 x86 windows 7 上使用 Visual Studio 2010。我正在将此代码与 winPE 集成,因此强烈需要 MFC。

4

1 回答 1

0

问题似乎是读取管道的循环阻塞了应用程序的 UI 主线程,因此在循环完成之前不会更新对话框。

您可以采取一些措施来解决此问题,但最简单的方法是在调用 DisplayToTextArea 后将消息处理循环添加到您的 do-while 循环中:

 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0)) 
 {
    TranslateMessage(&msg);
    DispatchMessage(&msg);  // send to window proc
 } 
于 2012-02-28T11:17:30.320 回答