[Autogenerated] let's start with the basics by creating some simple
threats and see what that looks like in code.
Going to create a normal console application as before.
Let me call that just threats very simply,
what I want to do is to create a thread and show you what that looks like.
In terms of the APIs we can use while working with the threat.
I'm going to delete all the stuff here.
Just leave to include one for windows dot H and the other one for S T D I O.
So in order to create the threat,
we need to use the create thread function and the great red
function will return handled to a thread and of course that's if
that call succeeds and it should succeed in most cases unless
we're really low on memory.
So let's look at the parameters we need to specify or
provide the create rate function.
The first one is called the security attributes,
data structure, which is a common structure used for all,
create functions for kernel objects.
For now I'm going to specify now indicating I'd like to get default security
for the object and then we have the stack size parameter.
This is exactly the parameter I mentioned in the previous
module as a way to control the stacks size.
If I specify zero here,
then the stack size is going to be determined by the values
that are stored as part of the p header that we can change
using linker options in visual studio.
Otherwise I can use a particular number and that would be
interpreted as the initial committed size unless I use the flag
later on in in other in the flags parameter for this function to
interpret that as a reserve size, the maximum size.
So I'm not going to tweak with the stack for now.
I'm going to just leave the stack size for zero,
which means just grab the defaults from the header and then you have
the most important parameter to this function,
which is the actual code to run on a different thread of execution.
So I'm going to create a function, I'm going to call that.
Do work.
So I'm going to just write the work here and then we'll implement this function.
Of course the next argument is about value.
I can pass to this function because I might have
multiple threats running the same function,
maybe that do work function, but each one might be doing work on different data.
This is my opportunity to provide a pointer to whatever that provides extra
information so that the threat knows on which data to work on.
So for our first example I'm just going to specify null,
indicating no particular value is passed to the function
and then we have a bunch of flags.
The only two flags we can specify except for zero is create suspended,
that's one of them.
This creates the thread in a suspended state.
That's not really going to start running but it's kind of ready to go
and we have to call the resume thread API in order to free it
completely or we can use the other end or we can use the other flag
called thread Parham is a reservation.
The flag that we mentioned when we talked about the thread stack
in order to interpret the stack parameter as the reserved size
rather than the initial committed size.
So in this case I want to create the threat to start running
immediately and we don't care about the stack size right now.
So we're going to just specify zero and the last argument is an
output that's going to give us back the thread ID.
That was generated by the Colonel.
If we don't care about that we can specify null.
And there are other ways to get that idea later on based on the handle.
So now that the thread is initiated it's supposed to be starting
it's supposed to start running as soon as possible.
I can just do other stuff in my main thread.
And so I'm going to just indicate that I'm free to do
other stuff by displaying the thread I.
D.
For my main thread.
Just by calling the function.
Get current thread ID which allows me to get the thread I.
D.
Of the currently executing code.
Let's assume that I can do other stuff here while the other thread is running.
However at some point I would like to get a result from that thread and so
I need to wait for that threat to to finish execution.
Maybe it already has finished.
I don't no,
but I want to make sure that I don't move forward
while that thread is still at work.
So the way to do that is to call the function wait for single object,
which is a generic function that allows the threat to wait
until some kernel object becomes signaled where the meaning of
signal depends on the object type.
In the case of thread, signal means terminated,
the threat is no longer running code so I'm going to provide
the handle to the thread here and then I can provide the time
that would like to wait at most.
And so in this case I'm going to use the infinite constant which
indicates that would like to wait for as long as it takes.
So at this point I can definitely say that the worker thread
is done or let's just call that work done,
let's say I want to get the result,
which is going to be the return code from the thread.
And so in order to do that, I can use one of the APIs and mentioned earlier.
Get exit code thread where you can provide the thread handle and and
then we'll get back whatever exit code was provided by the thread
which is returned here as the word value.
32-bit value so we can display that code and essentially we're done,
we can close the handle so that we clean up after ourselves properly
and we're done at least with the main function.
Of course at this point we didn't write the do work function just yet.
So let's write this function Now,
every third-function always has to have the same prototype.
It must return a 32 bit value and that's going to be the exit code for that
thread and then it must have the standard calling convention.
And this is important for 32-bit For 64-bit there's just
one calling convention at least for X 64.
But for X 86 it is important.
So we get proper behavior of stock cleanup when parameters are
passed to this function and the actual function itself and the
actual name that we provided and the parameter we get as the next
value that is sent here to the thread.
In this first example nothing is sent to the thread.
So parang will simply be null.
You might see in other examples that using this mac,
this calling convention,
standard calling convention is replaced by various
macros such as Win API or call back.
Just know that there are these are the same thing.
They simply expand to the STD call um Calling convention
and so let's write some simple code here,
we can do something like worker thread and just display the thread I.
D.
Here.
So this is another way to look at the thread.
IDE but from again from inside the threat.
So get current thread ID here will return the thread idea of the
currently executing code that would be the the identify that is used
to identify this threat throughout the system.
And for demonstration purposes let's just use a single sleep function here
so that we kind of simulate the fact that maybe this thread has do is
doing something maybe doing some eye operation,
maybe some calculation and then we'll just write that it's done again just to uh
to have something printed out and let's return some value.
And that would be the exit code for this thread.
And that's it for our first example let's build
that and see what that looks like.
So once I press control of five to run it, Here's what we see.
We see the main thread has some idea.
The worker thread has some I.
d.
and after three seconds the worker is done and we get
the results of 42 as we expect.
Now it's important to know that some of these items are going to be
shown in a particular order but some other items might not be shown
in the same order every time we run it.
So for example the first two lines here might be shown in different order
because it really depends on whether the threat that we created has managed
to start running before the worker thread display happened on the main
thread and there's really no way to tell.
It really depends on what else is going on in this in the system.
So in this case we got disorder, but it could have been reversed.
However, the rest of the items here are going to be in this order for sure,
because worker done is only displayed after the
threat is done with three second wait,
and that work done will also be displayed only once the threat is over.
So the other items will be shown in this order.
Always in the next clip,
we'll see some extra customizations we can do to this code