我想在 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。