RoxxSoft Development Blog
Posts tagged c programming language
COM Basics Tutorial Part 1 – C++ function pointers
Nov 9th
In these series, i will try to show how COM works at a very low level, which some may read as in a very complex level, which fortunately for us all, is not as true as you may have heard before, the only requirement for this tutorial is to have a basic understanding of the C and C++ languages, everything else will come with time.
But first, let’s get a quick look at a little app, we are going to be using this code to show how and why things are like they are with COM, but first we will start with a very simple application that shows what function pointers are and how a class is actually made, this of course mean we will be working with C++, COM can be used from C as well, but i tend to use more C++ so that’s what we are going to be using here.
You can download the code here: ComTutorialApp.zip
Once you have the code, open it with Visual Studio 2008, you can use the Express edition if you don’t have anything else, this is a win32 application so you won’t need anything else like MFC/ATL or stuff like that, for the time being, you can get away with the Express edition.
OK, so you have the project open, if you go and open COMTutorialApp.cpp, you will find it has only the WinMain method, which has been cleaned up so no actual windows application is created, we don’t have a window or anything else but the code related to our basic COM tutorial.
Now i will explain what this code does, first you can see we have defined some types at the start of the file:
typedef VOID (*PROCADDR)(); typedef int (*INTPROCADDR)(); typedef int (*INTPROCADDRPARAM)(int);
This is our way to define the signature of our methods, as you can see, we defined a type for a method with a signature similar to:
void SomeMethod();
Which is a C method, for now that will be enough for our purposes, next we define two more methods, one that returns an int, and one that returns an int, and receives an int as unique parameter.
Next, we find a new definition, this time the type we are defining is that of a structure:
typedef struct _object
{
PROCADDR Method;
INTPROCADDR MethodInt;
INTPROCADDRPARAM MethodIntParam;
}Object;
I have named the structure “Object”, just because i like to steal the names from C# :), so you can see that inside the structure we have three members, one for every type declared at the top of the file, if you look closely at the definition of the types you will see that these are pointers, think of them as templates that define how each pointer is formed:
typedef VOID (*PROCADDR)();
So you can see this type has the form of
void (pointer-to-something)(emptiness);
OK, so now we have a structure which contains three member variables, each of a different type, we could also add another variable like an int or char or whatever we want, this is a regular structure remember?
Now we define three standard methods:
void MethodImpl(); int IntMethodImpl(); int IntMethodImplParam(int i);
If you look at the signatures, these threee methods are exactly the same as the types defined at the top of the file, you can see we have the void method, the int method, and the int that accepts an int as parameter, so we have a method for each of the types we created first, these methods are going to be called later on.
Also, we have a different method, this one accepts a parameter of type Object*, as you will see, this method can use the object as if it was a C++ class object, no difference here.
But let’s get to what this post is all about, on the winmain, we create an object using our structure as type:
Object* obj = new Object();
Then, as we have already declared three methods, and since we know the compiler already has assigned addresses to those methods, we can do something like this:
obj->Method = &MethodImpl; obj->MethodInt = &IntMethodImpl; obj->MethodIntParam = &IntMethodImplParam;
And now the member variables in the structure point to the address in memory of the methods declared before, this works because the types used to declare the member variables are equal to the signatures of the methods we are using.
So now that we have the structure members pointing to some method addresses what can we do? well we can call those methods like this:
obj->Method(); obj->MethodInt(); obj->MethodIntParam(1);
See what happens here? so now we have created a simple C++ class, of course it is actually a structure, but that’s exactly what a class is, at least at a basic level that is.
Now we can pass our obj object to another method as a parameter:
ObjectCaller(obj);
And from inside this method, let’s call again the three C methods:
void ObjectCaller(Object* obj)
{
// Calls the methods
obj->Method();
obj->MethodInt();
obj->MethodIntParam(1);
}
And that’s it, basically, this is a C++ class, created by using a structure and some pointers to methods or pointers to functions, whatever sounds better to you, how is this useful for COM do you say? well, for a start, COM objects relay on something called the dispatch table or vtable, which is very similar to what we have here, using this table is that you are able to call C++ methods for each object you create.
On our second part, we will look at how the “this” pointer is created, by updating our Object structure to support the “this” pointer, we will be able to accomplish the same results that a C++ compiler gets, but we could even be using simple C functions instead of classes, and a C compiler which doesn’t understand anything about C++.
Once we have seen how to implement the “this” pointer, we will be ready to start learning how COM is implemented and the logic behind, which at first can be very simple to understand, until we get to the tricky parts of threading models and some more complex things, but remember, this is a basic COM tutorial, not a COM bible.
Well, we are at the end of the first part of our COM series, hope you have learned something today, and of course you are free to ask any questions and comment.
Next part coming soon…

Recent Comments