进程间通信方式.doc_第1页
进程间通信方式.doc_第2页
进程间通信方式.doc_第3页
进程间通信方式.doc_第4页
进程间通信方式.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一 利用剪贴板进行进程间通信void CClipboardDlg:OnBtnSend() / TODO: Add your control notification handler code hereif(OpenClipboard() /打开剪贴板 EmptyClipboard(); /释放剪贴板中数据的句柄并分配剪贴板所有权给当前窗口 CString str; HANDLE hClip; char *pBuf; GetDlgItemText(IDC_EDIT_SEND,str); hClip=GlobalAlloc(GMEM_MOVEABLE,str.GetLength()+1); /从堆中分配指定字节的内存 pBuf=(char*)GlobalLock(hClip); /对全局内存对象加锁,并将句柄转换为指针 strcpy(pBuf,str); GlobalUnlock(hClip); /解锁 SetClipboardData(CF_TEXT,hClip); /以指定的格式在剪贴板上放置数据 CloseClipboard(); /关闭剪贴板void CClipboardDlg:OnBtnRecv() / TODO: Add your control notification handler code hereif(OpenClipboard() if(IsClipboardFormatAvailable(CF_TEXT) /检测剪贴板是否包含指定格式的数据 HANDLE hClip; hClip=GetClipboardData(CF_TEXT); /从剪贴板中获取指定格式的数据 char *pBuf; pBuf=(char*)GlobalLock(hClip); /将句柄转换为地址 GlobalUnlock(hClip); SetDlgItemText(IDC_EDIT_RECV,pBuf); CloseClipboard(); 二 利用匿名管道进行进程间通信(匿名管道只能在父子进程间通信)父进程void CParentView:OnPipeCreate() / TODO: Add your command handler code hereSECURITY_ATTRIBUTES sa;sa.bInheritHandle=TRUE;sa.lpSecurityDescriptor=NULL;sa.nLength=sizeof(SECURITY_ATTRIBUTES);if(!CreatePipe(&hRead,&hWrite,&sa,0) /创建一个匿名管道并返回管道的读写句柄 MessageBox(创建匿名管道失败!); return;STARTUPINFO sui;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_INFORMATION pi;if(!CreateProcess(.ChildDebugChild.exe,NULL,NULL,NULL, TRUE,0,NULL,NULL,&sui,&pi)CloseHandle(hRead); CloseHandle(hWrite); hRead=NULL; hWrite=NULL; MessageBox(创建子进程失败!); return;else CloseHandle(pi.hProcess); CloseHandle(pi.hThread);void CParentView:OnPipeRead() / TODO: Add your command handler code herechar buf100;DWORD dwRead;if(!ReadFile(hRead,buf,100,&dwRead,NULL) MessageBox(读取数据失败!); return;MessageBox(buf);void CParentView:OnPipeWrite() / TODO: Add your command handler code herechar buf=武汉科技大学黄家湖校区;DWORD dwWrite;if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL) MessageBox(写入数据失败!); return;子进程void CChildView:OnInitialUpdate() CView:OnInitialUpdate();/ TODO: Add your specialized code here and/or call the base classhRead=GetStdHandle(STD_INPUT_HANDLE);hWrite=GetStdHandle(STD_OUTPUT_HANDLE);void CChildView:OnPipeRead() / TODO: Add your command handler code herechar buf100;DWORD dwRead;if(!ReadFile(hRead,buf,100,&dwRead,NULL) MessageBox(读取数据失败!); return;MessageBox(buf);void CChildView:OnPipeWrite() / TODO: Add your command handler code herechar buf=匿名管道测试程序;DWORD dwWrite;if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL) MessageBox(写入数据失败!); return;三 利用命名管道进行进程间通信服务端进程void CNamedPipeSrvView:OnPipeCreate() / TODO: Add your command handler code herehPipe=CreateNamedPipe(.pipeMyPipe, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, 0,1,1024,1024,0,NULL); /创建命名管道的实例并返回句柄if(INVALID_HANDLE_VALUE=hPipe) MessageBox(创建命名管道失败!); hPipe=NULL; return;HANDLE hEvent;hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);if(!hEvent) MessageBox(创建事件对象失败!); CloseHandle(hPipe); hPipe=NULL; return;OVERLAPPED ovlap;ZeroMemory(&ovlap,sizeof(OVERLAPPED);ovlap.hEvent=hEvent;if(!ConnectNamedPipe(hPipe,&ovlap) /命名管道服务端进程等待客户端连接请求的到来(客户端进程连接到命名管道的实例) if(ERROR_IO_PENDING!=GetLastError() MessageBox(等待客户端连接失败!); CloseHandle(hPipe); CloseHandle(hEvent); hPipe=NULL; return; if(WAIT_FAILED=WaitForSingleObject(hEvent,INFINITE) /等待事件对象变为有信号状态 MessageBox(等待对象失败!); CloseHandle(hPipe); CloseHandle(hEvent); hPipe=NULL; return;CloseHandle(hEvent);void CNamedPipeSrvView:OnPipeRead() / TODO: Add your command handler code herechar buf100;DWORD dwRead;if(!ReadFile(hPipe,buf,100,&dwRead,NULL) MessageBox(读取数据失败!); return;MessageBox(buf);void CNamedPipeSrvView:OnPipeWrite() / TODO: Add your command handler code herechar buf=武汉科技大学黄家湖校区;DWORD dwWrite;if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL) MessageBox(写入数据失败!); return;客户端进程void CNamedPipeCltView:OnPipeConnect() / TODO: Add your command handler code hereif(!WaitNamedPipe(.pipeMyPipe,NMPWAIT_WAIT_FOREVER) /等待命名管道的实例可用 MessageBox(当前没有可利用的命名管道实例!); return;hPipe=CreateFile(.pipeMyPipe,GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); /打开命名管道if(INVALID_HANDLE_VALUE=hPipe) MessageBox(打开命名管道失败!); hPipe=NULL; return;void CNamedPipeCltView:OnPipeRead() / TODO: Add your command handler code herechar buf100;DWORD dwRead;if(!ReadFile(hPipe,buf,100,&dwRead,NULL) MessageBox(读取数据失败!); return;MessageBox(buf);void CNamedPipeCltView:OnPipeWrite() / TODO: Add your command handler code herechar buf=命名管道测试程序;DWORD dwWrite;if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL) MessageBox(写入数据失败!); return;四 利用邮槽进行进程间通信服务端进程void CMailslotSrvView:OnMailslotRecv() / TODO: Add your command handler code hereHANDLE hMailslot;hMailslot=CreateMailslot(.mailslotMyMailslot,0, MAILSLOT_WAIT_FOREVER,NULL); /创建邮槽if(INVALID_HANDLE_VALUE=hMailslot) MessageBox(创建邮槽失败!); return;char buf100;DWORD dwRead;if(!ReadFile(hMailslot,buf,100,&dwRead,NULL) /邮槽的服务端只能接收数据 MessageBox(读取数据失败!); CloseHandle(hMailslot); return;MessageBox(buf);CloseHandle(hMailslot);客户端进程void CMailslotCltView:OnMailslotSend() / TODO: Add your command handler code hereHANDLE hMailslot;hMailslot=CreateFile(.mailslotMyMailslot,GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); /打开邮槽if(INVALID_HANDLE_VAL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论