[Autogenerated] Let's look at the third option of
sharing kernel object between processes.
So I'm going to create another one, another project here.
Let's just call that duplicate, and the purpose here.
Is to demonstrate duplicate handle.
A P I which we've seen in fact, a little bit earlier.
In the course just uh as a way, to look at some interesting aspects of handles.
But now we're going to use that in its sort of
intended purpose which is to share, and handle with an existing process.
This is the most generic mechanism.
It always works, but it has simple downside which I'll I'll describe.
So first, let's start with some kind of object we would like to share again.
Let's stick with an event but it works.
In the same way with any kind of kernel object,
and Of course,
we don't have an net in either because we can't have a name
for that object type or because we don't want to use the name
for some reason such as the fact, that it,
is very exposed, and We'd like to take that handle,
and duplicate, that into some target process.
That We're cooperating with and that process exists already.
If it doesn't exist.
We can create it.
And use handle inheritance.
But in this case, the scenario, is that the other process.
Is already live, is already running and we just need to inject,
a handle into its handle table, that would point,
to the same object as we're pointing it right here.
So in order to do that we need to call the function duplicate handle.
The duplicate handle has some requirements.
The first thing it requires is the source process.
So the source process, is our process or get current process.
Is the source process that's returning the pseudo handle pointing,
to our process.
The source handles also obvious that's H event That's the thing we would like.
To duplicate in some way.
To the other process.
But now we need to provide a handle, to a target process.
So in order to do that We have to first call the open process,
API to obtain a powerful enough handle, to the process in question.
So in this example, OK, let's go grab a paint again as just our guinea pig.
Here it doesn't really matter.
Again.
In the real scenario, that would be a process,
that is part of your software system.
It's not going to be some random process.
But now in order to kind of save time.
And make it simpler.
I'll just share a handle or an object.
To be more size with these paint instant.
So we need to open, a handle, to the process of paint.
And that requires an access mask, and the AX access mask,
that we need to specify here must be the very least process.
To handle.
That's the power We need to have over that process,
in order for handle, duplication,
to have a chance of succeeding this is necessary as a Counter example,
if you try to open, a handle, to a protected process,
for instance, that will not work,
which means you cannot share a kernel object with the protected
process because you cannot open a handle with process.
Loop handle, access mask from user mode to a protected process such as CSS S S,
ms S, and other protected processes.
We can specify what we'd like the inheritance handle,
uh sorry, bit to be in the returned handle which is not important.
In the created handle later on the handle.
To the process here, and then the process I idea of course, is critical.
So let's just hard.
Code it by looking at paint, let me go.
To the details here.
I'll find the ms paint, and see what its process.
ID is That's 3564 Let's just 356 64 just hard,
coded Of course, you might use other techniques.
To to Figure out the process,
ID You need to to share stuff with and If that succeeds.
Then we can Continue by providing H process here.
To the call to duplicate handle, and then we get the result which is the handle,
in the target process.
Let's just call that HT target,
we need to provide the address of H target in
order to get that information back,
then we can specify the desired access for the newly
created handle for the duplicated handle.
If we specify in the last parameter, these option,
call duplicate same access then we get the same kind of access mask,
that we have for their regional handle.
But if you don't specify that you can specify an access mask,
which might be not as powerful as the original one.
So let me use the synchronized access mask,
which is a generic axis mask indicating that You can do
weight for single object on the object, but you cannot do something like set,
event for an event object.
You cannot modify the state of an event object,
just to make things a bit, more interesting.
So if that succeeds, duplicate tender returns, two then we can say great.
That We have succeeded and the handle value itself,
is going, to be whatever we got in H target.
Now the important thing, to note, is that H target,
only has meaning in the paint process.
It has no meaning in my process.
However, the target process painting this example has no idea.
That handle, duplication took place.
And has no idea what the handle value.
Is and this is Exactly the problem with duplicate handle.
Although it's fairly easy.
To work with the problem.
Is that How can you tell the other process what the handle value happens?
To be So that's really the problem here because you need
to have some kind of mechanism already built.
In to allow communication with the other process.
Maybe you already have some kind of mechanism set up,
maybe be using Windows messages, maybe be using sockets,
named pipes, shared memory, whatever you must have something,
you must have some way.
To pass, that information, to the target process because there's no way,
to guess what?
That handle value, is going to be it's unlikely.
To have to be the same value as your handle because
that other process already alive.
And So that handle, is likely to be probably already used.
Uh But in any case, even if it happens, to be the same value,
it still doesn't change anything you need.
To tell the other process somehow what the value.
Is and This is really the practical problem with duplicate handle.
So first let's see if that works as we expect.
So let's run this.
Let me just change the project here.
To duplicate being the start.
Up project not the previous one.
So we are about to open, a handle, to the to the paint process.
So notice we have a handle, to the event has the value.
See eight Let's just check it out here using our duplicate process here.
So we have a handle with the particular value.
Let's find it.
See eight Here it goes.
You.
Can see that the AX access mask.
In fact, says event all axis which means that We,
when we create the object,
we have all the power in the world for working with that object,
which is the usual case.
So we have the AX access mask,
which allows us to do anything we want with the object.
Double clicking just verifies.
We have a single handle,
to this object right now which call it was just create at this point.
Now we start the process of duplication.
So let's see.
So first thing, let's open, a process handle,
to the to the paint process, and It works.
We have the value CC notice, that Here's the value of the handle.
CC, and Here's the process handle.
In fact, cross 64 is nice enough.
To show, that It does point, to our paint process,
and Notice the AX, access mask, is duplicate handled,
that is all that I asked for this is all that I got back.
It's possible.
To ask for more perhaps, but I don't need anything else.
In order for me.
To be able to call duplicate handles successfully.
So.
Now, that we have the duplicate the handle process working.
Up correctly for us.
We can call duplicate handle, and see what we get back.
So here goes, it succeeds.
And the handle returned is 900 CSS so let's go back.
To our cross explore here.
Let's first double-click On the existing event handle.
From the original process you can see the number of handles has grown.
To two, and again, we can verify,
that in the Other process we have the same object by memorizing this object.
Address Here.
Let's go.
To the other process.
That would be our paint process.
Here's the paint process.
This is the process we're supposed to be cooperating with and the handle value,
that We've seen is 900 in the Original 900,
has no meaning.
900 900 must exist.
And 900, to the same event object.
So here's 900, let's find it.
Here it goes.
Here's our event Notice the address,
is the same address Also notice the access is Just
synchronized exactly what we have specified if I
double-click we can see We have two handles now.
So this is one handle, and the other one,
is in the original duplicate process, and just like in the Previous example,
if I go ahead and continue running by closing the handle.
To the process, and to the event then Of course,
the event handle, is closed here, and Even if it doesn't,
when the process shuts down, it will be closed automatically.
And at this point.
TIFF I just double-click the existing event in paint,
we can see the number of handles has been decremented by one and
just like in the Previous case paint has no idea.
That that duplication took place.
So I'm not going to use that event handle.
In any way, it doesn't really know what to do with it.
It doesn't really know that it exists in fact,
and So it will continue to live, in the process until pain shuts down.
That's the only way for that handle, to be closed,
and the object, to be destroyed.
So obviously, this thing matters when we're working with another process.
That we're cooperating with and Again, the problem here.
Is how to tell, that other process.
Hey, I've performed the duplication.
And Here's the value I got.
In this case, 900 hacks,
you must already have some kind of inter process communication
mechanism set up ready with that other process.
So duplicate handle.
Is very easy.
To use from a technical perspective, just a single,
call to duplicate handle, but it is the most difficult one.
To use from a practical perspective because you need to tell the
other process what the handle value happens.
To be after the duplicate handled.
Call