Tuesday, March 12, 2013

Learn complex COM (Component Object Model) easily

Hearing the word COM, I always stayed away from it, as I felt it is something very difficult to understand even though I am good at C,C++,C#. The problem is Microsoft doesn't provide an elegant way of learning things i.e., 'Hello World' program in COM. That's the biggest problem with Microsoft. Although it has an edge over all the technologies, the reach is very less. There is so much data on COM in MSDN, but I bet no early birds can learn and write a COM implementation of their own. Because they are not organized properly and not presented in a easy understandable way. I will scribble more on such problems with Microsoft in an another blog.

Okay, now lets come to COM. COM is nothing but Component + Object + Model. We can remember it like it is some way in which (class) objects are implemented and accessed. So this tells me someone has implemented the class somewhere and someone else uses those class's objects to get this done. Now you got it :-) There are 2 main things in COM

1. Implementer\Owner\Producer of the Class
2. Consumer\Accessor\Customer of the Class

Now comes another question.. How do these both interact? Hmm.. now we remember that we have learned something called interfaces. So these both producer and consumer communicate via Interfaces.

Now comes an another question .... :-) We already have interfaces in our C\C++, then what special does COM plays here? Hmm.. here is the advantage ... COM is language independent :-) Woww that sounds awesome isn't it?? So the producer and consumer can be developed in any of the programming languages C or C++ or C# or Visual Basic or Objective C or Java ... etc.

Now comes one more question....:-) :-(. So how does COM handle language independent communication between producer and consumer? Here it is... COM handles it in a binary way .. bits and bytes :-) So any code that you write finally gets converted into EXE or DLLs, which is nothing but bits and bytes. So following some rules you can make your producer and consumer work together via COM in a binary way.

Now comes the next question :-) ... So what is that rules that has to be followed? Here it is...
1. Create an IDL file (Interface Definition Language)
2. Compile the IDL file with midl.exe(available along with Microsoft SDK) and this will output several files for consumer and producer.
3. Now write the producer and consumer class and use the corresponding output files generated in step 2.

That's it now you are ready and your COM basics are done in a outlined manner. 

Now where can I use COM? When you have two applications one sitting in a computer and another in a other computer, then they can communicate via COM. Hmm.. you got it now... COM is an language independent interprocess communication mechanism... :-)

Yehhh.. so got the grip on COM.. now go read the books on COM to dig deep in it.. :-)

rahul sundar