資源描述:
《VC++之線程和進(jìn)程之用共享內(nèi)存實(shí)現(xiàn)進(jìn)程間通信.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、一、創(chuàng)建對(duì)話框應(yīng)用程序二、編輯對(duì)話框資源控件ID及標(biāo)題IDC_STATIC?????????????????????????????發(fā)送數(shù)據(jù)IDC_EDIT_SENDIDC_BUTTON_SEND?????????????發(fā)送IDC_STATIC????????????????????????????接收數(shù)據(jù)IDC_EDIT_RECEIVEIDC_BUTTON_RECEIVE???????接收IDOK??????????????????????????????????????????退出?三、添加變量、函數(shù)、對(duì)象/庫(kù)模塊???????1、添加變量public:?CEdit?m_send;?
2、CEdit?m_receive;public:?HANDLESharedMemory;?void*?address;??????2、添加消息響應(yīng)函數(shù)四、添加代碼???????1、添加初始化代碼BOOLCThreadDlg::OnInitDialog(){?CDialog::OnInitDialog();......?SharedMemory=CreateFileMapping((HANDLE)0xffffffff,NULL,PAGE_READWRITE,0,10000,"MemFile");?address=MapViewOfFile(SharedMemory,FILE_MAP_WRITE
3、,0,0,0);??returnTRUE;?}???????2、添加函數(shù)代碼voidCThreadDlg::OnButtonReceive(){?//TODO:Addyourcontrolnotificationhandlercodehere?CStringstr;??strcpy((char*)(LPCTSTR)str,(char*)address);?m_receive.SetWindowText(str);?}voidCThreadDlg::OnButtonSend(){?//TODO:Addyourcontrolnotificationhandlercodehere?CString
4、str;?m_send.GetWindowText(str);?strcpy((char*)address,(char*)(LPCTSTR)str);?}五、編譯六、運(yùn)行七、函數(shù)說(shuō)明???????1、CreateFileMapping()函數(shù)聲明HANDLECreateFileMapping(HANDLEhFile,LPSECURITY_ATTRIBUTElpFileMappingAttributes,DWORDflProtect,DWORDdwMaximumSizeHigh,DWORDdwMaximumSizeLow,LPCTSTRlpName)??????????????功能:創(chuàng)建一個(gè)
5、內(nèi)存映射對(duì)象。成功,返回文件映射對(duì)象句柄;失敗,返回NULL。???????2、MapViewOfFile()函數(shù)聲明HANDLE?MapViewOfFile(HANDLEhFileMappingObject,DWORDdwDesiredAccess,DWORDdwFileOffsetHigh,DWORDdwFileOffsetLow,SIZE_TdwNumberOfByteToMap)??????????????功能:將內(nèi)存映射對(duì)象映射到進(jìn)程地址控件。成功,返回映射對(duì)象的起始地址;失敗,返回NULL。???????3、strcpy()函數(shù)聲明char*strcpy(char*strDes
6、tination,constchar*strSource)??????????????功能:將源字符串內(nèi)容復(fù)制到目的字符串。