Article View: borland.public.cppbuilder.nativeapi
Article #8516Re: Communication with Shared Memory
From: Paolo Borlandi
Date: Wed, 20 Oct 2004 10:40
Date: Wed, 20 Oct 2004 10:40
143 lines
5256 bytes
5256 bytes
"McKool" <kenethw@peak-system.com> ha scritto nel messaggio
news:4176363d@newsgroups.borland.com...
> Have someone an example of how works SharedMemory? I want to communicate
> two programs that work in the same computer (as Cleint/Server app).
>
> Thanks In Advance.
>
> K.W.
>
Hi
I put here the code I use to communicate some parameters among several
application on the same PC having one of these as Server (Supervisor) using
shared memory. If you have some problem feel free to email me!!
good luck
Paolo
HANDLE FShareHandle;
void * FMemory;
SendHASMessage(hmHASSUPERVISOR,hmHASPHONEANS,hmCMD_HIDE,0,0,"");
////////////////////////////////////////////////////////////////////////////////
void __fastcall TFMain::SendHASMessage(int mMittente,int mDest,int mCmd,
long lPar1, long lPar2, AnsiString sPar1)
{
hmc.idMit = mMittente;
hmc.idDest = mDest;
hmc.idCmd = mCmd;
hmc.lPar1 = lPar1;
hmc.lPar2 = lPar2;
strcpy(hmc.sPar1,sPar1._S);
memcpy(FMemory,&hmc,hmcSIZE);
HWND tw = FindWindow(NULL,destWindows[mDest-100]._S);
SendMessage(tw, UM_HASCOMMANDS, 0, 0 );
}
////////////////////////////////////////////////////////////////////////////////
void __fastcall TFMain::hcGetParameters()
{
memcpy(&hmc,FMemory,hmcSIZE);
}
////////////////////////////////////////////////////////////////////////////////
void __fastcall TFMain::SetSharedMem()
{
char name[20];
strcpy(name,"HASCMDMEM");
FShareHandle = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE,
0, (int)hmcSIZE, name);
FMemory = MapViewOfFile( FShareHandle, FILE_MAP_WRITE, 0, 0, 0 );
if ( FMemory == NULL )
MsgBox("ERRORE CREAZIONE PIPE COMANDI","H.A.S. Error SM2",0);
}
////////////////////////////////////////////////////////////////////////////////
void __fastcall TFMain::DestroySharedMem()
{
if ( !UnmapViewOfFile( FMemory ) )
{
MsgBox("ERRORE CHIUSURA PIPE COMANDI","H.A.S. Error SM3",0);
FMemory = NULL;
}
if ( FShareHandle != 0 )
if ( ! CloseHandle( FShareHandle ) )
MsgBox("ERRORE CHIUSURA PIPE COMANDI","H.A.S. Error SM4",0);
}
void __fastcall TFMain::WndProc(Messages::TMessage &Message)
{
switch ( Message.Msg ) {
case WM_ERASEBKGND:
Message.Result = 1;
break;
case UM_HASCOMMANDS:
hcGetParameters();
if ( hmc.idDest == hmHASSUPERVISOR )
{
switch ( hmc.idMit )
{
case hmHASPHONEANS:
switch(hmc.idCmd)
{
case hmCMD_APPHIDDEN:
SegreteriaTelefonica1->Checked = false;
break;
case hmCMD_APPSHOWN:
iPhoneAnsw->Show();
PHONEANS_READY = true;
SegreteriaTelefonica1->Checked = true;
break;
case hmCMD_APPCLOSED:
iPhoneAnsw->Hide();
PHONEANS_READY = false;
SegreteriaTelefonica1->Enabled = false;
break;
}
break;
case hmHASSPEAKER:
switch(hmc.idCmd)
{
case hmCMD_APPCLOSED:
iVoxModule->Hide();
SPEAKER_READY = false;
USEVOICE = false;
break;
case hmCMD_APPSHOWN:
iVoxModule->Show();
SPEAKER_READY = true;
USEVOICE = true;
break;
case hmCMD_ENDSPEAK:
SPEAKING = false;
tSpeak->Enabled = true;
break;
}
}
}
Message.Result = true;
break;
default:
TForm::WndProc(Message);
break;
}
}
Technical Details
Message-ID:
<417640b9@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!diablo.voicenet.com!38.119.100.83.MISMATCH!news-out1.nntp.be!propagator2-sterling!in.nntp.be!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4176363d@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,72d9dbdc5a1eeafa X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Paolo Borlandi" <pborlandi@virgilio.it> Newsgroups: borland.public.cppbuilder.nativeapi References: <4176363d@newsgroups.borland.com> Subject: Re: Communication with Shared Memory Date: Wed, 20 Oct 2004 12:40:53 +0200 Lines: 148 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 82.48.239.9 Message-ID: <417640b9@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1098268857 82.48.239.9 (20 Oct 2004 03:40:57 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!diablo.voicenet.com!38.119.100.83.MISMATCH!news-out1.nntp.be!propagator2-sterling!in.nntp.be!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail