NamedPipe通信

void CNamePipeSrvView::OnPipeCreate()
{ hPipe=CreateNamedPipe(" \\\\.\\pipe\\MyPipe",
  PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
  0,1,1024,1024,0,NULL);
 if(INVALID_HANDLE_VALUE==hPipe)
 {
  MessageBox("create namepipe fail");
  hPipe=NULL;
  return;
 }
 HANDLE hEvent;
 hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
 if(NULL==hEvent)
 {
  MessageBox("create event fail");
  CloseHandle(hPipe);
  hPipe=NULL;
  return;
 }
 OVERLAPPED ovlap;
 ZeroMemory(&ovlap,sizeof(OVERLAPPED));
 ovlap.hEvent=hEvent;
 if(!ConnectNamedPipe(hPipe,&ovlap))
 {
  if(ERROR_IO_PENDING !=GetLastError())
  {
   MessageBox("waitfor client connect erro");
   CloseHandle(hPipe);
   CloseHandle(hEvent);
   hPipe=NULL;
   return;
  }
 }
 if(WAIT_FAILED==WaitForSingleObject(hEvent,INFINITE))
 {
  MessageBox("waitfor object erro");
  CloseHandle(hPipe);
  CloseHandle(hEvent);
  hPipe=NULL;
  return;
 }
 CloseHandle(hEvent);.
void CNamePipeCltView::OnPipeConnect()
{
 // TODO: Add your command handler code here
 if(!WaitNamedPipe(" \\\\.\\pipe\\MyPipe",NMPWAIT_WAIT_FOREVER))
 {
  MessageBox("have no pipe is available ");
  return;
 }
 hPipe=CreateFie(" \\\\.\\pipe\\MyPipe",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
 if(INVALID_HANDLE_VALUE==hPipe)
 {
  MessageBox("CreateFile fail");
  hPipe=NULL;
  return;
 }
 

void CNamePipeSrvView::OnPipeRead()
{
 // TODO: Add your command handler code here
 char buf[100];
 DWORD dwRead;
 if(!ReadFile(hPipe,buf,100,&dwRead,NULL))
 {
  MessageBox("read data fail");
  return;
 }
 MessageBox(buf);
 
}
void CNamePipeSrvView::OnPipeWrite()
{
 // TODO: Add your command handler code here
 char buf[]=" [url]http://www.sunxin.com[/url]";
 DWORD dwWrite;
 if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL))
 {
  MessageBox("write data fail");
  return;
 }
 
}

你可能感兴趣的:(职场,休闲,NamedPipe)