November 28, 2020
Hot Topics:

VB6 Programmer's Introduction to COM

  • By James Limm
  • Send Email »
  • More Articles »

It's about time we began to consider how COM solves these issues and provides this kind of functionality for us. The beauty of using Visual Basic and COM together is the combination of the two makes it quite easy to address the problems without too much effort on the part of the developer - Visual Basic does most of the hard work for you!

COM-based components promote software reuse. COM components are usually written to be generic, which allows them to be used in a multitude of ways by different pieces of software. A graphing component, for example, could be COM-enabled so that you could use it in whatever container you choose. You may want to put a graph in your Microsoft Word document, or you might want to put it on a Microsoft Access form. The great thing is that you can use it in either application, and it will work the same way in one as it does in the other.

Looking at the pair of screenshots below, you can see that the Microsoft Chart Component looks and acts the same in both applications. This is very productive because if a developer knows how the chart control works for one application, they are likely to know how to make it work in another application.

As to the problem of being able to find your components after you've written them, COM has a solution that deals with this and more. Every time that you create a COM component with Visual Basic, an entry is placed in the system registry that indicates exactly where on the computer is the file containing your component. In fact, COM goes further than this: it requires that in order for a component to be used, an entry for it must exist in the registry. Without such an entry, the COM runtime will be unable to find the component when it is asked to do so.

This theme will be developed in the next chapter, along with a more complete description of how the registry works.

COM components are language independent. COM components can not only be used in Visual Basic, but also written in Visual Basic. More importantly, you can take a COM component you created in Visual Basic and integrate it into your Visual C++ or Visual J++ project. Although you can't create COM components in Visual InterDev, you can certainly use them by calling them from Active Server Pages. At the moment, language independence is typically limited to the programming languages Microsoft supports, but this is changing as the momentum for COM continues to grow.

COM objects have object-oriented features: obeying the strictures of encapsulation, they are accessed using only public methods and properties, so that any data they contain is hidden from public consumption. However, object-oriented programming is a tool of this book rather than its subject matter, so I'll limit any further discussion of it until it is relevant to the matter at hand. I suggest reading Beginning Visual Basic 6 Objects by Peter Wright (Wrox Press) as a good primer in creating and using software objects.

Time for me to get on my soapbox. If you haven't been to a Usenet newsgroup with "oop" (short for Object-Oriented Programming) in its name, you may not know that there seems to be something of a Jihad over OOP. The purists will tell you that all the software you write must adhere to the concepts of object-oriented programming.

For my part, I like to invoke the old saying, "When your only tool is a hammer, everything looks like a nail." I am not against OOP; indeed, I am quite happy that Visual Basic uses classes and encapsulation - it has made it simpler to develop applications, and it makes debugging much easier. I do think, though, that Microsoft is beginning to push the envelope (yet again) and reappraise the argument that says you should use object-oriented development just because you are supposed to. I am sure to get flame mail on this one, but I think it is better for the developer to decide how they want to design their programs based on what their users require, rather than on an ideological point.

Another great feature of COM is called DCOM, or Distributed COM. This was first shipped with Windows NT4, and was released as an add-on for Windows 95 in late 1996. It also ships as standard with Windows 98. DCOM allows your applications to call upon components that may not even be on the same machine as they are. DCOM takes the notion of location independence that's supported by COM's use of the registry to store the locations of components (provided that there's an entry, it doesn't matter exactly where the component is) and extends it to the network. Furthermore, it provides security for components that are distributed in this way. I will go into more detail on this subject in the DCOM chapter.

You should be able to see from this discussion that there are a number of advantages to using COM and DCOM as platforms for building and using components. COM goes out of its way to provide a robust and powerful environment for your software development needs.

Page 9 of 12

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date