Article View: borland.public.cppbuilder.nativeapi
Article #4252Re: Sending data from one application to another
From: Dennis Jones
Date: Sun, 26 Feb 2006 01:54
Date: Sun, 26 Feb 2006 01:54
108 lines
4542 bytes
4542 bytes
"Nate Lockwood" <RNLockwoodREMOVE@attglobal.net> wrote in message
news:4400fecc$1@newsgroups.borland.com...
> Dennis Jones wrote:
>
> >
> > How about a memory-mapped file? With a memory-mapped file, you can set
up
> > an area of shared memory that both applications have direct access to.
>
> That sounds promising, I know nothing about it, though, is memory-mapped
> a good keyword to search on.
Get your Win32 SDK help file and read up on:
CreateFileMapping
OpenFileMapping
MapViewOfFile
UnmapViewOfFile
Basically, what you do is create a file in memory with a specific name and
size using CreateFileMapping(), then map that file into application memory
(so that it has a real address) using MapViewOfFile(). Then you can read
and write to it using ReadFile() and WriteFile(). But because the file is
in memory, you are actually reading from and writing to memory directly, not
a (disk) file -- so it will be very fast. Then to access this same memory
from another application, you use OpenFileMapping() (as opposed to
CreateFileMapping) using the same file name that you used to create it in
the other application, and then MapViewOfFile() to map the file to the
application's memory.
The following code was taken from a working program, with a few pieces added
in and others taken out to keep it as short and simple as possible.
In the first application:
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;
char FName[] = "MySharedMemory";
int FSize = 1000;
HANDLE hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF,
&sa, PAGE_READWRITE,
0,
FSize,
__TEXT( FName ) );
if ( hMapFile == NULL )
{
// error creating the file mapping
}
// map the file into this application's memory
void *pMapView = MapViewOfFile( hMapFile, FILE_MAP_READ | FILE_MAP_WRITE,
0, 0, 0 );
if ( pMapView == NULL )
{
// error mapping the file to memory
CloseHandle( hMapFile );
}
// pMapView is a pointer to the memory -- remember it's only 'FSize'
bytes!
// write something to the memory
strcpy( (char *)pMapView, "This is my shared memory" );
// When you're done with the file:
UnmapViewOfFile( pMapView );
CloseHandle( hMapFile );
/******************************/
In the other application:
char FName[] = "MySharedMemory";
HANDLE hMapFile = OpenFileMapping( FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
__TEXT( FName ) );
if ( hMapFile == NULL )
{
// error opening the file mapping
}
// map the file into this application's memory
void *pMapView = MapViewOfFile( hMapFile, FILE_MAP_READ | FILE_MAP_WRITE,
0, 0, 0 );
if ( pMapView == NULL )
{
// error mapping the file to memory
CloseHandle( hMapFile );
}
// read the string that the other application wrote to our shared memory
char str[FSize];
strcpy( str, pMapView );
// When you're done with the file:
UnmapViewOfFile( pMapView );
CloseHandle( hMapFile );
/******************************/
This is a fairly basic example (it doesn't synchronize memory access, for
instance), but it should give you a pretty fair idea of what you need to do.
- Dennis
Technical Details
Message-ID:
<44010a5b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<44008241$1@newsgroups.borland.com> <be21025qs0d06a6tkmj646iu7lokr7lklq@4ax.com> <4400928d$1@newsgroups.borland.com> <4400f128$1@newsgroups.borland.com> <4400fecc$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1f88854cd7bf41f0 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Dennis Jones" <nospam@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <44008241$1@newsgroups.borland.com> <be21025qs0d06a6tkmj646iu7lokr7lklq@4ax.com> <4400928d$1@newsgroups.borland.com> <4400f128$1@newsgroups.borland.com> <4400fecc$1@newsgroups.borland.com> Subject: Re: Sending data from one application to another Date: Sat, 25 Feb 2006 17:54:37 -0800 Lines: 110 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 204.200.27.7 Message-ID: <44010a5b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1140918875 204.200.27.7 (25 Feb 2006 17:54:35 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail