🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #125

Re: Getting Started with Memory Mapped Files

#125
From: Remy Lebeau (TeamB)
Date: Wed, 11 Jun 2008 18:52
77 lines
3732 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message 
news:48500053@newsgroups.borland.com...

> Currently I've been storing these in a STL vector but now I need
> to provide access to this list, to another process.
>
> Having read a bit about this, it seems that a Memory Mapped
> file would be the easiest option.

Maybe, maybe not.  Since your service is doing live logging, you would have 
to provide serialized access to the list so that other apps could not query 
it at the same time it is being updated.

It might be better to have the service expose a separate server connection, 
whether that be a named pipe or TCP/IP socket or COM server or other IPC 
mechanism, that other apps can connect to and query the data using a 
structured protocol.  Then the service does not have to expose direct access 
to its private memory.

> I need my service to be able to read and write to the MMF
> but I don't want any other process to be able to modify it...

The service can create the MMF with read/write access, and each application 
can open the MMF with read-only access.

> Is CreateFileMapping() used to create a MMF

Yes.

> does my service create the initial MMF with CreateFileMapping(),

Yes.

> and does my secondary process get access to the created MMF
> with the same API call, CreateFileMapping?

It can, though in your situation it would be better to have the secondary 
processes use OpenFileMapping() instead.

> I'm assuming that using CreateFileMapping(INVALID_HANDLE_VALUE,...)
> means that I don't actually have to physically create a file on disk 
> initially?

You don't create your own file at all, no.  The memory for such a MMF is 
backed by the system paging file, though.

> Can I increase or decrease the size of the MMF once I've called 
> CreateFileMapping()

No.  You would have to re-map it.

> should I try and determine the maximum size that it will ever be, 
> initially?

Yes.

> How to I secure the MMF so only my service can access it?

If you do not provide a value in the lpName parameter of 
CreateFileMapping(), then no other process will be able to find it to open 
it.

Alternatively, CreateFileMapping() has a lpFileMappingAttributes parameter, 
so you can construct a SECURITY_ATTRIBUTES that grants access to only your 
service process.

> I'm looking at a code example in VC++ and the author seems to have
> used a mutex to control access whilst reading or writing - is this 
> necessary?

Yes, if the contents of the MMF change dynamically.  You don't want a 
secondary processes to read the data while the service is currently writing 
it.  They could end up with incomplete/corrupted data.


Gambit 
Technical Details
Message-ID: <48501f3a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <48500053@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b80765c4c39946b4
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <48500053@newsgroups.borland.com>
Subject: Re: Getting Started with Memory Mapped Files
Date: Wed, 11 Jun 2008 11:52:46 -0700
Lines: 79
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 207.178.157.90
Message-ID: <48501f3a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1213210426 207.178.157.90 (11 Jun 2008 11:53:46 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail