[Autogenerated] now that you have the basic allocation in place.
We want to implement the read and write button so let's
close the application and go back to our code before we do
the actual implementation for that.
There is a helper function here called colic and display address
based on the cell we're selecting so I want to use that as well in
order to call that from read and writes,
we can see the address that is going to be used for read and write operations.
So we have something to correlate with VMs So first thing we
need to do is to calculate the address.
So the actual address for the particular cell has to start with
the basic m address we've allocated and then we have to add
something that has to do with the size here.
So we have X and need to add X.
In fact to multiply y by the size X.
So the width of the cells.
Then we also have to multiply by the sales side which is necessary because
each cell has that particular size that's the actual cell will lend into in
terms of the address if we calculate things right.
The only thing that remains to be done here just to cast this to a bite stars.
These calculations are being performed with byte granularity.
Otherwise well it does not compile because you cannot perform any kind
of pointer or arithmetic with void star pointers.
You have to do something different and bite starts exactly what we
want to have this calculation happening in bytes.
So now we're going to do the same thing we've seen earlier just
format this address here using percent X and zero X and percent P
which is which stands for pointer.
And then we'll just grab our address whatever that value is.
And then we need to set that as a text to the I D C cell value here.
That's the idea for the address for a particular cell.
You can view that in the resource editorial
Visual Studio if you want to verify that.
And then our text here and will also return that address for the color.
So displaying will do two things here.
We're displaying the address and also returning that to the color.
So now we're ready to perform some operations such as read and
write which are the ones we want to test out.
So in terms of right let's see what happens.
The first thing I need to do here is to grab the X and Y values.
I'm going to call the get the LG item in this is just the helper.
WTL function to grab the integer from from some text box.
So I'm going to do some very simple here just to
grab that information for X and y.
We have these two control I d C X and Y.
And then we can go ahead and call this calculate and display address.
So this is where we can call that one and we're
going to get the the address back.
Let's call that P.
So we have X and y were passing along, we'll get the the address back here.
And so now we try to write whatever text is specified in that text
box in the application into this specific address.
So for this purpose I'm going to just use the very simple function wcs copy.
So I have to first retrieve the text from the text box.
I'm going to use another function from WTL here.
That's going to just bring me the text into a string object and then I
just need to copy that string into the cell address.
I'm going to use the W string copy here because we're using unicode strings.
And the underscore s is the safe variant of functions,
which means I will have to specify the maximum length
that I'm that I'm willing to write.
So we know the maximum length is essentially depends on the cell size,
but for now we'll just use the simplest possible thing
we can so first we need to cast here,
the compiler is happy we need to write something into the p address that we
have calculated the size is going to be cell size at most.
Um and we have to divide that by white character because the size is
supposed to be in characters and not in in byte.
And then our source is text itself,
which has a proper operator that converts the string into the actual
pointer two characters which is stored within that C stream,
essentially this is it.
This should do the trick.
So to test that,
let's see what happens while we have to also add some code for reading.
So let's add some code for reading in the same way.
So I'm going to copy this code here,
I'm just going to copy that so we can get the same information
based on the address and the cell that we like and then we need
to grab the text from the address and write that into into the
cell into the edit box.
So for that,
I'm going to do that in two state And that's going to be
mainly to demonstrate something a bit later.
Let me just create a text here.
That's going to be 128 characters in size.
And I'm going to call the get well actually just copy
into text whatever is coming from.
P just casting it out so the compiler is happy and if everything goes as
planned we can now call said the LG item text and just put inside text
whatever we copied into this temporary buffer.
So we could have done that directly.
However,
there's a slight snag is that this function is also
swallowing exceptions and I don't want those exceptions
swallowed if indeed they are raised.
So that's why I'm doing this in two steps in to demonstrate the point,
we'll get to that very soon.
So in order to test that,
let's start by going to allocate here and then going back
to the previous case where we had committing the memory and
reserving at the same time.
So let's try this out.
So here's our mini Excel application.
Let's uh re select a process here from VMs because against a new process now.
So here goes we have here the um the private data,
let me click allocate and again we need to refresh and you can see there's
these 256 megabytes of memory which is currently committed,
which means that if I for example, read something from cell 00,
I expect to find something which is basically nothing because we
know that committed memory always starts out completely zeroed
out and notice the address being calculated is the same address
as the base of this matrix, because really we have X and y as zero.
Let's insert some _____ text here and then click right,
once we do that,
the write operation has succeeded because we didn't get any kind of error here.
So if I go ahead and go to sell one for example and read it,
there will be nothing here.
If I go to sell zero and read it,
our text is there and that's very easy because everything is committed up front.
Let me go to a different cell all together, like 100 in X and maybe a 50 in y.
If I read there will be nothing if I right then to be written there.
If I go one cell to the right, there's nothing there.
If I go back,
I see when clicking read the previous text and that's probably
what we expect here because everything is committed up front or
really not much of a big deal here.
So let's change that.
Let's change that in order to conserve memory.
We don't need the entire 256 MB upfront because unlikely
the user will use all of these cells.
So instead we want to be more efficient by reserving memory only,
which doesn't really commit anything and just allows us to have a
reservation of a continuous hundreds range to be used for later.
So if I go ahead and we select the process again because we have
started from scratch and performed the allocation which in this
case is going to be reservation of memory.
If I refresh that as before we have our 256 megabyte chunk here,
everything here is reserved.
So if I go ahead and insert some _____ text and try to write it,
I'm going to get an exception.
My process crashes.
It simply crashes goes away.
Um and that's because we try to write to inaccessible memory to
verify that let's add to the right case here.
Let's add a try except let's just except whatever exception occurs.
Let's just use execute handler here for simplicity and then we can
_______ instead of kind of the process crashes just _______ the
exception and say that there's a problem here just to prove that
indeed this is the case will react so we have access violation here
and of course we can verify that.
We're looking at the get exception code function.
If we so desire.
I'm going to leave that as an exercise for you dear viewer.
Let's do that.
And that's it.
Let's try it now.
So there's a little snag here because we cannot use try except in functions
where there are objects that have the structures that require object and winding
this is that string here here that we see right now.
So let me just fix that by changing that to a simple string instead
of a C string like so that should be good enough.
Let's try this.
So now if I allocate and try to write something to whatever cell we're
going to get that access violation here just to prove that indeed the
problem is that we cannot write to the address here.
That particular address speed that we see here,
which is the cell address in this case is the same as the base address.
We can't do that because there's nothing there.
So we have to fix that somehow.
So let's do that in the next clip