Flylib.com

Books Software

 
 
 

Chapter 11: VB .Classic Debugging


Chapter 11: VB .Classic Debugging

It's clear that in spite of the arrival of .NET, VB.Classic applications are still going to be around for a long time. Moving a company's business applications from VB.Classic to VB .NET is simply not a cost-effective proposition in most cases, not to mention the problem with re- skilling whole IT departments. Fortunately, Microsoft realizes that it isn't feasible to ask companies to abandon or migrate all of their legacy code, so they've provided a COM Interoperability (usually shortened to "Interop") layer to allow cooperation between the managed and unmanaged worlds . This chapter discusses the debugging of the COM Interop layer between VB .NET and VB.Classic.

This chapter first provides an overview of the very different ways in which COM and .NET view the world. It then looks at debugging Interop with a COM component written in VB 6.0 and called by a VB .NET application. Next is an investigation of debugging Interop with a VB .NET component called by a VB 6.0 application. Finally, you'll look at some of the finer details of debugging COM Interop applications.

VB .Classic Versions

You can use either VB 5.0 or VB 6.0 projects with VB .NET, but I recommend using VB 6.0 exclusively if possible. One problem is that the debug symbol files produced by VB 5.0 can cause difficulties when you try to view VB 5.0 variables from within the Visual Studio debugger. Another problem with the same debug symbol files affects the VB 5.0 source code mapping. This results in the Visual Studio IDE showing the wrong VB 5.0 source lines being executed. In fact, this can also be a problem with VB 6.0 unless Service Pack 3 (SP3) or higher has been applied. The final problem that I've encountered with both VB 5.0 and VB 6.0 (without SP3 or higher applied) is that VB.Classic components are prone to crashing during shutdown.

I recommend using VB 6.0 with the latest service pack applied, SP5 at the time of writing. With this combination, I haven't experienced any debugger- related problems.

If you're using Windows NT 4.0 as your operating system, you'll need to install NT 4.0 SP4 or higher. You won't be able to debug VB 6.0 components from VB .NET without this.



Managed vs. Unmanaged

Before you delve deeply into COM Interop, it's a good idea to understand the completely different views that managed and unmanaged code have of the world. When you have a good understanding of these two views and where they agree and disagree , it's much easier to debug the inevitable problems that COM Interop throws up.

The following list presents the major differences between these two architectures:

  • Coding model: The managed world uses an object-based view of code, whereas the COM world thinks of code in terms of interfaces. This means that Interop has to generate an interface to expose managed code to the unmanaged world, unless the managed code already provides such an interface.

  • Error handling: The usual COM practice is to return an HRESULT to signify whether a method has succeeded or failed. VB 6.0 developers get a break here because they're already used to the practice of raising errors using the Err object. COM Interop maps VB 6.0 errors to .NET exceptions, and it maps .NET exceptions to VB 6.0 errors.

  • Identities: COM uses globally unique identifiers (GUIDs) to identify unmanaged types and interfaces, whereas .NET uses strong names, which uniquely identify assemblies and managed types. COM Interop can generate GUIDs for .NET assemblies and strong names for COM components .

  • Type compatibility: Although some managed and unmanaged types are compatible, others are not. Fortunately there are .NET attributes that can solve most type compatibility problems.

  • Type definitions: A COM type library is optional and only contains public types, whereas .NET metadata is mandatory and describes both public and private types. COM Interop is quite successful at converting between type libraries and metadata.

  • Versioning: VB 6.0 allows you to extend interfaces without breaking binary compatibility, but COM interfaces are generally considered to be immutable. However, .NET interfaces can evolve while keeping the same name . When you step into the unmanaged world, you need to be careful about versioning issues, which are sometimes known as DLL Hell .

  • In this chapter I point out some of the issues raised by these two very different views of the world and discuss how you avoid or work around them.