ServerDlg.cpp
上传用户:shljtb88
上传日期:2010-02-09
资源大小:82k
文件大小:5k
源码类别:

系统编程

开发平台:

Visual C++

  1. // ServerDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Server.h"
  5. #include "ServerDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CServerDlg dialog
  13. CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CServerDlg::IDD, pParent)
  15. , m_edit1(_T(""))
  16. {
  17. //{{AFX_DATA_INIT(CServerDlg)
  18. // NOTE: the ClassWizard will add member initialization here
  19. //}}AFX_DATA_INIT
  20. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  21. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  22. }
  23. void CServerDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. DDX_Text(pDX, IDC_EDIT, m_edit1);
  27. //{{AFX_DATA_MAP(CServerDlg)
  28. // NOTE: the ClassWizard will add DDX and DDV calls here
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
  32. //{{AFX_MSG_MAP(CServerDlg)
  33. ON_WM_PAINT()
  34. ON_WM_QUERYDRAGICON()
  35. ON_BN_CLICKED(IDC_BUTTON_StartClient, OnBUTTONStartClient)
  36. ON_BN_CLICKED(IDC_BUTTON_CloseClient, OnBUTTONCloseClient)
  37. ON_BN_CLICKED(IDC_BUTTON_SendMessage, OnBUTTONSendMessage)
  38. ON_BN_CLICKED(IDC_BUTTON_WriteToMemory, OnBUTTONWriteToMemory)
  39. ON_BN_CLICKED(IDC_BUTTON_WriteToMessagePipe, OnBUTTONWriteToMessagePipe)
  40. //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CServerDlg message handlers
  44. BOOL CServerDlg::OnInitDialog()
  45. {
  46. CDialog::OnInitDialog();
  47. // Set the icon for this dialog.  The framework does this automatically
  48. //  when the application's main window is not a dialog
  49. SetIcon(m_hIcon, TRUE); // Set big icon
  50. SetIcon(m_hIcon, FALSE); // Set small icon
  51. // TODO: Add extra initialization here
  52. return TRUE;  // return TRUE  unless you set the focus to a control
  53. }
  54. // If you add a minimize button to your dialog, you will need the code below
  55. //  to draw the icon.  For MFC applications using the document/view model,
  56. //  this is automatically done for you by the framework.
  57. void CServerDlg::OnPaint() 
  58. {
  59. if (IsIconic())
  60. {
  61. CPaintDC dc(this); // device context for painting
  62. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  63. // Center icon in client rectangle
  64. int cxIcon = GetSystemMetrics(SM_CXICON);
  65. int cyIcon = GetSystemMetrics(SM_CYICON);
  66. CRect rect;
  67. GetClientRect(&rect);
  68. int x = (rect.Width() - cxIcon + 1) / 2;
  69. int y = (rect.Height() - cyIcon + 1) / 2;
  70. // Draw the icon
  71. dc.DrawIcon(x, y, m_hIcon);
  72. }
  73. else
  74. {
  75. CDialog::OnPaint();
  76. }
  77. }
  78. // The system calls this to obtain the cursor to display while the user drags
  79. //  the minimized window.
  80. HCURSOR CServerDlg::OnQueryDragIcon()
  81. {
  82. return (HCURSOR) m_hIcon;
  83. }
  84. void CServerDlg::OnBUTTONStartClient() 
  85. {
  86. // TODO: Add your control notification handler code here
  87.     PROCESS_INFORMATION pi;
  88.     STARTUPINFO si;
  89. si.dwFlags=STARTF_USEPOSITION;
  90. si.dwX=300;
  91. si.dwY=500;
  92. memset(&si,0,sizeof(si));
  93.     si.cb=sizeof(si);
  94.     bool fRet=CreateProcess(NULL,
  95. "Client.exe",
  96. NULL,
  97. NULL,
  98. FALSE,
  99. NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE,
  100. NULL,
  101. NULL,
  102. &si,
  103. &pi);
  104. }
  105. void CServerDlg::OnBUTTONCloseClient() 
  106. {
  107.     CString str="Client";
  108. CWnd *pWnd=CWnd::FindWindow(NULL,str);
  109. if(pWnd){
  110.         pWnd->SendMessage(WM_CLOSE,0,0); 
  111. }
  112. }
  113. void CServerDlg::OnBUTTONSendMessage() 
  114. {
  115.     UpdateData(TRUE);
  116. CString str="Client";
  117. CWnd *pWnd=CWnd::FindWindow(NULL,str);
  118. if(pWnd)
  119. {
  120. COPYDATASTRUCT buf;
  121. char * s=new char[m_edit1.GetLength()];   //m_edit1为CString类型的变量
  122. s=m_edit1.GetBuffer(0);
  123. buf.cbData=strlen(s)+1;
  124. buf.lpData=s;
  125. pWnd->SendMessage(WM_COPYDATA,0,(LPARAM)&buf); //传送大量数据要用WM_COPYDATA消息
  126. }
  127. }
  128. void CServerDlg::OnBUTTONWriteToMemory() 
  129. {
  130. HANDLE m_hMap;   
  131. LPSTR  m_lpData;   
  132. m_hMap=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x200,"MYSHARE");   
  133. if(m_hMap==NULL)   
  134. {   
  135. AfxMessageBox("CreateFileMapping() failed.");
  136. return;
  137. }
  138. //将文件的视图映射到一个进程的地址空间上,返回LPVOID类型的内存指针
  139. m_lpData=(LPSTR)MapViewOfFile(m_hMap,FILE_MAP_ALL_ACCESS,0,0,0);   
  140. if(m_lpData==NULL)   
  141. {   
  142. AfxMessageBox("MapViewOfFile() failed.");
  143. return;
  144. }
  145.     UpdateData(TRUE);
  146. //给这段映像内存写数据
  147. sprintf(m_lpData,m_edit1);   
  148. }
  149. void CServerDlg::OnBUTTONWriteToMessagePipe() 
  150. {
  151.     HANDLE PipeHandle;
  152.    // 创建有名管道文件句柄
  153.     UpdateData(TRUE);
  154. if ((PipeHandle = CreateFile("\\.\pipe\mypipe",
  155. GENERIC_READ | GENERIC_WRITE, 0,
  156. (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING,
  157. FILE_ATTRIBUTE_NORMAL,
  158. (HANDLE) NULL)) == INVALID_HANDLE_VALUE)
  159. {
  160. AfxMessageBox("创建文件失败!");
  161. return;
  162. }
  163. else
  164. {
  165. DWORD dwWrite;
  166. char cSend[20];
  167. UpdateData(TRUE);
  168. int j;
  169. for(j=0;j<m_edit1.GetLength();j++)
  170. cSend[j] = m_edit1.GetAt(j);
  171. cSend[j] = '';
  172. for(int i=0;i<strlen(cSend)+1;i++)
  173. {
  174. WriteFile(PipeHandle,cSend+i,1,&dwWrite,NULL);
  175. }
  176. CloseHandle(PipeHandle);
  177. }
  178. CloseHandle(PipeHandle);
  179. }
  180. void CServerDlg::OnOK() 
  181. {
  182. // TODO: Add extra validation here
  183. CDialog::OnOK();
  184. }
  185. void CServerDlg::OnCancel() 
  186. {
  187. // TODO: Add extra cleanup here
  188. CDialog::OnCancel();
  189. }