zoukankan      html  css  js  c++  java
  • 进程间通信:匿名管道

    // ParentView.cpp : implementation of the CParentView class
    //
    
    #include "stdafx.h"
    #include "Parent.h"
    
    #include "ParentDoc.h"
    #include "ParentView.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView
    
    IMPLEMENT_DYNCREATE(CParentView, CView)
    
    BEGIN_MESSAGE_MAP(CParentView, CView)
    	//{{AFX_MSG_MAP(CParentView)
    	ON_COMMAND(IDM_PIPE_CREATE, OnPipeCreate)
    	ON_COMMAND(IDM_PIPE_READ, OnPipeRead)
    	ON_COMMAND(IDM_PIPE_WRITE, OnPipeWrite)
    	//}}AFX_MSG_MAP
    	// Standard printing commands
    	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView construction/destruction
    
    CParentView::CParentView()
    {
    	hRead = NULL;
    	hWrite = NULL;
    }
    
    CParentView::~CParentView()
    {
    	if(hRead) CloseHandle(hRead);
    	if(hWrite)CloseHandle(hWrite);
    }
    
    BOOL CParentView::PreCreateWindow(CREATESTRUCT& cs)
    {
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	return CView::PreCreateWindow(cs);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView drawing
    
    void CParentView::OnDraw(CDC* pDC)
    {
    	CParentDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	// TODO: add draw code for native data here
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView printing
    
    BOOL CParentView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    	// default preparation
    	return DoPreparePrinting(pInfo);
    }
    
    void CParentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add extra initialization before printing
    }
    
    void CParentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add cleanup after printing
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView diagnostics
    
    #ifdef _DEBUG
    void CParentView::AssertValid() const
    {
    	CView::AssertValid();
    }
    
    void CParentView::Dump(CDumpContext& dc) const
    {
    	CView::Dump(dc);
    }
    
    CParentDoc* CParentView::GetDocument() // non-debug version is inline
    {
    	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CParentDoc)));
    	return (CParentDoc*)m_pDocument;
    }
    #endif //_DEBUG
    
    /////////////////////////////////////////////////////////////////////////////
    // CParentView message handlers
    
    void CParentView::OnPipeCreate() 
    {
    	SECURITY_ATTRIBUTES sa;
    	sa.bInheritHandle=TRUE;
    	sa.lpSecurityDescriptor=NULL;
    	sa.nLength=sizeof(SECURITY_ATTRIBUTES);
    
    	if(!CreatePipe(&hRead,&hWrite,&sa,0)){
    		MessageBox("创建管道失败");
    		return;
    	}
    	STARTUPINFO sui;
    	PROCESS_INFORMATION pi;
    	ZeroMemory(&sui,sizeof(STARTUPINFO));
    	sui.cb = sizeof(STARTUPINFO);
    	sui.dwFlags = STARTF_USESTDHANDLES;
    	sui.hStdInput = hRead;
    	sui.hStdOutput = hWrite;
    	sui.hStdError = GetStdHandle(STD_ERROR_HANDLE);
    
    	/*if(!CreateProcess("..\\Child\\Debug\\Child.exe",NULL,NULL,NULL,TRUE,0,NULL,NULL,&sui,&pi))*/
    	if(!CreateProcess("C:\\Workspace\\VC6\\ProgMfcExercise\\Parent\\Child\\Debug\\Child.exe"
    		,NULL,NULL,NULL,TRUE,0,NULL,NULL,&sui,&pi))
    	{
    
    		CloseHandle(hRead);
    		CloseHandle(hWrite);
    		hRead = NULL;
    		hWrite = NULL;
    		DWORD code = GetLastError();
    		MessageBox("创建子进程失败");
    		return;			 
    	}
    	else{
    		CloseHandle(pi.hProcess);
    		CloseHandle(pi.hThread);
    	}
    }
    
    void CParentView::OnPipeRead() 
    {
    	char buf[100];
    	DWORD dwRead;
    	if(!ReadFile(hRead,buf,100,&dwRead,NULL)){
    		MessageBox("读取数据失败");
    		return;
    	}
    	MessageBox(buf);
    }
    
    void CParentView::OnPipeWrite() 
    {
    
    	char buf[]="http://www.baidu.com/";
    	DWORD dwWrite;
    	if(!WriteFile(hWrite,buf,strlen(buf)+1,&dwWrite,NULL)){
    		DWORD code = GetLastError();
    		CString str;
    		str.Format("code,%d,写入数据失败",code);
    		MessageBox(str);
    		return ;
    	}
    }
    

      

    // ChildView.cpp : implementation of the CChildView class
    //
    
    #include "stdafx.h"
    #include "Child.h"
    
    #include "ChildDoc.h"
    #include "ChildView.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView
    
    IMPLEMENT_DYNCREATE(CChildView, CView)
    
    BEGIN_MESSAGE_MAP(CChildView, CView)
    	//{{AFX_MSG_MAP(CChildView)
    	ON_COMMAND(IDM_PIPE_READ, OnPipeRead)
    	ON_COMMAND(IDM_PIPE_WRITE, OnPipeWrite)
    	//}}AFX_MSG_MAP
    	// Standard printing commands
    	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView construction/destruction
    
    CChildView::CChildView()
    {
    	hRead = NULL;
    	hWrite = NULL;
    }
    
    CChildView::~CChildView()
    {
    	if(hRead) CloseHandle(hRead);
    	if(hWrite) CloseHandle(hWrite);
    }
    
    BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
    {
    	// TODO: Modify the Window class or styles here by modifying
    	//  the CREATESTRUCT cs
    
    	return CView::PreCreateWindow(cs);
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView drawing
    
    void CChildView::OnDraw(CDC* pDC)
    {
    	CChildDoc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	// TODO: add draw code for native data here
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView printing
    
    BOOL CChildView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    	// default preparation
    	return DoPreparePrinting(pInfo);
    }
    
    void CChildView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add extra initialization before printing
    }
    
    void CChildView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    	// TODO: add cleanup after printing
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView diagnostics
    
    #ifdef _DEBUG
    void CChildView::AssertValid() const
    {
    	CView::AssertValid();
    }
    
    void CChildView::Dump(CDumpContext& dc) const
    {
    	CView::Dump(dc);
    }
    
    CChildDoc* CChildView::GetDocument() // non-debug version is inline
    {
    	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChildDoc)));
    	return (CChildDoc*)m_pDocument;
    }
    #endif //_DEBUG
    
    /////////////////////////////////////////////////////////////////////////////
    // CChildView message handlers
    
    void CChildView::OnPipeRead() 
    {
    	char buf[100];
    	DWORD dwRead;
    	if(!ReadFile(hRead,buf,100,&dwRead,NULL))
    	{
    		MessageBox("读取数据失败");
    		return;
    	}
    	MessageBox(buf);
    }
    
    void CChildView::OnPipeWrite() 
    {
    	// TODO: Add your command handler code here
    	char 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 class
    	this->hRead = GetStdHandle(STD_INPUT_HANDLE);
    	this->hWrite = GetStdHandle(STD_OUTPUT_HANDLE);
    }
    

      

  • 相关阅读:
    腾讯视频插入网页的代码;
    FW: 软件持续交付的诉求;
    TOGAF
    Windows WSL2 htop打开黑屏的问题解决
    requests.exceptions.ConnectionError: HTTPSConnectionPool(host='appts.xxx.com%20', port=443):
    sqlalchemy实现模糊查询
    jenkins过滤版本,可选择版本
    QML 布局之一:锚布局详解(各种例子)
    Qt Quick 常用控件:Button(按钮)用法及自定义
    The Common Order Operations of Dis Operation System (DOSS)
  • 原文地址:https://www.cnblogs.com/wucg/p/2419957.html
Copyright © 2011-2022 走看看