I need to learn COM for my current project. I am a noob and I haven't been able to find good starting points for COM. I have been looking into MSDN and googling... I did find some interesting articles on codeproject.com, but I am not satisfied. Why do we use COM ? Why does it exist ? In what places does it exist ? ..... and so on .. So, will you please tell me where can I find answers to these questions ?
- Firstly, COM is just like object-oriented programming. COM interface is an abstract interface.
- COM has an advantage over each language's own built-in OOP in that you can have COM classes implemented in different languages and/or compiled by different versions of compilers, and they will still be able to inter-operate. This is called "application binary interface" (ABI)
- COM uses reference counting. It might be annoying at first, but as soon as you learn about smart pointers, your life will be much easier and you will find that the concept of reference counting is very easy to understand.
- COM uses a weird way of class casting, namely "Query Interface".
Learning the IUnknown is the first step.
A good COM interface that is suitable for beginner's study is the IStream interface. You may practice COM by implementing the IStream interface using many kinds of ways of storing data.
After the COM mechanism was invented, people at Microsoft discovered that these mechanisms open the door to unusual programming tricks which are not orthodox OOP. At the lowest level,
IUnknown.QueryInterface is just a method; it can do anything. These tricks make COM intimidating to learners, because they can invalidate the basic reasoning that can be borrowed from OOP.
The Microsoft documentation at MSDN spends a lot of time saying "COM is not ..." or "Make no assumption" explaining how these unusual tricks can break your code.
COM designers use Liskov Substitution Principle (the "L" in SOLID) to make sure their multiple interfaces and use of tricks do not invalidate basic OOP reasoning or program operation.
If your project does not use these tricks, it is okay to see COM just from the OOP point of view. If your project uses these tricks, you will need to see COM as self-modifying code.
(Joke: We had to spend so much time with L that we typically ignore the four other principles.)
The main reason for COM is to provide an object oriented communications interface between an application and other applications and dll's that may live in different memory spaces, and between objects that may have different internal object representations.
Something to remember when learning COM is that at the time C++ was king and applications called dll's using C. There was no common ABI (as @rwong says) and COM was the microsoft solution.
It's probably worth having a look at Active Template Library (ATL) for visual studio which makes it a bit easier to use COM as well.
A couple of books I found useful back in the day
I originally learned about COM from the book Learning DCOM (DCOM is just an extension of COM, the book is mostly about COM). It's over ten years old and should be cheap if you get it used (since COM isn't exactly hot anymore, not even in the Microsoft world, even though it's still used for interop).
The main reason for COM to exist is to facilitate component-based development, which basically means that instead of linking libraries when you compile your code, you reference an interface (the description of how to communicate with an external component) and use some implementation of that when you deploy. It underlies most of Microsoft's own technology.
The system has support for some features that were cool before everything went managed (such as reference counting and self-description (through IDispatch)).
COM is a standard created in the 90's to allow different pieces of software to talk to each other. Most important is the binary standard that allows one piece of software create or bind to an object and then call into that interface. This allows you to do things like create a DLL in C++ which can be called by an ASP page (COM Objects), or create a control in Delphi which you can put onto a VB dialog box (ActiveX controls), or Excel worksheet. When you paste a picture into Word it uses COM too, embedding a separate application into word. Less popular is DCOM which is to call remote objects, there are usually simpler ways to do this.
So Com/ActiveX/DCOM was very popular 10 years ago with VB, VBA, C++/ATL, Delphi. Dotnet is much simpler though and C# has replaced most use of COM and VB etc
Still there are many COM interfaces in there which exist which you might want to call from a dotnet application or Java or C++. (You dont say)
If you need to call COM interfaces VB is the simplest way to do this. In Dotnet 4.0 C# has the dynamic keyword which makes it much easier to call IDispatch interfaces, which is probably a great solution you should look at.
If you need to know a lot of detail, you really need to use C++ and ATL, call the C APIs like CoCreateInstance. Avoid this if you can though as its old tech and isn't much use, stick with recommendations above.
I'm not sure what monitoring software you're looking at but its likely to be available via System.Management classes in dotnet.
The followings could be useful COM overviewM:
The first video talks about the general idea of creating Binary Components to reuse codes written in different languages (language-independent) and are in different machines (processes) (location independent/transparent). This technology is used in some Software Companies to integrate with other software (companies) instead of buying their software and embed them into their own or writing the codes from scratch in their systems.
This technology has three different versions (implementtaions):
OLE/COM/DCOM/ActiveX for C++ developers (ATL is used to make programmer's job easier)
JavaBeans/RMI/EJB for Java developers
Just think of it as a technology that comes handy when you have your software written in Java and you want to add a functionality to it that is already exist in a package but it is written in C++ and even perhaps is located in different machine not in your local machine. How do you call "new" , how do you make objects and call methods from them?
The following books that I have seen people who do COM coding have either of them in their bookshelves:
Developer's Workshop to COM and ATL 3.0