資源描述:
《vc之線程和進(jìn)程之用命名管道實(shí)現(xiàn)進(jìn)程間通信》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、一、創(chuàng)建應(yīng)用工程???????1、創(chuàng)建單文檔應(yīng)用工程????????2、添加新單文檔應(yīng)用工程二、編輯菜單項(xiàng)???????1、編輯Thread工程之菜單項(xiàng)???????2、編輯Child工程之菜單項(xiàng)三、添加變量、函數(shù)???????1、添加變量?????????????a、為CThreadView類添加變量?????????????b、為CChildView類添加變量???????2、添加函數(shù)?????????????a、為CThreadView類添加菜單消息響應(yīng)函數(shù)?????????????b、為CChildView類添
2、加菜單消息響應(yīng)函數(shù)四、添加代碼???????1、為CThreadView類添加代碼?????????????a、添加初始化代碼CThreadView::CThreadView(){?//TODO:addconstructioncodehere???pipe=NULL;}CThreadView::~CThreadView(){?if(pipe)?CloseHandle(pipe);}?????????????b、添加消息響應(yīng)函數(shù)代碼voidCThreadView::OnPipeCreate(){?//TODO:Addyo
3、urcommandhandlercodehere?pipe=CreateNamedPipe("\\.\pipe\mypipe",PIPE_ACCESS_DUPLEX
4、FILE_FLAG_OVERLAPPED,0,1,1024,1024,0,NULL);?if(INVALID_HANDLE_VALUE==pipe)?{??MessageBox("管道創(chuàng)建失敗");??pipe=NULL;??return;?}?HANDLEevent;?event=CreateEvent(NULL,TRUE,FALSE,NULL)
5、;?if(!event)?{??MessageBox("事件對(duì)象創(chuàng)建失敗");??CloseHandle(pipe);??pipe=NULL;??return;?}?OVERLAPPEDoverlap;?ZeroMemory(&overlap,sizeof(OVERLAPPED));?overlap.hEvent=event;?ConnectNamedPipe(pipe,&overlap);???????????????????????????????????//等待客戶端連接???WaitForSingleObjec
6、t(event,INFINITE);?}voidCThreadView::OnPipeRead(){?//TODO:Addyourcommandhandlercodehere??charreadbuf[100];?DWORDdwread;?ReadFile(pipe,readbuf,100,&dwread,NULL);??MessageBox(readbuf);?}voidCThreadView::OnPipeWrite(){?//TODO:Addyourcommandhandlercodehere??charbuf[
7、]="你好,長(zhǎng)江!";?DWORDdwwrite;?WriteFile(pipe,buf,strlen(buf)+1,&dwwrite,NULL);?}???????2、為CChildView類添加代碼?????????????a、添加初始化代碼CChildView::CChildView(){?//TODO:addconstructioncodehere???pipe=NULL;}CChildView::~CChildView(){?if(pipe)??CloseHandle(pipe);}?????????????
8、b、添加消息響應(yīng)函數(shù)代碼voidCChildView::OnConnect(){?//TODO:Addyourcommandhandlercodehere?WaitNamedPipe("\\.\pipe\mypipe",NMPWAIT_WAIT_FOREVER);?pipe=CreateFile"\\.\pipe\mypipe",GENERIC_READ
9、GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);?}voidCChild
10、View::OnRead(){?//TODO:Addyourcommandhandlercodehere??charreadbuf[100];?DWORDdwread;?ReadFile(pipe,readbuf,100,&dwread,NULL);??MessageBox(readbuf);?}voidCChildView::O