動作環境
C++ Builder XE4
処理概要
- 2つのウィンドウ間で文字列を送受信する
- Form1: 送信側
- Form2: 受信側
参考
-
650_他のアプリへデータ ( 文字列や画像 ) の送信 ( Delphi 同士 ) by Mr.XRAY様
- Delphi実装
-
Daten mit WM_COPYDATA zwischen Prozessen austauschen
- C++ Builder実装
情報感謝です。
Herzlichen Dank!
実装
送信側 > Form1
Unit1.h
//---------------------------------------------------------------------------
# ifndef Unit1H
# define Unit1H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormShow(TObject *Sender);
private: // ユーザー宣言
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
# endif
Unit1.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit1.h"
# include "Unit2.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HWND hChild;
hChild = FindWindow(NULL, L"Form2");
if (hChild == NULL) {
return;
}
TCopyDataStruct cds;
char sndtxt[] = "Hello,World";
cds.dwData = 1234; // 適当な値
cds.cbData = (strlen(sndtxt) + 1) * sizeof(char);
cds.lpData = &sndtxt;
SendMessage(hChild, WM_COPYDATA, 0, (LPARAM)&cds);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
Form2->Show();
}
//---------------------------------------------------------------------------
受信側 > Form2
Unit2.h
//---------------------------------------------------------------------------
# ifndef Unit2H
# define Unit2H
//---------------------------------------------------------------------------
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.StdCtrls.hpp>
# include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE で管理されるコンポーネント
TMemo *Memo1;
private: // ユーザー宣言
public: // ユーザー宣言
void __fastcall WM_RecvString(TWMCopyData &cds);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_COPYDATA, TWMCopyData, WM_RecvString);
END_MESSAGE_MAP(TForm)
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
# endif
Unit2.cpp
//---------------------------------------------------------------------------
# include <vcl.h>
# pragma hdrstop
# include "Unit2.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::WM_RecvString(TWMCopyData &cds)
{
char *ptxt = (char *)(cds.CopyDataStruct->lpData);
Memo1->Lines->Add(String(ptxt));
}