copydata-窗口间消息

来源:互联网 发布:网络教育好考吗 编辑:程序博客网 时间:2024/05/16 05:53

::::PART1::::

BOOL CPart1Dlg::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
 
 CenterWindow(GetDesktopWindow()); // center to the hpc screen
 CString str;
 mymsg = RegisterWindowMessage(str);
 // TODO: Add extra initialization here
 
 return TRUE;  // return TRUE  unless you set the focus to a control
}

 

void CPart1Dlg::OnButtonTransfer()
{
 // TODO: Add your control notification handler code here
 
 //CreateProcess(L"//part2.exe",text,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,NULL,&proinfo);
}

void CPart1Dlg::OnButton2()
{
 HWND hwnd = ::FindWindow(NULL,L"part2");//正在执行文件的窗口名字
 COPYDATASTRUCT copydata;
 copydata.cbData = 20;
 copydata.dwData = 0;
 copydata.lpData = L"//123.txt";
 if (hwnd)
 {
  ::SendMessage(hwnd,WM_COPYDATA,0,(LPARAM)&copydata);
 }
 else
 {
  MessageBox(L"No Find");
 }
}

 

::::PART2::::

BOOL CPart2Dlg::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
 
 CenterWindow(GetDesktopWindow()); // center to the hpc screen

 CString str;
 mymsg = RegisterWindowMessage(str);

 // TODO: Add extra initialization here
 //GetCmdLine = GetCommandLine();
 //m_editval = GetCmdLine;
 return TRUE;  // return TRUE  unless you set the focus to a control
}

 


LRESULT CPart2Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 // TODO: Add your specialized code here and/or call the base class
 if (message == WM_COPYDATA)
 {
  COPYDATASTRUCT* receivedata = (COPYDATASTRUCT*) lParam;
  TCHAR* buf = (TCHAR*)receivedata->lpData;
  MessageBox(buf);

  char str[20];
  memset(str,0,20);
  CString text;
  CFile file;
  if(file.Open(buf,CFile::modeRead))
  {
   while (file.Read(str,19)>0)
   {
    text += str;
    memset(str,0,20);
   }
  }
  else
  {
   return 0;
  }
  m_editval = text;
  UpdateData(FALSE);
  file.Close();
 }
 return CDialog::WindowProc(message, wParam, lParam);
}

原创粉丝点击