RoxxSoft Development Blog
Posts tagged component object model
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…
COM without MFC/ATL/Whatever
Nov 6th
I’ve seen a lot of programmers lately that write COM as one of the things they are experts at, but, once you actually put them to the test, they turn out to know the bare minimum of what COM is and how to use it, and why is this? most of them turn out to be Visual Studio programmers, not actually C/C++/C#, etc programmers.
By Visual Studio programmers i mean, those who can only code with VS, take them out of the environment and they are completely lost, ask them to create a windows application without the application wizards and it’s a no go.
Of course most of the people would say, it doesn’t matter, what are the chances that you end up having to work without VS?, yeah i know, if you know where to look for work, you will never find yourself under those circumstances, but how sad that is for anyone who sells himself as a true professional?
Anyway, i had a rather funny situation in which i wrote a c# com object and loaded it from c++, one of the programmers was very impressed, because as far as he knew, you could use c++ from inside a c# application, but not the other way around, so i explained that the c# object was a COM object so there was no problem loading it from c++, but he could not really get it, and why? because funnily enough, he thinks COM is a c-only technology, well now he knows better, but wait, why was this guy hired in the first place? as a windows programmer, to lead the development of a windows application that uses COM as one of the principal technologies with an Office add-on and IE plug-ins and a lot of COM related programming, but he doesn’t really understand anything that is happening, VS does all the heavy work.
So i though about it and decided to write a very simple IE plug-in using the Visual C++ Express edition as with all the code on this site, by reading the code of this plug-in you will be able to see how a COM object actually works without wizard generated code or anything else but what you will find on the source files of our project.
When i say, COM without MFC/ATL, what i mean is, most of the people i know that use COM think it is somehow heavily related to using MFC or ATL, when in fact it is not, there are even those who for some reason believe COM == OLE, how can this happen, for one i think Microsoft’s documentation about COM is very hard to understand for beginners, and second, by using tools like VS most of the knowledge developers could get is taken away from them by the wizards that write all the code and just leave the developer to fill in the methods, which leads to developers who depend on the generated code since they just know that by filling those methods things will work out ok, without having to understand how or why.
So, for those who really want to learn the how and why, I’ll be posting a simple tutorial on the very basics of COM, and i mean really very basics, starting from how a class is made and virtual tables and pointers to methods, interfaces, etc.
Now i would like to ask all windows programmers out there, how much do you really think a software developer needs to understand about windows technologies to describe that person as an “expert” ?

Recent Comments