[Autogenerated] Let's close all instances and add the file to the mix.
So going back to create fine mapping instead of
providing invalid handle value here,
let's create a file or open the file to use the create
file function which is really short for create file
object that might point to a file.
And let's just select some filing, like temp my data dot txt.
Something very, very simple.
And then the access I would like is read and a right because I
want to be able to read and write to the file.
Eventually we need to provide some kind of sharing so we can use file share,
read.
Maybe that's going to be good enough.
Maybe no sharing at all.
Maybe I want an exclusive access to the file once it is open,
which kind of makes sense.
And then we have security attributes.
I'm going to just use um null pointer here.
The next thing is important, which is the creation disposition it indicates.
Should they create a new file?
Should open an existing file.
So in order not to to make the existing contents go away, I'm going to use open.
Always open.
Always means that if the file doesn't exist, please create it.
If it does exist, just open it and then we have some flags,
which I don't care about and a template file which you don't care about either.
So once we have that we're going to pass h file to this,
create fine mapping call.
So we're going to create a file,
open a handle to that file and then pass that along to create fine mapping.
And technically after that it is possible if we so
desire to close the handle to the file.
This is okay, this is okay to do.
And the reason for that is because the memory file is going to
take a reference on the file object anyway.
So in fact closing the handle is perfectly fine.
The file object will not actually be destroyed because
there is another reference taken by the section object
internally because of that file.
And in fact that should be it.
Let's see, let's try it out.
Let's see if that works.
So I'm going to give one instance, that's another instance.
The basic sharing capabilities of memory shouldn't really change at all.
So that should be exactly the same.
It's just a matter of whether the final content is
going to be residing in the farm.
So I've written some text here into the shared memory.
Let me close all these things all together.
We can open c temp to see what's going on here.
Well,
I guess that probably is not going to work out because
there is no sea temp on this machine.
So that's not going to be very good.
So let me just create that folder.
I didn't do any error handling any error checking.
So in fact my experiment here didn't really succeed
and the handle to the file failed.
But the sharing of memory didn't really fail why because h file when it fails,
has the value invalid handle value,
which is exactly the one that's going to be passed here.
So essentially from that perspective the chairman will still work but the page
file will be the backup but now hopefully it's going to be our file because c
temp now exists and there is a chance for that work.
So again,
let me create two instances we can create just one
or three or any number really.
So again, let's do something like hello this time with more exclamation marks,
just to make a point here is the read operation,
everything looks fine.
Let's close all that.
But even though we close everything now we have my
data txt here in the file system, notice its size,
that's exactly one megabyte if I double click that
we'll be able to see the content Well, very close to the content.
So I'm using Notepad here.
Notepad doesn't really understand that the string is unicode.
Utf 16 so it tries to interpret that as a ski.
And so the high byte here,
every time we see the empty space here is really just zero free
come from the perspective of Notepad so it just shows that as
space but it's actually zero byte.
So this is why we see what, what we see here,
that's because we're working with unicode functions throughout the
application going back here if I just go ahead and run another new instance
and click the read button without writing anything.
I should see the contents and here they are because the
file wasn't created from scratch,
it was opened with its existing content and that's why
I'm able to see the content right back.
By the way, notice what happened for the second instance.
Let me just run one of those instances again, we can read and see the data.
Let me run this instance with the debugger pressing a five here to go into
the create file call here just to see what's going on here.
And you notice you notice that one side try that create
file actually is going to fail on me.
So the second time it's supposed to fail on me notice
the handle is -1 which is invalid, handle value.
Why?
Because the file was opened in the first place with exclusive access
so I can't open another handle to this file again.
But that is okay.
So that invalid handle value would be passed here to create fine mapping.
And that's going to work out just fine because the creator is really an open
operation because there's already one instance in play.
And so what we'll get is another handle to an existing financing object.
In fact that in this case all the other parameters really don't
make any sense and they don't mean that much.
So that the fact that it's the same name,
that's of course very meaningful, but the size has no effect.
And uh and so the file here doesn't really have any effect anyway,
because it's an existing memory mapped file object.
The only thing here that is completely useless is closing off the handle
because I'm closing invalid handle that is going to fail as well.
So in fact, to make it slightly better,
perhaps I should do something like if h file is not invalid,
handle value only,
then there's some meaning into enclosing the handle to the file.
That will only happen for the first instance only actually
creates that file object to begin with.