|
왕초보 님이 쓰신 글 :
: 안녕하세요.. 다름이 아니라.. 실행파일들간에 문자열을 주고 받는 모듈을 만들려고 합니다..
: 유닉스 시스템에서는 Shared Memory나 IPC 등을 사용해서 했었는데여..
: 윈도에서는 어떻게 하는지를 잘 몰라서리... DDE를 사용해봤는데요.. 그게 잘 안되더라구요..
:
: 가령 예를 들어서 A라는 프로세스에서 B라는 프로세스에 어떤 문자열을 던져주고 B라는
: 프로세스에서는 A에서 넘겨준 문자열을 가지고 처리를 한 뒤에 그 결과를 다시 A에게 던져주는
: 그런 역할을 하는 것입니다...
:
: DDE를 사용할때는 A를 DDE Client로 B를 DDE Server로 두고 몇가지 방법으로 해 봤는데
: 연결은 되는데 DATA Poke 에러가 자꾸나서리... T.T
:
: 그리고 WM_COPYDATA 이벤트를 사용해서 다른 방법으로 구현을 해 보려고 하는데 그것도 좀
: 수월치가 않군요... DDE 를 사용하는 방법이라던지 아니면 다른 방법으로 할 수 있는 방법이
: 있으면 좀 가르쳐 주세요... 일단 DDE를 사용한 소스를 올려보겠습니다..
:
: // DDE Client
: //---------------------------------------------------------------------------
:
: #include <vcl.h>
: #pragma hdrstop
:
: #include "ddeclient.h"
: //---------------------------------------------------------------------------
: #pragma package(smart_init)
: #pragma resource "*.dfm"
: TForm1 *Form1;
: //---------------------------------------------------------------------------
: __fastcall TForm1::TForm1(TComponent* Owner)
: : TForm(Owner)
: {
: if( DdeClientConv1->SetLink( "DDEServer", "DDEServer" ) == false )
: ShowMessage( "DDE Link Error !" );
: DdeClientConv1->OpenLink();
: DdeClientItem1->DdeItem = "DdeServerItem1";
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::btn_sendClick(TObject *Sender)
: {
: if( DdeClientConv1->PokeData( DdeClientItem1->DdeItem, Edit1->Text.c_str() ) == false )
: ShowMessage( "DDE Poke Data Error" );
: }
: //---------------------------------------------------------------------------
: void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
: {
: if( DdeClientConv1->PokeData( DdeClientItem1->DdeItem, "Client End" ) == false )
: ShowMessage( "DDE Poke Data Error" );
: }
: //---------------------------------------------------------------------------
:
:
: // DDE Searver
: //---------------------------------------------------------------------------
:
: #include <vcl.h>
: #pragma hdrstop
:
: #include "ddeserver.h"
: //---------------------------------------------------------------------------
: #pragma package(smart_init)
: #pragma resource "*.dfm"
: TForm1 *Form1;
: //---------------------------------------------------------------------------
: __fastcall TForm1::TForm1(TComponent* Owner)
: : TForm(Owner)
: {
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::DdeServerItem1PokeData(TObject *Sender)
: {
: Memo1->Lines->Add( DdeServerItem1->Text );
: if( DdeServerItem1->Text == "Client End" ) Close();
: }
: //---------------------------------------------------------------------------
:
: 이상입니다.. 별다른건 없고 서버프로그램에는 메모 컴포넌트가 있고 클라이언트에는
: 에디트 박스가 있어서 클라이언트에서 에디트 박스에 내용을 넣고 Send 버튼을 누르면
: 서버에 있는 메모 컴포넌트에 보낸 내용이 찍히게 하려고 하는 것입니다...
:
: 도와주세요... 그럼 감사합니다.
우째우째.. 해결이 되었습니다... ㅡ.ㅡ;;
위의 소스는 클라이언트에서 서버로의 메세지 전송 밖에 되지 않는데... 그나마도 안되었었는데..
프로그램 둘이 양방향으로 메세지를 주고 받으려면 각 프로세스가 서버도 되고 클라이언트로도
되어야 할 것 같아서 어떻게 만들어 봤습니다..
아래에 소스를 올려놓습니다.. 혹시나 이렇게 하면 안된다는 분이 계시면 답변을 좀 주세요..
테스트를 해 보니까.. 별로 문제점은 발견이 되진 않는데.. 혹시나 안되는 부분이 있으면 안되니까..
그럼 모두들 즐프하세요...
// 프로세스 1
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ddeclient.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_sendClick(TObject *Sender)
{
if( DdeClientConv1->SetLink( "ddeserv", "ddeserv" ) == false )
ShowMessage( "DDE Link Error !" );
DdeClientConv1->OpenLink();
DdeClientItem1->DdeItem = "DdeServerItem1";
if( DdeClientConv1->PokeData( DdeClientItem1->DdeItem, Edit1->Text.c_str() ) == false )
ShowMessage( "DDE Poke Data Error" );
}
// 폼이 닫힐때 처리부분을 주석으로 한것은 Client가 종료되면 서버도 종료되게 프로그램을
// 만들었다가 그부분을 막아 놓은 것입니다.. client가 종료될때 서버가 종료되게 하려면
// client에서 "Client End"라는 메세지를 던지면 Server에서 받아다가 문자열을 비교한뒤
// "Client End"라는 메세지면 서버도 종료를 하면 될 것입니다.
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
// if( DdeClientConv1->PokeData( DdeClientItem1->DdeItem, "Client End" ) == false )
// ShowMessage( "DDE Poke Data Error" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DdeServerItem1Change(TObject *Sender)
{
Memo1->Lines->Add( DdeServerItem1->Text );
}
//---------------------------------------------------------------------------
// 프로세스 2
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ddeserver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DdeServerItem1Change(TObject *Sender)
{
Memo1->Lines->Add( DdeServerItem1->Text );
// if( DdeServerItem1->Text == "Client End" ) Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( DdeClientConv1->SetLink( "ddecli", "ddecli" ) == false )
ShowMessage( "DDE Link Error !" );
DdeClientConv1->OpenLink();
DdeClientItem1->DdeItem = "DdeServerItem1";
if( DdeClientConv1->PokeData( DdeClientItem1->DdeItem, Edit1->Text.c_str() ) == false )
ShowMessage( "DDE Poke Data Error" );
}
//---------------------------------------------------------------------------
프로세스 1이나 2나 똑같은 코드일 것입니다... 다만 DdeClientConv1->SetLink 하는데 있어서 조금
다를 뿐... 코드가 허접해서 죄송합니다.. 그럼 빠빠...
|