資源描述:
《VC++之線程和進(jìn)程之用郵槽實(shí)現(xiàn)進(jìn)程間通信》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、VC++之線程和進(jìn)程之用郵槽實(shí)現(xiàn)進(jìn)程間通信2011年01月21日星期五下午3:04一、創(chuàng)建應(yīng)用工程???????1、創(chuàng)建單文檔應(yīng)用程序工程????????2、添加新單文檔應(yīng)用程序工程二、編輯菜單項(xiàng)???????1、編輯Thread工程之菜單項(xiàng)???????2、編輯Child工程之菜單項(xiàng)三、添加消息響應(yīng)函數(shù)?????????????a、為CThreadView類添加菜單消息響應(yīng)函數(shù)?????????????b、為CChildView類添加菜單消息響應(yīng)函數(shù)四、添加代碼???????1、為CThreadView類添加消息響應(yīng)函數(shù)代碼voidCThreadView::OnReceive()
2、{?//TODO:Addyourcommandhandlercodehere?HANDLEmailslot;?mailslot=CreateMailslot("\\.\mailslot\mymailslot",0,MAILSLOT_WAIT_FOREVER,NULL);????//創(chuàng)建郵槽?if(INVALID_HANDLE_VALUE==mailslot)?{??MessageBox("Createfail");??CloseHandle(mailslot);??return;?}?charbuf[100];?DWORDdwread;?ReadFile(mailslot,
3、buf,100,&dwread,NULL);??????????????????????????????????????????????????????????????????????????????????????//讀文件?MessageBox(buf);?CloseHandle(mailslot);?}???????2、為CChildView類添加消息響應(yīng)函數(shù)代碼voidCChildView::OnSend(){?//TODO:Addyourcommandhandlercodehere?HANDLEmailslot;?mailslot=CreateFile("\\.\m
4、ailslot\mymailslot",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);?if(INVALID_HANDLE_VALUE==mailslot)?{??MessageBox("Openfail");??CloseHandle(mailslot);??return;?}?charbuf[]="你好,長江!";?DWORDdwwrite;?WriteFile(mailslot,buf,strlen(buf)+1,&dwwrite,NULL);?CloseHandle
5、(mailslot);?}五、編譯六、運(yùn)行七、函數(shù)說明???????1、CreateMailslot()函數(shù)聲明HANDLECreateMailslot(LPCTSTRlpName,DWORDnMaxMessageSize,DWORDlReadTimeOut,LPSECURITY_ATTRIBUTESlpSecurityAttributes)????????????功能:創(chuàng)建一個(gè)郵槽。函數(shù)調(diào)用成功,返回郵槽句柄;否則,返回INVALID_HANDLE_VALUE。???????2、CreateFile()函數(shù)聲明HANDLE?CreateFile(LPCTSTRlpFileName
6、,DWORDdwDwsiredAccess,DWORDdwShareMode,LPSECURITY_ATTRIBUTESlpSecurityAttributes,DWORDdwCreationDispostion,DWORDdwFlagsAndAttributes,HANDLEhTemplateFile?)????????????功能:創(chuàng)建或打開一個(gè)文件、通信資源、磁盤驅(qū)動(dòng)器等。???????3、ReadFile()函數(shù)聲明BOOL?ReadFile(HANDLEhFile,LPVOIDlpBuffer,DWORDnNumberOfBytesRead,LPDWORDlpNumber
7、OfBytesRead,LPOVERLAPPEDlpOverlapped)????????????功能:從一個(gè)文件中文件指針位置開始讀取數(shù)據(jù)。成功,返回非零值;失敗,返回零。???????4、WriteFile()函數(shù)聲明BOOL?WriteFile(HANDLEhFile,LPVOIDlpBuffer,DWORDnNumberOfBytesToWrite,LPDWORDlpNumberOfBytesWritten,LPOVERLAPPEDlpOverlapped)?