[Autogenerated] Let's now examine the second way of sharing objects.
Kernel objects between processes using something called handle inheritance.
Let me create a new project Here,
let's call that Simply inherit let add that to that solution.
So we have all the demos, in the same place.
Let me as usual remove all the boiler plate,
coded end includes for Windows dot H, and STD,
io If we need that So here's the idea of using handling inheritance,
this mechanism.
Only works when one process creates another process.
If the other process.
Is already running then this method of sharing objects.
Is irrelevant.
And cannot be used, which means that the create process,
core must be here somewhere.
So let's go ahead.
And create one.
So I'm going to Just uh use the uh the code that we've done in previous classes.
To create a process.
So we have a start up info, start, up info structure,
and her process information as an output and We need to create something.
So let's go with the simple Notepad here.
Now, in the real scenario, these process.
You're creating is part of your software system.
You're not creating some random process.
To share an object with you're creating something very specific.
That is part of your system which you would like to share,
a bunch of objects, one or more but more easier demonstration,
just going to use Notepad as as an example,
just to show the mechanism how it works.
And So we need to make the create process.
Call Let's do that So we have create process here.
We're going to Provide the name here for the Notepad process.
To be created these attributes, are not really important at this point.
And the important point here.
Is to have two as this parameter.
So this parameter called inherit handles,
is a general flag indicating whether I would like handle inheritance.
To work in general when creating that new process.
If I specify false then no handles, are going, to be inherited.
If I specify truth it means that only handles,
that I mark as inheritable will be inherited.
And I'll show you how to mark handles as inheritable in a moment.
But that's crucial piece.
In order for this mechanism.
To work, then We have some creation flags which you don't care about.
And the other parameters.
I'm going to just use the default and provide the start,
up info, and the cross information as the last structures,
I'm not going to Even bother checking the return value here.
I expect Notepad to run successfully.
So before I do that I need to uh to prepare the handles.
I would like to share.
So let's create um a handle, to something,
an an object, uh some kind of kernel object,
and Again,
let's go with an event Uh but it could be anything really but in the This case,
what I'm going to do I'm not going to bother,
and create a name.
In fact, I don't want to create a name.
Let's just assume, that We're talking about an object type.
That doesn't even have a property called a name something.
We can actually specify,
but even if we could specify a name like in the case of an event we
might not want to do that One of the downsides of working with names
which I'll cover a bit later in general,
is the fact, that it can collide with existing names.
In fact, some malicious entity might decide to grab,
a hold of an object because the name is fairly easily
visible in tools such as cross explorer,
and In fact, can be obtained by using some APIs So for now,
we don't want a name for the object.
In fact, this is what we want to start with an unnamed object.
And then we would like that handle,
to be duplicated into the newly created process.
So that's the idea.
Now, by default, this will not happen because that handle,
is not marked as inheritable doesn't have the I flag,
that We described in the previous module So in order to change,
that bit that inheritance, bit to One there,
are two ways of doing that One of them,
is to call the function set handle information.
This is where you can specify the IP properties we briefly talked
about in terms of the properties of handle.
So we have the handle, to the event,
and the mask indicates what which of these flags would like to change.
So we have H handle flag inherit here which is what we want.
And then The value would like to provide for this flag.
In this case, the same value.
Want to turn on that flag.
If you would like to turn that off, we will simply place a zero here.
But now we want to turn on that flag because it is turned off by default.
Once we do that it's going to work out.
So that handle, is going to be duplicated,
to the target, Notepad process, and the important point here.
Is that the handle value, in the newly created process,
in the Notepad process, in the this case,
is going to be exactly the same as it.
Is in our process.
Which is Very important.
We explain in a moment.
Why that is important.
So let me just close the handles here.
Just to be nice and Let's test it out.
See what that looks like.
And then We'll discuss why it's important.
That the handle value, is In fact, the same,
the first thing we need to realize that It's possible.
To be the same because the newly created process,
is new and its handle table, is empty.
So obviously whatever handle value we have in our creator process here.
It's available.
In the new process because the new process has zero handles,
and the point of creation.
So this is why it is possible.
To have the same value.
Why is that important we'll discuss in a moment for now.
Let's press a breakpoint here.
Let's change this project to be the start,
up project and press a five to run it.
So what I want to see, is how these things play out using tools.
So now I'm going to create the event object.
Here goes going to cross explorer.
Here.
Let's find our application.
Our process called inherit.
Here goes, and the handle value as you can see here,
is C, four that happens, to be C, four and You don't see these handle here.
And That's because we only see your handles for objects.
That have some kind of name.
And these project is explicitly nameless.
So we have to go, to the view, menu, and show, an end handles, and mappings.
Now, we can go.
To see four and this is our event right here, double clicking.
It shows that It has, there's just one handle, to This object which makes sense.
We just created it.
It has no name which is fine.
It doesn't detract anything from the functionality of the object.
It just means it cannot be looked up using some kind of name.
So we have that and Notice there, is a column here,
in the new cross explorer, that is called attributes,
and attributes, is The one that's going to show us whether the inheritance,
be is actually on or off.
And Currently we see nothing, which means that the inheritance, be is off.
It's not on and We need to turn it on which is What the next call?
Is going, to be Doing so let me press F 10, to run the next instruction.
And then We expect the inheritance bit to show up here.
However, there, is currently,
a bug in cross explore where it doesn't refresh the attributes
column unless we go to a different process,
and then go back, and Now, if we look at C four here,
you notice that Now the handle, is in fact,
has the inheritance.
Bit turned on.
So now we know that these handle, can be inherited by child processes.
If we use the parameter here as true for the inherit handles flag.
So here's our Notepad Let's let's do that So here goes,
Notepad should be coming up.
And if it doesn't.
Well, let's see.
Maybe it didn't work as I as I had Ed.
So it didn't work out properly.
And I would guess.
That That's because Windows 11 uses the new Notepad here.
So it's a Little bit of annoying annoyance here,
but let's just fix that So let's start try something else.
Maybe let's go with paint.
It doesn't really matter.
I think there's a paint here somewhere.
Let's try this again.
So again, we're creating the event object,
changing the inheritance, bit Let's go back.
To Make sure that In fact, it works as we expect here.
Is our event Let's find it.
Here it goes.
It happens.
To have the same handle, see four fine, and we can see the inheritance,
bit is on.
So now let's create the paint process.
Here goes, and we do get some pain process here,
going back, to cross explore even without switching to that process.
Just yet.
Let's look at see four just by double clicking it
once we do that we can see there, is a second handle,
somewhere within the system.
To that same, event object.
So where is that handle, supposed to be the way hand inheritance works.
Is that the handle, is going to have the same value.
In the newly created process.
In this case, that will be our paint process.
And So notice the address here, the object, address it ends with CCD, 42 E zero.
Let's now find paint.
Let's see what paint looks like here.
Is ms paint, and we expect the handle value.
To be exactly the same.
This is one of the important properties of handle inheritance which I've yet.
To explain why it is important.
So here goes the handle value, is In fact, the same notice, is the same handle.
Notice we see the exact same information here.
In the properties of the object.
And That's it.
We've made the sharing work we have now these two processes
sharing the same event object in this case,
doing that in a different way without using a name using handle inheritance.
If I go ahead and press their 52 end our inherit process here.
So now we are left with these event object, still alive.
And The number of handles has dropped.
To one,
and that handle will survive for as long as paint will survive
because paint has no idea It has been injected.
And duplicated, that handle, and of course,
paint, is not part of our software system,
it has no idea It has this handle in its hand.
And table, and it will not use it.
And will not close it either.
So the only way for that to be destroyed at some point.
Is only If paint is shutting down which indeed that would be the case.
Obviously in a real scenario, we're not going to be creating paint.
We're going to be creating another process.
That is part of our software system,
this is why we're doing that kind of sharing.
We'd like to use the same kernel object in both of these processes.
So why is that important the fact.
That the handle value, is the same the the important part here,
is that How can we tell the other process the newly
created process what the handle value?
Is Even if the other process knows it's going to get a handle,
to the same object we're supposed to be sharing.
How does it, how can you tell what the handle value?
Is going to be There's no way.
To guess, that and So because the handle value,
is going to be the same in the target process,
in the newly created process.
It's possible.
To pass, that information using the command line.
So for example, I can make some slight changes here.
Let me add an code four string,
to make it a bit easier from a C++ uh perspective.
So, I can create a W string here, to have the name paint,
and then I can add, that add, to that Uh the,
the I idea the handle value of the,
of the event So I'm going to do something like name.
Plus equals let's just do something simple like two double string,
to take the handle value, and change, that to sym,
to a simple string.
So let me just make sure.
That the compiler, is happy by uh by using a LUN pointer here,
to turn it into a number, a 64-bit number,
and then we can Go ahead.
And pass along, that information, to the to use data here,
and It still won't properly compile.
Because of a bug in C++ 14, we 14, to change,
that to C++ 17 at 17 very least or just cast that forcefully.
To Shut up the compiler.
Let me just change.
Two see plus plus 17, and now, we can pass,
that event value, to the command line.
So if the process is indeed something we're working with that
process knows to examine its command line.
And the first number it sees there.
It's supposed to be the handle value.
So you can just cast that value, to a handle, and work with it.
Of course, in the paint example, if I try this again.
It wouldn't pan out very nicely because paint is
going to give us an error saying, hey, I don't know what that 344 dot ping G,
is So we just passed in the command line for 344 that was the command line,
that We provided and paint, is interpreting that as a PNG file,
and which of course doesn't exist.
So that's something we we kind of expect because net doesn't really
know we're going to pass it some kind of handle.
But in another kind of executable where it's part of our software system,
this is what?
We can do and we can do that Because we know for sure what the handle value.
Is going to be even before the new process, is actually being created.
So that's why it's important for these values.
To be the same.
So, that we can tell the other process what the value happens.
To be