COM anyone?

Started by
9 comments, last by ByteMe95 23 years, 10 months ago
Hey. I''ve been programming C/C++ for around 5 years, I just moved to VC and DX a little more than a year ago. I see that DX uses COM, but I''ve never really used it because I dont have a clue as to what it is. Can anyone explain very briefly what exactly COM is and what it does? Bare in mind I have NO clue, so please be as descriptive as you can without getting too technical
ByteMe95::~ByteMe95()My S(h)ite
Advertisement
Briefly? The DX documentation does that.

In more detail, get Dale Rogerson''s Inside COM.
VK
The DX docs do a pretty bad job explaining it, and I''m not out to get a whole book and dedicate a good part of my time figuring it out (is it worth it to understand this topic? Cause i dont even know what it is)
ByteMe95::~ByteMe95()My S(h)ite
Briefly, the idea of COM is the same as using classes: provide a standard non-changing interface that lets you make calls to an object without worry about the underlying processes that the object uses (black box).

You design the interface(s) to the object, and the interface is set in stone. You should never change it. If something does need to change, you add a new interface and use the new one instead.

What is an Interface? It's basically what determines how you access functions within your object.

Take a look at a DirectDrawSurface for example -
There is a DirectDrawSurface4 interface, and a DirectDrawSurface7 interface. They both use the same COM object, but have different interfaces. The DirectDrawSurface4 .Blt function has different parameters than the 7 version.It's sorta like versioning. The DDraw.dll has been updated many times, but people can still use the older legacy interface IDirectDraw4 if they want to without any problems.

Other things include built in stuff, such as reference counting, etc.

You might understand QueryInterface better now that you realize that you are asking a COM object for a certain Interface and all calls through the object that is returned use that Interface.

A COM object of course has Methods, Properties, and in addition, has Events. An event might be triggered externally by another object, for example. It's basically the same as a Method, and is sometimes difficult to understand.

My suggestion is to go buy a good book about COM.

The other nice feature about COM is that any COM objects you write can be reused in any language that supports it. VB, Java, C++, Delphi, etc.

Clay


Edited by - Falagard on June 29, 2000 3:11:05 PM
Clay LarabieLead DeveloperTacendia.com
COM is a binary standard that allows objects to be used by all programs no matter what programming language the application or object where written in.

If a standard DLL was developed in Visual C++ and then used in Borland C++ you would run into problems because the way VC structures DLLs is different than BC++. Now with COM all languages share the same binary layout for DLLs and can therefore use DLLs written by another compiler or another language.

Applications can get access to objects throught interfaces which the object implements. This can be compared to an abstract class which has a number of pure virtual functions and relys on the derived classes to implement them. Each derived class exposes the same abstract functions, or interfaces, but each may implement them differently. This way you can gain polymorphism.

Check out "Learning DCOM" published by O''Reilly. This explains COM/DCOM, MIDL and how components can be used on web pages.

Regards
Michael
I''m not sure what you mean by Binary standard. If you mean that the WAY programs access a COM object is the standard, then yes. Other programming languages can call any type of COM object, because the way COM objects are accessed is the same no matter what language the object was developed in.

You can develop a COM object from a text file, just name it .wsc
Try taking a text file, name it .wsc, and right click it. One of the options you''ll see is "Register". (Only if you are running Win98, NT with latest service pack, etc. because you need to have Windows Scripting Host installed on the computer).
In the text file you write script - similar to in a web page.
You define the type of language you want to use, (java-script, VBScript), define the Methods and Properties of the object. Write the code. At any time you can register the object, which stores the information in the windows registry. This script file is now officially a COM object, which can be used from C++, VB, Delphi, Java, etc.

So it''s not the binary code that has been standardized, it''s the way you make calls to the object.

Clay
Clay LarabieLead DeveloperTacendia.com
Hrm, I just re-read your post and I see that''s what you meant. But I just thought I''d use my script example anyhow :-)

Clay
Clay LarabieLead DeveloperTacendia.com
I dont have any of my code in front of me, but I''m pretty sure in the DX games I program I dont use COM because I dont remember calling CoInitialize and uninitialize and I dont use QueryInterface.

1) Is that possible? I might be wrong, i dont have my code in front of me
2) If it is possible, is there a difference whether I use COM or not? What are the pros/cons if any
ByteMe95::~ByteMe95()My S(h)ite
If you''re using DirectX 7.0 then in simple programs you may not use QueryInterface much if not at all.

But, in DirectX 5/6 you had to use it to gain access to the more recent interfaces because the functions to create the objects only returned the first interfaces implemented. In DX 5/6 DirectDrawCreate returned IDirectDraw and you had to then QueryInterface(IID_IDirectDraw4, (LPVOID *)&var). But in DX 7 you have DirectDrawCreateEx which will only return IID_IDirectDraw7 interface.

As for using COM, you''ve got no choice. If you don''t get down to the ins and outs of it all, then its just like using C++ objects !!!

Regards
Michael
OK, I think I understand this COM thing a lot better now, thanks. So From what I understand, in order to use the latest DX u would have to use COM in order to get it''s interface (with the exception of DX7 as stated above).

So is there any general rule on whether or not COM should be used. I mean what''s the overall difference if I do use COM than if I dont, how will it affect my application?

And in general, what do most "professionals" use in their programs? COM or not.
ByteMe95::~ByteMe95()My S(h)ite

This topic is closed to new replies.

Advertisement