- UID
- 1029342
- 性别
- 男
|
消息处理函数:
//往匿名管道中写入数据按钮的消息处理函数
void CNoNamedPipeChildDlg::OnBnClickedBtnWritePipe()
{
//如果子进程还没有获得对匿名管道的读写句柄的话需要先获取句柄
this->GetReadWriteHandleFromParent();
ChildWritePipe();
}
//从匿名管道中读取数据按钮的消息处理函数
void CNoNamedPipeChildDlg::OnBnClickedBtnReadPipe()
{
//如果子进程还没有获得对匿名管道的读写句柄的话需要先获取句柄
this->GetReadWriteHandleFromParent();
ChildReadPipe();
}
//从匿名管道读取数据成员函数
void CNoNamedPipeChildDlg::ChildReadPipe(void)
{
DWORD dwRead;
char * pReadBuf;
CString cStrRecvData;
pReadBuf = new char[dataLength];
memset(pReadBuf, 0, dataLength);
//读取数据
if(!ReadFile(hPipeRead, pReadBuf, dataLength, &dwRead, NULL))
{
MessageBox(TEXT(" 从匿名管道接收数据失败 ..."),
TEXT("提示"), MB_ICONERROR);
return;
}
cStrRecvData = " 从匿名管道接收数据成功: ";
cStrRecvData += pReadBuf;
this->m_CStrReadPipe.Empty();
this->m_CStrReadPipe = pReadBuf;
UpdateData(FALSE);
MessageBox(cStrRecvData, TEXT("提示"), MB_ICONINFORMATION);
}
//往匿名管道中写入数据
void CNoNamedPipeChildDlg::ChildWritePipe(void)
{
UpdateData();
if(!this->m_CStrWritePipe.IsEmpty())
{
char * pSendData;
DWORD dwWrite;
CString cStrSendData;
//在这里需要将 Unicode 字符集转换为 ASCII 字符集
pSendData = new char[this->m_CStrWritePipe.GetLength() + 1];
memset(pSendData, 0, this->m_CStrWritePipe.GetLength() + 1);
for(int i=0;i<this->m_CStrWritePipe.GetLength();i++)
{
pSendData[i] = (char)this->m_CStrWritePipe.GetAt(i);
}
//写入数据
if(!WriteFile(hPipeWrite, pSendData,
this->m_CStrWritePipe.GetLength(), &dwWrite, NULL))
{
MessageBox(TEXT(" 给匿名管道发送数据失败 ..."),
TEXT("提示"), MB_ICONERROR);
return;
}
cStrSendData = "给匿名管道发送数据成功: ";
cStrSendData += this->m_CStrWritePipe;
this->m_CStrWritePipe.Empty();
UpdateData(FALSE);
MessageBox(cStrSendData, TEXT("提示"), MB_ICONINFORMATION);
}
else
{
MessageBox(TEXT(" 请先输入要发送给匿名管道的数据 ..."),
TEXT("提示"), MB_ICONERROR);
}
}
//需要获取继承自父进程的匿名管道读写句柄
void CNoNamedPipeChildDlg::GetReadWriteHandleFromParent(void)
{
if(this->m_IsGettedParentHandle == FALSE)
{
hPipeRead = GetStdHandle(STD_INPUT_HANDLE);
hPipeWrite = GetStdHandle(STD_OUTPUT_HANDLE);
this->m_IsGettedParentHandle = TRUE;
}
} |
|