.NET is the heart of Microsoft's technology today. But before .NET, there was COM, the Component Object Model. COM was king for over fifteen years and is the technology core of Visual Basic 6.0 and VBA (Visual Basic for Applications). But COM isn't dead. It's still in leading edge products like Expression and Sharepoint. COM will be with us for a long time. Even if your organization has clear plans to move to .NET, you will likely need to upgrade gradually and that will mean making COM programs and .NET programs work together in the same system.
The problem is that COM and .NET use very different interoperation models. Getting them to work together can be a tough nut to crack. Microsoft has made it fairly easy to call a COM object in a .NET program. Part 1 of this series showed a simple example of calling Excel 2007 from a VB.NET program.
Going the other way, that is, using .NET objects in a COM based architecture, isn't nearly as easy. (One suspects that Microsoft is trying to make it harder to use VB6 so people will move to VB.NET.) But it is possible and this article shows you how.
A simple "Hello World" example might demonstrate that calling .NET from COM is possible, but it wouldn't really show that you can do anything worthwhile. So the example in this article uses one of the latest, and most useful features in VB.NET: Linq. Testing was done with VB.NET Express 2008 (Framework 3.5). The COM side is VB6 running in a Windows XP environment. As in Part 1, earlier versions can require substantially different code due to changes in the way Microsoft handles COM interop.
Unlike calling COM from VB.NET, there's nothing 'automatic' about creating the pieces of the puzzle to make this work. In summary, here are the steps involved:
Create a DLL in VB.NET that uses the features you need in COM. Although VB.NET lets you simply 'add a reference' to a COM object, you can't do the reverse and 'reference' a .NET object in VB6. Instead, you have to write your own 'wrapper' code and there are some critical 'tricks' that have to be done right.
Add the DLL into the COM environment. There are several command line utility programs, REGASM and SN, that you have to use to get this step done.
Write the VB6 program to consume the .NET object. This can be any COM technology, such as VBA or another language. This example uses VB6.
On the next page, we get started with step 1, Creating the VB.NET DLL that 'wrappers' VB.NET Linq for use in COM.

