🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages Started by Nate Lockwood Sat, 25 Feb 2006 16:13
Sending data from one application to another
#4238
Author: Nate Lockwood
Date: Sat, 25 Feb 2006 16:13
13 lines
1353 bytes
My application detects a previous instance running, posts a message to 
the previous instance, and returns.  If that application was started by 
the user double clicking on a file or group of files I'd like to pass 
them to the running application.  I can see how to pass the address of 
a, say, TStringList but that doesn't work as, AFAIK, the running 
application would then try to access protected memory.  Is that right?

I can see how to save the file list to a file but is there a way to pass 
it in memory?

Thanks

Nate
Re: Sending data from one application to another
#4239
Author: Bob Gonder
Date: Sat, 25 Feb 2006 16:46
6 lines
1188 bytes
Nate Lockwood wrote:

>I can see how to save the file list to a file but is there a way to pass 
>it in memory?

Have you looked at WM_COPYDATA ?
Re: Sending data from one application to another
#4240
Author: Nate Lockwood
Date: Sat, 25 Feb 2006 17:23
11 lines
1260 bytes
Bob Gonder wrote:

> 
> Have you looked at WM_COPYDATA ?
> 

Never heard of it 'till now but found: http://tinyurl.com/62khl at MSDN. 
  I was hopeful that a more elegant solution than a temporary file or 
socket was available.

Thanks
Re: Sending data from one application to another
#4241
Author: Nate Lockwood
Date: Sat, 25 Feb 2006 18:57
4 lines
1129 bytes
I googled and found a couple of forum discussions that I think I can 
puzzle out.

Nate
Re: Sending data from one application to another
#4243
Author: Dennis Jones
Date: Sun, 26 Feb 2006 00:07
17 lines
1511 bytes
"Nate Lockwood" <RNLockwoodREMOVE@attglobal.net> wrote in message
news:4400928d$1@newsgroups.borland.com...
> Bob Gonder wrote:
>
> >
> > Have you looked at WM_COPYDATA ?
> >
>
> Never heard of it 'till now but found: http://tinyurl.com/62khl at MSDN.
>   I was hopeful that a more elegant solution than a temporary file or
> socket was available.

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.

- Dennis
Re: Sending data from one application to another
#4249
Author: Nate Lockwood
Date: Sun, 26 Feb 2006 01:05
10 lines
1303 bytes
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.

Nate
Re: Sending data from one application to another
#4252
Author: Dennis Jones
Date: Sun, 26 Feb 2006 01:54
108 lines
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
Re: Sending data from one application to another
#4253
Author: Nate Lockwood
Date: Sun, 26 Feb 2006 16:01
19 lines
1719 bytes
Dennis Jones wrote:

> 
> Get your Win32 SDK help file and read up on:
> 
> CreateFileMapping
> OpenFileMapping
> MapViewOfFile
> UnmapViewOfFile

I did find the SDK help and did a quick read as I was still working on 
SendMessage where I managed to break my ap, it returns 0, which I guess 
is "No Error" but is not seen at all on the receiving side.  I've had a 
night's sleep now and will look at it again later today or Monday.  I'd 
like to learn as I can see uses for them in some of my other software.

Thanks for your help

Nate
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads