16,548
社区成员




void CNamedPipeSrvView::OnPipeCreate()
{
//创建命名管道
m_hPipe=CreateNamedPipe(_T("\\\\.\\pipe\\mypipe")
,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE,2,1024,1024,0,NULL);
if(INVALID_HANDLE_VALUE==m_hPipe)
{
MessageBox(_T("创建命名管道失败!"));
m_hPipe=NULL;
return;
}
HANDLE hEvent;
hEvent=CreateEvent(NULL,true,false,NULL);
if(!hEvent)
{
MessageBox(_T("创建事件对象失败"));
CloseHandle(m_hPipe);
m_hPipe=NULL;
return;
}
OVERLAPPED ovd;
ZeroMemory(&ovd,sizeof(OVERLAPPED));
ovd.hEvent=hEvent;
if(!ConnectNamedPipe(m_hPipe,&ovd))
{
if(ERROR_IO_PENDING!=GetLastError())
{
MessageBox(_T("等待客户端连接失败!"));
CloseHandle(m_hPipe);
m_hPipe=NULL;
CloseHandle(hEvent);
return;
}
}
if(WAIT_FAILED ==WaitForSingleObject(hEvent, INFINITE))
{
MessageBox(_T("等待对象失败!"));
CloseHandle(m_hPipe);
m_hPipe=NULL;
CloseHandle(hEvent);
return;
}
CloseHandle(hEvent);
}
void CNamedPipeSrvView::OnPipeRead()
{
// 读管道
wchar_t ch[100];
DWORD dwRead;
if(!ReadFile(m_hPipe,ch,100,&dwRead,NULL))
{
MessageBox(_T("读取数据失败!"));
return;
}
MessageBox(ch);
}
void CNamedPipeSrvView::OnPipeWrite()
{
// 写管道
wchar_t ch[]=_T("http://users9.jabry.com/pj7892002");
DWORD dwWrite;
if(!WriteFile(m_hPipe,ch,(wcslen(ch)+1)*2,&dwWrite,NULL))
{
MessageBox(_T("写入数据失败!"));
return;
}
}
void CNamedPipeClientView::OnPipeConnect()
{
// 连接管道
/*if(!WaitNamedPipe(_T("\\\\ServerName\\pipe\\mypipe"),
NMPWAIT_WAIT_FOREVER))*/ //异地测试失败
if(!WaitNamedPipe(_T("\\\\.\\pipe\\mypipe"),
NMPWAIT_WAIT_FOREVER)) //本地测试成功
{
MessageBox(_T("当前没有可用的命名管道实例!"));
return;
}
m_hPipe=CreateFile(_T("\\\\AdminPj-PC\\pipe\\mypipe"),
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE==m_hPipe)
{
MessageBox(_T("打开命名管道失败!"));
m_hPipe=NULL;
return;
}
}
void CNamedPipeClientView::OnPipeRead()
{
// 读管道
wchar_t ch[100];
DWORD dwRead;
if(!ReadFile(m_hPipe,ch,100,&dwRead,NULL))
{
MessageBox(_T("读取数据失败!"));
return;
}
MessageBox(ch);
}
void CNamedPipeClientView::OnPipeWrite()
{
// 写管道
wchar_t ch[]=_T("命名管道测试程序!");
DWORD dwWrite;
if(!WriteFile(m_hPipe,ch,(wcslen(ch)+1)*2,&dwWrite,NULL))
{
MessageBox(_T("写入数据失败!"));
return;
}
}