以下思路不是最佳方案,仅供参考:
控制台程序支持重定向到文件,然后读取文件内容到编辑框即可。
假设你的程序名为xxx,编辑一个批处理文件n.bat,内容为:xxx > 1.log_unlink("c:\\1.log");
ShellExecute(m_hWnd, "open", "c:\\n.bat", "", "c:\\", SW_SHOW);// 判断控制台程序执行结束char buff[10240];
CFile f;
if ( f.Open( "c:\\1.log", CFile::modeRead) )
{
UINT uBytes = f.GetLength();
f.Read((void*)buff, uBytes);
f.Close();
buff[uBytes] = 0;
m_edit.SetWindowText(buff);
}
需要解决的问题有:识别程序结束,自动关闭DOS框后再读取文件内容。
控制台程序支持重定向到文件,然后读取文件内容到编辑框即可。
假设你的程序名为xxx,编辑一个批处理文件n.bat,内容为:xxx > 1.log_unlink("c:\\1.log");
ShellExecute(m_hWnd, "open", "c:\\n.bat", "", "c:\\", SW_SHOW);// 判断控制台程序执行结束char buff[10240];
CFile f;
if ( f.Open( "c:\\1.log", CFile::modeRead) )
{
UINT uBytes = f.GetLength();
f.Read((void*)buff, uBytes);
f.Close();
buff[uBytes] = 0;
m_edit.SetWindowText(buff);
}
需要解决的问题有:识别程序结束,自动关闭DOS框后再读取文件内容。
都是用CreateProcess()将StdHandle替换掉!
<待续>
自己创建:
CreatePipe(hRead, hWrite, NULL, 0);
将hRead, hWrite在CreateProcess的调用参数中传给子进程!
<待续>
//声明两个句柄:HANDLE hConnectRead,
hConnectWrite;//创建用来传递信息的管道;
CreatePipe(&hConnectRead, &hConnectWrite, NULL, 0);
//使read的句柄不可继承;
DuplicateHandle(GetCurrentProcess(),
hConnectRead, GetCurrentProcess(),
NULL, 0, FALSE,
DUPLICATE_SAME_ACCESS));
//启动read的进程!
DWORD dwThreadID;CreateThread(NULL, 0, ReadProc, (LPVOID)(&hConnectRead), 0, &dwThreadID) //或其他启动的方式;//启动进程;
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcessInfo;
memset( &StartInfo, 0, sizeof(STARTUPINFO) );
StartInfo.cb = sizeof(STARTUPINFO);
StartInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
StartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
StartInfo.hStdOutput = hConnectWrite;
StartInfo.hStdError = hConnectWrite;
StartInfo.wShowWindow = SW_HIDE;
StartInfo.lpReserved = NULL;
StartInfo.cbReserved2 = 0;
StartInfo.lpReserved2 = NULL;CreateProcess(szProcess.GetBuffer(szProcess.GetLength()), szCommandLine.GetBuffer(szCommandLine.GetLength()), NULL,
NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL,
&StartInfo, &ProcessInfo));// 等待;
while(WaitForSingleObject(hProcess, 0) == WAIT_TIMEOUT)
{ //不用INFINITE是为了能够随时终止;
...
//判断外部的终止条件,或其他需要在进程运行时需要监测的工作!
}/////////////////////////////////////////////
// ReadProc
{
DWORD dwReadCount;
HANDLE hRead = (HANDLE)(*(HANDLE*)lpvoidPara);
while(CanContinue()) //CanContinue()只是示意,具体如何判断,根据自己的情况!
{
FlushFileBuffers(hRead);
ReadFile(hRead, lpOutputCache, CACHE_SIZE, &dwReadCount, NULL);
//将lpOutputCache中的内容保存起来;
AddToYourDataBuffer(lpOutputCache);//自己的函数;
}
}实际的运行中还需要一个临界区的控制将可能冲突的变量保护起来!