I’m working on a project that uses web services code written in .Net (‘cos it’s heaps easier that way) but calls legacy code written in VB6. And vice versa.
(Wow, I never thought I’d be referring to VB6 code as legacy, but there you go. None of your smart comments, any VB-haters out there.)
Here’s what I’ve discovered about making it work.
How to make .Net DLLs visible to COM/VB6
In your .Net project
- Project settings; Assembly information; switch on “Make assembly COM-visible”
- Watch out for the variables you use. VB.Net Longs can’t be used in VB6, for instance.
- Build it
- regasm.exe path\whatever.dll /tlb /codebase /verbose
- /tlb tells it to produce a TLB file, which VB6 can talk to
- /codebase makes it work for assmemblies other than in the Global Assembly Cache
- /verbose because you want to see all the grisly details, right?
- You’ll get a warning if your Assembly (DLL) isn’t strongly named. This involves name/version uniqueness, a digital signature and certificate. More details here. Ah, the lengths we must go through to avoid DLL hell.
- Do a regasm.exe /unregister if you need to later
In your creaking old VB6 project
- Add the reference to your whatever.tlb
- Call that sucker. Shame the Intellisense seems to only half work.
If you want to be able to call it with late-binding, there are some extra hoops to jump through.
How to call COM/VB6 DLLs from .Net
It’s really easy. Just add the reference to the COM DLL.
But beware: in .Net, merely setting an object to Nothing doesn’t release it immediately; it gets released at some stage later, when the .Net runtime feels like it. To force its release, use the Marshal.ReleaseComObject method.
And if you’re calling from Asp.Net, check if you need to use the ASPCompat option, or use Com Plus, which is necessary when calling COM from a web service, otherwise you’ll get weird results if you happen to get two calls happening simultaneously.
DO NOT be tempted to add the “ClassInterfaceType.AutoDual” attribute to the class in order to get Intellisense working in VB6 (as some pages on the net suggest) This will cause problems when methods are added/changed in .NET class, or even when you try to use the COM object on a different machine.
See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconintroducingclassinterface.asp
for more details.