views:

261

answers:

1

Hi

Recently I was reading about interop marshalling but I still don't understand one thing. To make .NET assembly visible to COM I need to use tlbexp tool and/or regasm tool. To make COM visible to .NET assebly I have to use tlbimp tool - all that is clear for me.

  1. Apart from that, I've seen on the internet a lot of code dealing with COM interfaces with guid attributes, IUnknown and IDispach.

My question is how all these relate to COM - do I need to use that? I am preparing to certification but they didn't say a word about interfaces, guids, IUnknown etc...

I don't really like to leave unclear things behind me, so If someone could please explain it to me, I would be really grateful.

  1. I also have one question regarding type library imported from COM (tlbimp tool). Does the type library imported from COM still require COM object registred in the system?

Kind regards PK

+3  A: 

I have no idea why someone downvoted this question. It seems a perfectly valid question to me; maybe they just haven't had their coffee yet this morning.

You seem to be asking what GUIDs, IUnknown and IDispatch have to do with COM. I will try to give a brief survey.

Every COM component exposes a common interface, IUnknown. IUnknown has the methods AddRef(), Release() and QueryInterface(). AddRef() and Release() are used to support reference counting, so that when every reference to an object has been released the object will be destroyed. QueryInterface() is kind of like COM's version of the dynamic_cast<> operator in C++. It is used by client code to see if the IUnknow pointer they have actually points to an object of another type, like IDog, ICat, or whatever.

So every COM component must implement IUnknown, but most COM libraries will implement this for you automatically so when you are writing COM code you typically don't have to write any code to get IUnknown; you get it for free.

GUIDs are kind of like fingerprints. Just like in real life two people can have exactly the same name, in COM two COM components can also have the same name. For example, you can have 2 libraries that both implement the IDog interface, but they may do totally different things. You should still be able to have both libraries installed on your machine however, and distinguishing between the two is what GUIDs are used for. A GUID is a Globally Unique IDentifier, meaning that when you generate one, in theory no other person on Earth at any othher point in time will ever be able to create the same GUID. So in addition to a name, every COM object (and coclass, and library, etc) has a GUID.

IDispatch is another base interface much like IUnknown, but unlike IUnknown which is required for every COM object, IDispatch is optional, and provides special functionality that many but not all COM objects support. Things like supporting certian language features and making your object easier to use by clients. Most (probably) COM object expose this interface.

Regarding COM libraries; yes, they must be registered in Windows.

John Dibling
So when I am writing COM component it is enough to create class library project (with public members and default constructor) and later when using tlbexp tool (or regasm), IUnknown and GUID will be generated automatically?
pkolodziej