欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

两个窗口程序(进程)之间的通信

程序员文章站 2022-09-16 09:28:14
两个进程或者说两个窗口程序之间如何交换信息呢?Windows提供了一个消息机制WM_COPYDATA,可以在两个窗口之间交换数据。WM_COPYDATA消息在两个窗口程序之间传递一个预定义的数据结构COPYDATASTRUCT。typedef struct tagCOPYDATASTRUCT{ DW ......

   两个进程或者说两个窗口程序之间如何交换信息呢?Windows提供了一个消息机制WM_COPYDATA,可以在两个窗口之间交换数据。WM_COPYDATA消息在两个窗口程序之间传递一个预定义的数据结构COPYDATASTRUCT。
typedef struct tagCOPYDATASTRUCT
{
  DWORD dwData;
  DWORD cbData;
  PVOID lpData;
} COPYDATASTRUCT;
dwData为32位的自定义数据,lpData为指向数据的指针,cbData为lpData指针指向数据内存的大小(字节数)。

  发送WM_COPYDATA消息只能用SendMessage而不能用PostMessage,原因是WM_COPYDATA发送的数据是以内存映射文件形式发送的,用PostMessage后内存映射文件的句柄会失效了,造成数据丢失。

发送方窗口程序(进程)的代码

class TForm1 : public TForm
{
        __published:    // IDE-managed Components
            TEdit *edt1;
            TButton *btn1;
            void __fastcall btn1Click(TObject *Sender);
       private:            // User declarations
        public:        // User declarations
            __fastcall TForm1(TComponent* Owner);
            char *cpSendStr,cSendStr[512];
            COPYDATASTRUCT pCopyData;
};
//-----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
       cpSendStr = cSendStr;
       cpSendStr = "Losng";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn1Click(TObject *Sender)
{
         HANDLE fm;
         fm = FindWindow(NULL,"Recemess");
         if (fm != NULL)
             {
                 pCopyData.dwData = 0;
                 if(edt1->Text != "") cpSendStr = edt1->Text.c_str() ;
                 pCopyData.lpData = cpSendStr ;
                 pCopyData.cbData = strlen(cpSendStr);
                 SendMessage(fm,WM_COPYDATA,0,LPARAM)&pCopyData);
             }
         else ShowMessage("没有找到接收窗口!");
}
//-----------------------------------------------------------------------
                        

 

接收窗口程序(进程)代码

class TForm1 : public TForm
{
  __published:    // IDE-managed Components
        TEdit *edt1;
  private:          // User declarations
  public:           // User declarations

       void __fastcall OnWMCopyData(TMessage &Msg);
      BEGIN_MESSAGE_MAP
          VCL_MESSAGE_HANDLER(WM_COPYDATA,TMessage,OnWMCopyData);
      END_MESSAGE_MAP(TForm)
      __fastcall TForm1(TComponent* Owner);

};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   this->Caption = "Recemess";
}
//---------------------------------------------------------------------------
 void __fastcall TForm1::OnWMCopyData(TMessage &Msg)
 {
   char *cpSendStr,cSendStr[512];
   cpSendStr = cSendStr;
   bool flag = false ;
   if ( Msg.Msg == WM_COPYDATA )
     {
        COPYDATASTRUCT* pCopyData = (COPYDATASTRUCT* )Msg.LParam ;
        cpSendStr = (char *)pCopyData->lpData;
        edt1->Text = cpSendStr ;
        flag = true ;
     }
   if (!flag)
     {
        TForm::WndProc(Msg);
     }
 }