How to I share memory between 2 different process on same machine?

Started by xanatose, September 24, 2010, 01:05:05 AM

Previous topic - Next topic

redskull

Strange women, lying in ponds, distributing swords, is no basis for a system of government

donkey

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

"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv


ToutEnMasm


hutch--

 :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
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

redskull

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
Strange women, lying in ponds, distributing swords, is no basis for a system of government

jj2007

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).

redskull

Call CreateFileMapping() with hFile as INVALID_HANDLE_VALUE on a system with no pagefile.  :bg
Strange women, lying in ponds, distributing swords, is no basis for a system of government

jj2007

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

redskull

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
Strange women, lying in ponds, distributing swords, is no basis for a system of government

jj2007

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

redskull

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
Strange women, lying in ponds, distributing swords, is no basis for a system of government

donkey

Memory mapping using the page file is documented at MSDN Creating Named Shared Memory, it includes a code example to use it. Be sure to check the UAC limitations at the bottom of the page.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable