Despite the growing popularity of Microsoft Visual Basic .NET (VB.NET), many companies are still running components written in VB6 code. For the moment, these two components (VB6 and .NET) must co-exist. Eventually, more robust .NET objects will replace VB6 components. However, this transition will take time. During this evolution, developers will have to deal with interoperability to make VB6 and .NET code sets talk to each other.
 |
| Figure 1: Interoperability basics 1-- Use marshalling in a .NET application. |
 |
| Figure 2: Interoperability basics 2 -- Using marshalling can cause a performance hit. |
Specifically, the .NET runtime's interoperability services let you write Visual Basic .NET applications that use existing VB6 ActiveX DLLs or other Component Object Model (COM) DLLs. They also let you write VB6 applications that use DLLs created with VB.NET. You can create .NET applications that leverage your existing investment in COM components, and you can use a VB6 ActiveX DLL in VB.NET and still access its functionality from VB6 applications.
Interoperability basics: marshaling, blit, managed, and unmanaged
When code from one platform calls a method on an object running on the other platform, the method call requires marshaling. In marshaling, the .NET interoperability services package up the method call, transfer it to the other platform, invoke the method, package the results, and transfer them back to the original platform. Figures 1 and 2 show this process. Marshalling causes a performance hit as the process moves the parameters from one platform to the other.
This performance hit varies depending on the data types of the parameters. Performance is best when you're moving simple numeric data, such as a 32-bit integer. This is called a blittable data type. The term "blittable" refers to the fact that you can simply copy, or blit, data from .NET memory to COM memory (or vice versa). Examples of blittable data types are Byte, Integer, and Long. However, data types such as Boolean are non-blittable and require conversion from one format to another when they're marshaled from one platform to another. The .NET and COM platforms represent and store blittable data types in memory the same way. As a result, interoperability between them is almost as quick as regular method calls.
Another interesting concept is managed and unmanaged code. Code executing under the control of the .NET runtime libraries is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code. Under the managed code scenario, .NET provides complete control of components' behavior. This control includes features such as memory addressing, exception handling, and error processing. This is possible because of .NET's Common Language Runtime infrastructure, which isn't present in COM or unmanaged components.
This tip is an excerpt from Alok's article "Explore .NET and VB6 COM Interoperability." In the full article, he shows you how VB6 and VB.NET interoperate. He provides two examples: The first describes how to call VB6 from .NET, and the second shows how to call .NET from VB6. ACCESS ADVISOR magazine subscribers can read the full article online at http://Advisor.com/doc/14478.