预览
正文
下面我们来编写我们的子进程的代码:
第一步:
我们先在我们资源里面添加我们相应的菜单项
第二步:
我们对这些菜单项建立消息响应函数(在我们的view类中)
第三步:
我们来获取我们子进程的标准输入和标准输出句柄(我们通过建立一个虚函数OnInitialUpdate())——这个虚函数是我们的窗口创建之后第一个调用的函数
第四步:
同样的我们先来建立两个句柄,一个是读取的,一个是写入的,并在我们的构造函数中进行初始化,在我们的析构函数中我们去关闭这两个句柄
CChildView::CChildView()
{
// TODO: add construction code here
hread=NULL;
hwrite=NULL;
}
CChildView::~CChildView()
{
if (hread)
{
CloseHandle(hread);
}
if (hwrite)
{
CloseHandle(hwrite);
}
}
第五步:
在我们的虚函数OnInitialUpdate()中去得到我们的管道的句柄(读取和写入句柄)
void CChildView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
hread=GetStdHandle(STD_INPUT_HANDLE);
hwrite=GetStdHandle(STD_OUTPUT_HANDLE);
}
第六步:我们来编写我们的读取和写入程序
void CChildView::OnPipeRead()
{
// TODO: Add your command handler code here
char buf[100];
DWORD dwread;
if(!ReadFile(hread,buf,100,&dwread,NULL))
{
MessageBox("读取数据失败!");
return;
}
MessageBox(buf);
}
void CChildView::OnPipeWrite()
{
// TODO: Add your command handler code here
char buf[]="990561853";
DWORD dwwrite;
if (!WriteFile(hwrite,buf,strlen(buf)+1,&dwwrite,NULL))
{
MessageBox("写入数据失败!");
return;
}
}