进程A中重要函数 void CProcessADlg::OnStartB() //创建进程B { // TODO: Add your control notification handler code here PROCESS_INFORMATION pi; STARTUPINFO si; //初始化变量 memset(&si,0,sizeof(si)); si.cb=sizeof(si); si.wShowWindow=SW_SHOW; si.dwFlags=STARTF_USESHOWWINDOW; //打开记事本程序 BOOL fRet=CreateProcess(NULL, "ProcessB.exe", NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); if(!fRet) {//创建失败,显示错误信息 LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); AfxMessageBox( (LPCTSTR)lpMsgBuf); LocalFree( lpMsgBuf ); } else { //AfxMessageBox("CreateProcess成功"); m_hPro[i++]=pi.hProcess; } } void CProcessADlg::OnStopB() { // TODO: Add your control notification handler code here //判断进程句柄是否合法 if(m_hPro[i-1]) { //根据句柄,终止刚才打开的记事本程序 if(!TerminateProcess(m_hPro[i-1],0)) { //终止出现错误,显示错误信息 LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); AfxMessageBox( (LPCTSTR)lpMsgBuf); LocalFree( lpMsgBuf ); } else { //AfxMessageBox("TerminateProcess成功"); } m_hPro[i---1]=NULL; } else { AfxMessageBox("进程B未由A直接创建!"); } } void CProcessADlg::OnSendMessage() { // TODO: Add your control notification handler code here //找到接收消息的窗口(窗口名为Receiver) CString str="ProcessB"; CWnd *pWnd=CWnd::FindWindow(NULL,str); CString m_Msg1; GetDlgItem(IDC_EDIT1)->GetWindowText(m_Msg1); if(pWnd) { COPYDATASTRUCT buf; char * s=new char[m_Msg1.GetLength()]; //m_Msg1为CString类型的变量 s=m_Msg1.GetBuffer(0); buf.cbData=strlen(s)+1; buf.lpData=s; pWnd->SendMessage(WM_COPYDATA,0,(LPARAM)&buf); //传送大量数据要用WM_COPYDATA消息 } } void CProcessADlg::OnSendToMemory() //共享内存通信 { // TODO: Add your control notification handler code here //创建内存映像对象 HANDLE hMapping; LPSTR lpData; hMapping=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x200,"MYSHARE"); if(hMapping==NULL) { AfxMessageBox("CreateFileMapping() failed."); return; } //将文件的视图映射到一个进程的地址空间上,返回LPVOID类型的内存指针 lpData=(LPSTR)MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0); if(lpData==NULL) { AfxMessageBox("MapViewOfFile() failed."); return; } //给这段映像内存写数据 CString m_Msg1; GetDlgItem(IDC_EDIT1)->GetWindowText(m_Msg1); sprintf(lpData,m_Msg1); //释放映像内存 UnmapViewOfFile(lpData); } void CProcessADlg::OnButton5()//实现管道通信,此为客户端 { // TODO: Add your control notification handler code here HANDLE PipeHandle; DWORD BytesWritten; CHAR Buffer[256]; GetDlgItem(IDC_EDIT1)->GetWindowText(Buffer,256); if (WaitNamedPipe(".//pipe//mynamedpipe", NMPWAIT_WAIT_FOREVER) == 0) { AfxMessageBox("WaitNamedPipe failed"); } // Create the named pipe file handle //总是这里出错????? if ((PipeHandle = CreateFile(".//pipe//mynamedpipe", GENERIC_READ | GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL)) == INVALID_HANDLE_VALUE) { AfxMessageBox("CreateFile failed"); } if (WriteFile(PipeHandle,Buffer,sizeof(Buffer), &BytesWritten, NULL) == 0) { AfxMessageBox("WriteFile failed"); CloseHandle(PipeHandle); } //printf("Wrote %d bytes", BytesWritten); CloseHandle(PipeHandle); } 进程B中重要函数 BOOL CProcessBDlg::OnCopyData(CWnd* pWnd,COPYDATASTRUCT* pCopyDataStruct) //接收消息通信 { //AfxMessageBox((LPCSTR)(pCopyDataStruct->lpData));/*利用对话框表示收到消息*/ GetDlgItem(IDC_EDIT1)->SetWindowText((LPCSTR)(pCopyDataStruct->lpData)); return CWnd::OnCopyData(pWnd,pCopyDataStruct); } void CProcessBDlg::OnGetMemoryMessage() //从内存读取信息 { // TODO: Add your control notification handler code here //See if memory-mapped file named MYSHARE already exists HANDLE hFileMapT=OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE,"MYSHARE"); if (hFileMapT!=NULL) { //The MYSHARE does exist,map it into the process's address space PVOID pView=MapViewOfFile(hFileMapT,FILE_MAP_READ | FILE_MAP_WRITE,0,0,0); if (pView!=NULL) { //Put the contents of the MYSHARE into the output area GetDlgItem(IDC_EDIT1)->SetWindowText((PTSTR)pView); //释放映像内存 UnmapViewOfFile(pView); } else { AfxMessageBox("MapViewOfFile() failed."); } } else { AfxMessageBox("CreateFileMapping() failed."); } } void CProcessBDlg::OnCreatePipe() //创建管道 { // TODO: Add your control notification handler code here HANDLE PipeHandle; DWORD BytesRead; DWORD BytesWritten; CHAR Buffer[256]; CString str; BOOL fSuccess; if ((PipeHandle = CreateNamedPipe(".//pipe//mynamedpipe", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, PIPE_UNLIMITED_INSTANCES, 1024, 1024, 2000, NULL)) == INVALID_HANDLE_VALUE) { AfxMessageBox("CreateNamedPipe failed"); } // Serve client connections forever while(1) { if (ConnectNamedPipe(PipeHandle,NULL) ==0) { AfxMessageBox("ConnectNamedPipe failed"); break; } // Read data from and echo data to the client until // the client is ready to stop if(ReadFile(PipeHandle, Buffer,sizeof(Buffer), &BytesRead, NULL) > 0) { //AfxMessageBox("Echo %d bytes to client"); //AfxMessageBox("the thread%d"); /*if (WriteFile(PipeHandle, Buffer, BytesRead, &BytesWritten, NULL) == 0) { AfxMessageBox("WriteFile failed"); break; }*/ str=Buffer; //AfxMessageBox(str); } //AfxMessageBox(str); GetDlgItem(IDC_EDIT1)->SetWindowText(str); break; if (DisconnectNamedPipe(PipeHandle) == 0) { AfxMessageBox("DisconnectNamedPipe failed"); break; } } CloseHandle(PipeHandle); }