ServerDlg.cpp
资源名称:进程通信vc.rar [点击查看]
上传用户:shljtb88
上传日期:2010-02-09
资源大小:82k
文件大小:5k
源码类别:
系统编程
开发平台:
Visual C++
- // ServerDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Server.h"
- #include "ServerDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CServerDlg dialog
- CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CServerDlg::IDD, pParent)
- , m_edit1(_T(""))
- {
- //{{AFX_DATA_INIT(CServerDlg)
- // 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 CServerDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDIT, m_edit1);
- //{{AFX_DATA_MAP(CServerDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
- //{{AFX_MSG_MAP(CServerDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON_StartClient, OnBUTTONStartClient)
- ON_BN_CLICKED(IDC_BUTTON_CloseClient, OnBUTTONCloseClient)
- ON_BN_CLICKED(IDC_BUTTON_SendMessage, OnBUTTONSendMessage)
- ON_BN_CLICKED(IDC_BUTTON_WriteToMemory, OnBUTTONWriteToMemory)
- ON_BN_CLICKED(IDC_BUTTON_WriteToMessagePipe, OnBUTTONWriteToMessagePipe)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CServerDlg message handlers
- BOOL CServerDlg::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 CServerDlg::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 CServerDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CServerDlg::OnBUTTONStartClient()
- {
- // TODO: Add your control notification handler code here
- PROCESS_INFORMATION pi;
- STARTUPINFO si;
- si.dwFlags=STARTF_USEPOSITION;
- si.dwX=300;
- si.dwY=500;
- memset(&si,0,sizeof(si));
- si.cb=sizeof(si);
- bool fRet=CreateProcess(NULL,
- "Client.exe",
- NULL,
- NULL,
- FALSE,
- NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
- NULL,
- NULL,
- &si,
- &pi);
- }
- void CServerDlg::OnBUTTONCloseClient()
- {
- CString str="Client";
- CWnd *pWnd=CWnd::FindWindow(NULL,str);
- if(pWnd){
- pWnd->SendMessage(WM_CLOSE,0,0);
- }
- }
- void CServerDlg::OnBUTTONSendMessage()
- {
- UpdateData(TRUE);
- CString str="Client";
- CWnd *pWnd=CWnd::FindWindow(NULL,str);
- if(pWnd)
- {
- COPYDATASTRUCT buf;
- char * s=new char[m_edit1.GetLength()]; //m_edit1为CString类型的变量
- s=m_edit1.GetBuffer(0);
- buf.cbData=strlen(s)+1;
- buf.lpData=s;
- pWnd->SendMessage(WM_COPYDATA,0,(LPARAM)&buf); //传送大量数据要用WM_COPYDATA消息
- }
- }
- void CServerDlg::OnBUTTONWriteToMemory()
- {
- 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;
- }
- UpdateData(TRUE);
- //给这段映像内存写数据
- sprintf(m_lpData,m_edit1);
- }
- void CServerDlg::OnBUTTONWriteToMessagePipe()
- {
- HANDLE PipeHandle;
- // 创建有名管道文件句柄
- UpdateData(TRUE);
- if ((PipeHandle = CreateFile("\\.\pipe\mypipe",
- GENERIC_READ | GENERIC_WRITE, 0,
- (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- (HANDLE) NULL)) == INVALID_HANDLE_VALUE)
- {
- AfxMessageBox("创建文件失败!");
- return;
- }
- else
- {
- DWORD dwWrite;
- char cSend[20];
- UpdateData(TRUE);
- int j;
- for(j=0;j<m_edit1.GetLength();j++)
- cSend[j] = m_edit1.GetAt(j);
- cSend[j] = ' ';
- for(int i=0;i<strlen(cSend)+1;i++)
- {
- WriteFile(PipeHandle,cSend+i,1,&dwWrite,NULL);
- }
- CloseHandle(PipeHandle);
- }
- CloseHandle(PipeHandle);
- }
- void CServerDlg::OnOK()
- {
- // TODO: Add extra validation here
- CDialog::OnOK();
- }
- void CServerDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- CDialog::OnCancel();
- }