工程1:新建一个对话框工程,并放一个按钮,按钮函数调用f(),f是把控制台程序输出内容输出到e:\\txalarm.txt中.
void f()
{
HANDLE hOutFile;
hOutFile=CreateFile("e:\\txalarm.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);SECURITY_ATTRIBUTES saPipe; /* security for anonymous pipe */
saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
saPipe.lpSecurityDescriptor = NULL;
saPipe.bInheritHandle = TRUE;HANDLE hReadPipe, hWritePipe,hWritePipe2;
BOOL bSuccess = CreatePipe(&hReadPipe, /* read handle */
&hWritePipe, /* write handle, used as stdout by child */
&saPipe, /* security descriptor */
0); /* pipe buffer size */bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hReadPipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
NULL, /* new handle - don't want one, change original handle */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
FALSE, /* make it *not* inheritable */
DUPLICATE_SAME_ACCESS);bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hWritePipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
&hWritePipe2, /* new handle, used as stderr by child */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
TRUE, /* it's inheritable */
DUPLICATE_SAME_ACCESS);PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.hStdInput=hReadPipe;
si.hStdOutput=hWritePipe;
si.hStdError=hWritePipe2;
si.dwFlags=STARTF_USESTDHANDLES;
si.cb=sizeof(si); ZeroMemory( &pi, sizeof(pi) );BOOL bRet=CreateProcess("E:\\console.exe",NULL,NULL,FALSE,TRUE,NULL,NULL,NULL,&si,&pi);
WaitForSingleObject(pi.hThread,INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);CloseHandle(hWritePipe);
CloseHandle(hWritePipe2);char chReadBuffer[256];
DWORD cchReadBuffer=sizeof(chReadBuffer);for (;;)
{
bSuccess = ReadFile(hReadPipe, /* read handle */
chReadBuffer, /* buffer for incoming data */
sizeof(chReadBuffer), /* number of bytes to read */
&cchReadBuffer, /* number of bytes actually read */
NULL); /* no overlapped reading */
if (!bSuccess && (GetLastError() == ERROR_BROKEN_PIPE))
break; /* child has died */if (bSuccess && cchReadBuffer)
{
/* write the data from the child to the file */
bSuccess = WriteFile(hOutFile, /* write handle */
chReadBuffer, /* buffer to write */
cchReadBuffer, /* number of bytes to write */
&cchReadBuffer, /* number of bytes actually written */
NULL); /* no overlapped writing */
}
}
CloseHandle(hOutFile);
CloseHandle(hReadPipe);
}//f//按钮函数
void CpipeDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
f();//调用控制台重定向输出.
}//----------------------
工程2:另外新建一个控制台程序 console.exe
// console.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0;i<100;i++)
puts("1111111111111111111111111111111111111"); return 0;
}
然后把console.exe放到e:, 然后运行工程1,可以在e:\\txalarm.txt下看到控制台输出的内容一致.
但现在碰到2个问题:
1.如果把 console.cpp中的for改为:for(int i=0;i<500;i++),那么运行工程1就会出现死机的状况.
2.这个可以实现打开控制台程序,但只有程序执行完毕后才能输入到文件,我想让他时时的向文件只输入请高手帮忙怎么样才能实现呀!!急呀!!老板在头上发火呢!
void f()
{
HANDLE hOutFile;
hOutFile=CreateFile("e:\\txalarm.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);SECURITY_ATTRIBUTES saPipe; /* security for anonymous pipe */
saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
saPipe.lpSecurityDescriptor = NULL;
saPipe.bInheritHandle = TRUE;HANDLE hReadPipe, hWritePipe,hWritePipe2;
BOOL bSuccess = CreatePipe(&hReadPipe, /* read handle */
&hWritePipe, /* write handle, used as stdout by child */
&saPipe, /* security descriptor */
0); /* pipe buffer size */bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hReadPipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
NULL, /* new handle - don't want one, change original handle */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
FALSE, /* make it *not* inheritable */
DUPLICATE_SAME_ACCESS);bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hWritePipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
&hWritePipe2, /* new handle, used as stderr by child */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
TRUE, /* it's inheritable */
DUPLICATE_SAME_ACCESS);PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.hStdInput=hReadPipe;
si.hStdOutput=hWritePipe;
si.hStdError=hWritePipe2;
si.dwFlags=STARTF_USESTDHANDLES;
si.cb=sizeof(si); ZeroMemory( &pi, sizeof(pi) );BOOL bRet=CreateProcess("E:\\console.exe",NULL,NULL,FALSE,TRUE,NULL,NULL,NULL,&si,&pi);
WaitForSingleObject(pi.hThread,INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);CloseHandle(hWritePipe);
CloseHandle(hWritePipe2);char chReadBuffer[256];
DWORD cchReadBuffer=sizeof(chReadBuffer);for (;;)
{
bSuccess = ReadFile(hReadPipe, /* read handle */
chReadBuffer, /* buffer for incoming data */
sizeof(chReadBuffer), /* number of bytes to read */
&cchReadBuffer, /* number of bytes actually read */
NULL); /* no overlapped reading */
if (!bSuccess && (GetLastError() == ERROR_BROKEN_PIPE))
break; /* child has died */if (bSuccess && cchReadBuffer)
{
/* write the data from the child to the file */
bSuccess = WriteFile(hOutFile, /* write handle */
chReadBuffer, /* buffer to write */
cchReadBuffer, /* number of bytes to write */
&cchReadBuffer, /* number of bytes actually written */
NULL); /* no overlapped writing */
}
}
CloseHandle(hOutFile);
CloseHandle(hReadPipe);
}//f//按钮函数
void CpipeDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
f();//调用控制台重定向输出.
}//----------------------
工程2:另外新建一个控制台程序 console.exe
// console.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0;i<100;i++)
puts("1111111111111111111111111111111111111"); return 0;
}
然后把console.exe放到e:, 然后运行工程1,可以在e:\\txalarm.txt下看到控制台输出的内容一致.
但现在碰到2个问题:
1.如果把 console.cpp中的for改为:for(int i=0;i<500;i++),那么运行工程1就会出现死机的状况.
2.这个可以实现打开控制台程序,但只有程序执行完毕后才能输入到文件,我想让他时时的向文件只输入请高手帮忙怎么样才能实现呀!!急呀!!老板在头上发火呢!
解决方案 »
- opengl 画出了三维模型怎么得到每个点的坐标
- csocket接收数据的问题
- 伪装IP访问的问题~~~新 husheng34(随意生活) 请进
- 想學VC,卻在一開始迷失了方向,請各位前輩支指點指點!
- 我安装了VC之后,浏览某些网站,会跳出对话框:脚本错误,是否需要调试
- 请教:关于FileMon.sys的DeviceIOControl(...FILEMON_setdrives...)问题
- 微软的弟兄们能不能解决这个问题?(VC6)
- MCF 中的六种数据结构怎麽用???????????????
- 有关BCB中DCOM编程中STUB/PROXY的问题 - 请大家讨嫩讨嫩!
- 推荐一个串口类(带有一个监视线程的)
- 请教WTL下如何给CStatic控件设置背景色?
- 视图窗口显示不出内容?
关于你说的第一点,我没明白.
我直接运行console 工程,是没啥问题的.控制台输出正常.哪里缓冲区异常了?