The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: xanatose on September 24, 2010, 01:05:05 AM

Title: How to I share memory between 2 different process on same machine?
Post by: xanatose on September 24, 2010, 01:05:05 AM
What do I need to look into?

Title: Re: How to I share memory between 2 different process on same machine?
Post by: redskull on September 24, 2010, 01:26:33 AM
section objects (and/or memory mapped files)

-r
Title: Re: How to I share memory between 2 different process on same machine?
Post by: donkey on September 24, 2010, 01:26:50 AM
You can go one of three routes.

Memory mapped files.
Marshal data using WM_COPYDATA
Use a shared DLL

In addition to those three, there are also a number of API's that will allocate shared memory, VirtualAllocEx/VirtualFreeEx being the most useful but they are only available on NT based systems. If you need 9x support (though I can't imagine why) you can use the ordinal calls available in comctl32.dll (ordinals 71,72,73 and 74 - Alloc, ReAlloc, Free and GetSize respectively)

Edgar

Title: Re: How to I share memory between 2 different process on same machine?
Post by: oex on September 24, 2010, 01:28:17 AM
:lol What they said I was too slow :bg

I did find links though :P

Memory mapped Files: http://support.microsoft.com/kb/188535
WM_COPYDATA http://msdn.microsoft.com/en-us/library/ms649011(VS.85).aspx
Title: Re: How to I share memory between 2 different process on same machine?
Post by: jj2007 on September 24, 2010, 06:40:16 AM
See also the WM_COPYDATA thread (http://www.masm32.com/board/index.php?topic=13215.msg102618#msg102618) - it's really simple.
Title: Re: How to I share memory between 2 different process on same machine?
Post by: ToutEnMasm on September 24, 2010, 12:18:03 PM

see also DDE,atom
Title: Re: How to I share memory between 2 different process on same machine?
Post by: hutch-- on September 24, 2010, 12:44:45 PM
 :bg

You can tell the men from the boys by the paper they use.  :bdg

use a "Memory Mapped File", the way REAL MEN[TM} do.  :P
Title: Re: How to I share memory between 2 different process on same machine?
Post by: redskull on September 24, 2010, 12:48:57 PM
Just to clarify, a "memory mapped file" is not necessarily a real actual disk file; shared memory just uses the same mechanisms as mapping files (section objects), so a named section object that is backed by the pagefile is, essentially, a block of shared memory.

-r
Title: Re: How to I share memory between 2 different process on same machine?
Post by: jj2007 on September 24, 2010, 01:22:38 PM
Quote from: redskull on September 24, 2010, 12:48:57 PM
Just to clarify, a "memory mapped file" is not necessarily a real actual disk file; shared memory just uses the same mechanisms as mapping files (section objects), so a named section object that is backed by the pagefile is, essentially, a block of shared memory.

-r

Red,
Can you give an example of a memory mapped file that does not use a disk? It is one of the reasons why I'd prefer WM_COPYDATA (not being a Real Man in Hutch' opinion :green2).
Title: Re: How to I share memory between 2 different process on same machine?
Post by: redskull on September 24, 2010, 01:45:00 PM
Call CreateFileMapping() with hFile as INVALID_HANDLE_VALUE on a system with no pagefile.  :bg
Title: Re: How to I share memory between 2 different process on same machine?
Post by: jj2007 on September 24, 2010, 01:49:16 PM
Quote from: redskull on September 24, 2010, 01:45:00 PM
Call CreateFileMapping() with hFile as INVALID_HANDLE_VALUE on a system with no pagefile.  :bg

Sounds exciting but where do a find a system with no pagefile?
:wink
Title: Re: How to I share memory between 2 different process on same machine?
Post by: redskull on September 24, 2010, 02:03:19 PM
My point was that you don't need create the file yourself; tell someone that they need to use a "memory-mapped file", and they often get the misconception that they have to create "MyTempFile.tmp" someone on the hard drive, map it into memory, and use that.  Perhaps I should have said it doesn't use the disk any more than something like GlobalAlloc() (or WM_COPYDATA) does.  :bg

-r
Title: Re: How to I share memory between 2 different process on same machine?
Post by: jj2007 on September 24, 2010, 04:16:18 PM
Quote from: redskull on September 24, 2010, 02:03:19 PM
Perhaps I should have said it doesn't use the disk any more than something like GlobalAlloc() (or WM_COPYDATA) does.  :bg

Would GlobalAlloc use the disk (i.e. the pagefile) if enough physical RAM was available? I am not trying to be difficult, just curious :bg
Title: Re: How to I share memory between 2 different process on same machine?
Post by: redskull on September 24, 2010, 04:40:43 PM
It depends on what you consider "using".  Technically, any commited memory is backed by a file, be it a "real" disk file or the page file.  When the page of memory is commited (which, in the case of GlobalAlloc(), the heap manager would do), a space in the pagefile is reserved for it, but it would only really 'use' it if it gets paged out.  So while it will potentially never be written to, the space in the file would still be there for it, and still count against you in the commit charge (which would be the same thing that happens in a pagefile backed memory mapped file).

Also, just to point out, paging has nothing really to do with physical RAM availability; a process is given a limit of space it can use, and if it exceeds this, it will start taking paging hits, regardless if there is usable RAM or not.  Of course, if there's lots of free RAM, windows will up this limit for processess that are paging alot, so they are indirectly related.

-r
Title: Re: How to I share memory between 2 different process on same machine?
Post by: donkey on September 24, 2010, 05:00:13 PM
Memory mapping using the page file is documented at MSDN Creating Named Shared Memory (http://msdn.microsoft.com/en-us/library/aa366551%28v=VS.85%29.aspx), it includes a code example to use it. Be sure to check the UAC limitations at the bottom of the page.