[Autogenerated] Here's my Visual Studio running,
let's create a new project and to make things slightly more interesting.
Let's create an mfc application.
Mfc is an old library created by Microsoft in the nineties.
The C++ library that provides some helpers in creating some user interface.
So I'm going to use that even though Mfc is not very popular these days,
it's still very well supported by visual studio.
If you want to try out these things yourself,
you might need to go to the visual studio installer and C libraries,
otherwise things will not work out for you very well because
they are not necessarily installed by default.
So I'm selecting Mfc application here and clicking next,
I'm going to give it a name like shared demo and click
create and then we need to select in the wizard here what
kind of project we would like?
I'm going to select the simplest possible project,
which is a dialogue based project.
I'm going to just simply click finish here,
that's going to be good enough for our purposes.
And my goal here is to be very simple.
UI here,
you can see the dialogue that's been created for us
going to delete all these controls, make it slightly simpler.
Just going to do something very simple in order to demonstrate memory
files and their sharing of member capabilities.
I'm going to go to the toolbox here and just create these red box here.
That's going to be used for setting some kind of text would like
to share with multiple potential processes.
Let's just give it a nicer name like I d c text here at the bottom.
And then I'm going to create two _______.
One of them is going to be for writing purposes and the other
one is going to be for reading purposes.
I'm not going to bother giving them very nice names.
I'm just going to select both of them with the control key.
Go to format and just make them align nicely at the bottom.
So arranged.
But bottom.
And this is what we get.
If double click,
the right button will get a handler created by the wizard here for this button,
going to create another handler for the red button or double clicking it.
This is a very simple thing to do.
So I didn't have to add any code manually in order to wire up these click
handlers to some functions that's going to be for right.
And the other one is going to be for red.
So now we want to demonstrate the idea of sharing memory between two processes.
Now,
the way this project is built is that you have this
shared demo deal G class created by mfc.
Were supposed to be saving our states.
I'm going to add a handle here to our memory mapped file objects.
It's called that file H file and there's a function called dialogue,
which is wired up to a message that comes up when
the dialogue initially loads up, you can see there's a to do here saying,
add extra initialization here.
Great.
This is what you want to add and create the file mapping object that will use
for later on for right and read for changing the memory.
So here's how to do that first.
What we're going to get back is our main file.
So let's just grab this variable here and call the function.
Create file mapping.
Great Fan mapping is the one that allows us to create a file mapping object,
specifying its properties.
The first one is a handle to a file.
So this is the case where we want to back up our shared memory with the
true for so that the data remains and persistent later on.
We'll do that in the next example.
But for now let's start with the special value invalid handle
value which is going to be interpreted as used the page file as
the backup for the shared memory.
So as long as the object is alive,
the memory is still there and it might be backed up by the page file if needed.
Once all the handles are closed to the fine mapping object.
The memory is going to go away.
It's going to be so recycled within the page file we have some
security attributes which I don't care about him.
Let's just grab the default.
We need the protection here to indicate what kind of
mapping will be able to do later on.
I'm going to use page read right to indicate that would like to have read,
write memory And then we have the size of the fire mapping
object which is comprised of two 32-bit values which are
just one big 64-bit values.
So essentially it could be very,
very big assuming of course the page file is large enough to accommodate that
in this case or the actual file if you are using a file.
So in this case I'm just going to do something fairly
simple and create a one megabyte size, shared memory.
So one shifted 20 beats to the left, that's two to the 20th power,
that's one megabyte and the high 32-bit value is going to be zero,
I don't go beyond four gigabytes in this example and then we can
provide a name for the file mapping object,
this is not the name of the file is the name of the section
object itself that could be used by another process or
processes to share that memory.
So we want that because we know that one way to share
kernel objects in Windows is by name,
let's call that something like my shared memory and this should work out.
We'll see if that fails later on,
but let's just create a simple assert here just to verify that what
we get here is not going is not failing on us.
So once we have that we can now go ahead and implement
the right and read functionality in order to perform some
kind of write functionality.
We have to first map a view into the address space of our process.
So I'm going to call the function map,
view of file and need to provide the handle to the memory mapped file object,
the kind of mapping I'd like to do in this case I
would like to do um right mapping, so file map,
right is the correct constant to use and then I can select where I want
to start the mapping and the size of that mapping.
And again,
because we potentially might have very large piece of
memory even beyond four gigabytes,
we have two 32-bit values to create the offset and then
one 64-bit value to specify the size.
So I'm going to start with offset zero for
simplicity and going to map 64 kilobytes.
That's going to be good enough.
So not mapping the entire size of the shared memory which is one megabyte
in this case I'm just mapping the 1st 64 kilobytes.
And of course later on I can unmapped and map a different
location if I so desire assuming this succeeds and it should
unless my system is really low on memory,
let's do a quick assertive just to make sure now we
can just write to this memory.
So to do that, I'm going to use some mfc code to get the text,
get the LG item, text from the text box into a c string variable,
which is a class provided by M f C and an A T L and W T L as well,
by the way, that just rubs a nice string.
And then the only thing I need to do is just to use this string
copy function to copy inside that shared memory which is
currently mapped the 1st 64 kilobytes at least are mapped into my
address space at the address P.
So I'm going to just cast here.
So the compiler is happy I need to provide the size which is
going to be the length of the of the string here.
I'm going to add one because we need to account for the Nile terminator.
So get length here in the C string object just does something like W
C S land just looks up the length of the string,
but also it's already stored there.
So it's slightly faster and then we can provide the
source which is exactly text.
That would be the actual C string because it has a proper
operator to return the internal pointer to the actual
string and that should be it.
The only thing left to do is to add map view of five here so
that we don't need that mapping anymore.
But the memory is still there.
It's still there because because the fine mapping
object is still very much alive.
Let's now do the read operation and see what that looks like if we try it out.
I'm going to just do some copy pasting here and make the correct changes.
So the first thing I need to do is to change the
mapping that I'd like to do to read from,
right.
I still want to map exactly the same region.
Otherwise I will not see the right text to the right information.
And then I just need to do something similar here.
Just called the LG item, text to put in the I D C text,
edit box, whatever P is storing.
So I'm going to assume it's going to be a string.
Just cast this or compiler is happy and just passed P here.
I'm going to pass P and P points to the actual string
that is within our shared memory.
We're sharing a string here and that should be in the next clip.
We'll test it out.