RoxxSoft Development Blog
Posts tagged Visual C++
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…
Simple Win32 application with source code
Oct 2nd
My last post generated some mail about how to use a .rc file from inside Visual C++ 2008 Express, so i decided to post this little example of an application made using resedit, basically what is contained here is a .rc file, a cpp file that uses it, and a resource.h file which contains some definitions.
I’ve used resedit because at least for me, it is as good as any Microsoft application, and it is free, fast, small and i like to work with it, of course you can choose whatever suits you best, but resedit is the best for me. :), if you don’t know where to find this app, please have a look at my post about resource editors, where you can find links to the most popular ones.
To recreate a project like this one, first you need to create an empty project in Visual C++, then, just add an empty .cpp file, then, in resedit, generate at least one dialog, with all the controls you need, save the .rc file into the same folder where your Visual C++ project is, then import the file into the editor by right clicking over the Resource Files section in the Solution Explorer, then select your .rc file.

If you generate the c++ code from inside resedit, only import the resource.h file, forget about the generated .cpp file, after including those files, all you have to do is write the windows procedure and the windows main function and that’s it, the zip file i have linked here has a very simple win32 application that does all these things, if you are interested to know how to build a simple application without too much effort have a look at the code sample, i was going to write a little tutorial about win32, but, the app is so simple that i believe it is better to just explore the code and use the msdn docs to learn about how it works.
After importing the .rc file into Visual C++, you can still change it from resedit, everytime you save your changes, the Visual C++ editor will notify you and allow you to reload the .rc file, this makes it very easy to keep working with both tools open and without locked file conflicts.
Click here to download the code: win32App.zip
Hope this is useful to you, if any doubt comes along, you are free to ask :), i will try to help but please remember it may take some time to see your questions since i don’t check the blog everyday.
Depending on the free time i have, i am considering writing some tutorials about win32, but i think there are some good ones already on the web. anyway, maybe if enough people are interested i’ll do it.
Visual resource editor for Visual C++ Express
Sep 26th
As mentioned in a previous post, after installing the Express edition of Visual C++ 2008, i found out it didn’t had a visual resource editor, while the c# express compiler does include one, so i started to look for an alternative and after looking at various commercial apps i found some free ones:
Very cool resource editor, free, not open source though:
ResEdit
This one includes the complete IDE, it is open source and seems to work with several compilers:
http://www.ultimatepp.org/
Here is another one, seems ok, not tested:
http://www.wilsonc.demon.co.uk/d10resourceeditor.htm
And of course there are a couple commercial ones, i found this one to be good:
http://www.resource-builder.com/download.html
But i did not use it so much to have a real world opinion so…
While the Visual c++ 2008 express edition seems to be targeted to students or beginners, i found that very hard to believe, without the resource editor, how are they supposed to get up to speed?, by programming like 25 years ago? I guess it is in microsoft’s best interest to move everyone into using c# as soon as possible.
Win32 and Visual C++ Express Edition
Sep 15th
I’ve been working with the Win32 api for a very long time, even for my client’s apps i tend to use Win32, i don’t like MFC, i have made several applications with MFC and Windows Forms, but i still keep working with Win32 whenever i have a chance. In fact i usually work with STL instead of any other c++ library, why? well for a start, it is easier to port the code to another operating system, and second, i don’t like to bloat the app just because i don’t want to leave the comfort of the new “modern” APIs.
Yeah, i know that’s not the best API, or the coolest one, or anything fancy at all, but i like the speed and the small size i get with applications made using just Win32.
A few days ago i decided to try Visual C++ 2008 Express Edition, and i realized that it only supports Win32 applications, and to my surprise, no resource editor, so how new programmers who are interested in learning to program C++ applications will do without the resource editor? is this version of Visual Studio for students and beginners? or for hard core programmers who can really understand the Win32 API inner workings as they have spent several years learning every function, message, parameter and structure supported by this api?
I’ve tried Visual C# 2008 Express, and of course this one does have the resource editor, you can add controls, forms, format and set properties all using visual controls, i guess Microsoft doesn’t want people to actually keep using C++ and instead they want them to go to C#.
Otherwise, i don’t see a reason to remove the resource editor from Visual C++.
Anyway, i have decided to keep the Express edition installed and implement some free apps using this compiler just because well, it is free, it seems fast enough, and i don’t care about the resource editor or any other stuff like that, i like to program like in the old days, if anything just to complicate my life and spend two hours building a user interface that i could get done in 10 minutes using the professional edition, or Visual C# Express.
Life goes on..

Recent Comments