資源描述:
《進程間通信方式》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、一利用剪貼板進行進程間通信voidCClipboardDlg::OnBtnSend(){//TODO:Addyourcontrolnotificationhandlercodehereif(OpenClipboard())???//打開剪貼板{??EmptyClipboard();??//釋放剪貼板中數(shù)據(jù)的句柄并分配剪貼板所有權(quán)給當(dāng)前窗口??CStringstr;??HANDLEhClip;??char*pBuf;??GetDlgItemText(IDC_EDIT_SEND,str);??hClip=GlobalAlloc(GMEM_MOVEABLE,str.GetL
2、ength()+1);??//從堆中分配指定字節(jié)的內(nèi)存??pBuf=(char*)GlobalLock(hClip);???//對全局內(nèi)存對象加鎖,并將句柄轉(zhuǎn)換為指針??strcpy(pBuf,str);??GlobalUnlock(hClip);?????//解鎖??SetClipboardData(CF_TEXT,hClip);??//以指定的格式在剪貼板上放置數(shù)據(jù)??CloseClipboard();??????//關(guān)閉剪貼板}}voidCClipboardDlg::OnBtnRecv(){//TODO:Addyourcontrolnotificationhan
3、dlercodehereif(OpenClipboard()){??if(IsClipboardFormatAvailable(CF_TEXT))??//檢測剪貼板是否包含指定格式的數(shù)據(jù)??{???HANDLEhClip;???hClip=GetClipboardData(CF_TEXT);??//從剪貼板中獲取指定格式的數(shù)據(jù)???char*pBuf;???pBuf=(char*)GlobalLock(hClip);???//將句柄轉(zhuǎn)換為地址???GlobalUnlock(hClip);???SetDlgItemText(IDC_EDIT_RECV,pBuf);???
4、CloseClipboard();??}}}二利用匿名管道進行進程間通信(匿名管道只能在父子進程間通信)父進程voidCParentView::OnPipeCreate(){//TODO:AddyourcommandhandlercodehereSECURITY_ATTRIBUTESsa;sa.bInheritHandle=TRUE;sa.lpSecurityDescriptor=NULL;sa.nLength=sizeof(SECURITY_ATTRIBUTES);if(!CreatePipe(&hRead,&hWrite,&sa,0))??//創(chuàng)建一個匿名管道并返
5、回管道的讀寫句柄{??MessageBox("創(chuàng)建匿名管道失敗!");??return;}STARTUPINFOsui;ZeroMemory(&sui,sizeof(STARTUPINFO));sui.cb=sizeof(STARTUPINFO);sui.dwFlags=STARTF_USESTDHANDLES;sui.hStdInput=hRead;sui.hStdOutput=hWrite;sui.hStdError=GetStdHandle(STD_ERROR_HANDLE);PROCESS_INFORMATIONpi;if(!CreateProcess("..
6、\Child\Debug\Child.exe",NULL,NULL,NULL,??TRUE,0,NULL,NULL,&sui,&pi)){??CloseHandle(hRead);??CloseHandle(hWrite);??hRead=NULL;??hWrite=NULL;??MessageBox("創(chuàng)建子進程失??!");??return;}else{??CloseHandle(pi.hProcess);??CloseHandle(pi.hThread);}}voidCParentView::OnPipeRead(){//TODO:Addyourcomman
7、dhandlercodeherecharbuf[100];DWORDdwRead;if(!ReadFile(hRead,buf,100,&dwRead,NULL)){??MessageBox("讀取數(shù)據(jù)失??!");??return;}MessageBox(buf);}voidCParentView::OnPipeWrite(){//TODO:Addyourcommandhandlercodeherecharbuf[]="武漢科技大學(xué)黃家湖校區(qū)";DWORDdwWrite;if(!WriteFile(hWrite,buf,strlen(buf)+1,&