ClientDlg.cpp
资源名称:进程通信vc.rar [点击查看]
上传用户:shljtb88
上传日期:2010-02-09
资源大小:82k
文件大小:4k
源码类别:
系统编程
开发平台:
Visual C++
- // ClientDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Client.h"
- #include "ClientDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CClientDlg dialog
- CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CClientDlg::IDD, pParent)
- , m_edit1(_T(""))
- {
- //{{AFX_DATA_INIT(CClientDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CClientDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDIT, m_edit1);
- //{{AFX_DATA_MAP(CClientDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
- //{{AFX_MSG_MAP(CClientDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON_ReadFromMemory, OnBUTTONReadFromMemory)
- ON_BN_CLICKED(IDC_BUTTON_CreatePipeAndReceive, OnBUTTONCreatePipeAndReceive)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CClientDlg message handlers
- BOOL CClientDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CClientDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CClientDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- BOOL CClientDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- CString str=(LPTSTR)pCopyDataStruct->lpData;
- m_edit1=str;
- UpdateData(FALSE);
- return CDialog::OnCopyData(pWnd, pCopyDataStruct);
- }
- void CClientDlg::OnBUTTONReadFromMemory()
- {
- HANDLE m_hMap;
- LPSTR m_lpData;
- m_hMap=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x200,"MYSHARE");
- if(m_hMap==NULL)
- {
- AfxMessageBox("CreateFileMapping() failed.");
- return;
- }
- //将文件的视图映射到一个进程的地址空间上,返回LPVOID类型的内存指针
- m_lpData=(LPSTR)MapViewOfFile(m_hMap,FILE_MAP_ALL_ACCESS,0,0,0);
- if(m_lpData==NULL)
- {
- AfxMessageBox("MapViewOfFile() failed.");
- return;
- }
- m_edit1=m_lpData;
- //m_list.InsertString(-1,m_msg1);
- UpdateData(FALSE);
- //释放映像内存
- UnmapViewOfFile(m_lpData);
- CloseHandle(m_hMap);
- }
- void CClientDlg::OnBUTTONCreatePipeAndReceive()
- {
- DWORD dwUDW = NMPWAIT_USE_DEFAULT_WAIT;
- HANDLE hServer = CreateNamedPipe("\\.\pipe\mypipe",PIPE_ACCESS_DUPLEX,PIPE_TYPE_BYTE,5,256,256,dwUDW,NULL);
- if( hServer == INVALID_HANDLE_VALUE )
- {
- AfxMessageBox("打开管道时发生错误!");
- }
- else
- {
- if (ConnectNamedPipe(hServer,NULL))
- {
- BYTE bRead;
- CString temp = _T("");
- char cRead[20];
- DWORD dwRead;
- int i = 0;
- while (ReadFile(hServer,&bRead,1,&dwRead,NULL))
- {
- cRead[i] = bRead;
- i++;
- }
- temp = cRead;
- m_edit1=temp;
- UpdateData(FALSE);
- }
- else
- {
- AfxMessageBox("没有连接!");
- }
- CloseHandle(hServer);
- }
- }
- void CClientDlg::OnOK()
- {
- // TODO: Add extra validation here
- CDialog::OnOK();
- }
- void CClientDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CDialog::OnCancel();
- }