Thursday, April 26, 2007

Call Into The .NET Framework From Existing Visual Basic 6.0 Apps

Wrap It Up: Call Into The .NET Framework From Existing Visual Basic 6.0 Apps -- MSDN Magazine, May 2006


Calling the .NET Framework
Visual Basic 6.0 is great at calling into COM objects. However, the classes in the .NET Framework are not COM objects and are not directly callable from Visual Basic 6.0. Instead, the .NET Framework classes must be called through a wrapper. The wrappers are built using the COM Class item, and they behave like any other COM objects. The wrappers are callable from Visual Basic 6.0, Visual Basic Scripting Edition (VBScript), Visual Basic for Applications (VBA), or any other COM-compatible environment. The wrapper internals are constructed in Visual Basic 2005 and can call seamlessly into the .NET Framework (see Figure 1).
Figure 1 .NET Wrappers for Visual Basic 6.0
The construction of wrappers is a downright trivial task. These following steps will walk you through creating a COM callable wrapper for the Ping functionality in the .NET Framework 2.0.
Start either Visual Studio 2005 or Visual Basic Express.
Create a new Class Library project named FX20Wrapper.
Select the Project menu, and then select Add New Item.
Select the COM Class item template. For the name, enter NetworkWrapper, and click Add.
Add the following function to the NetworkWrapper class:

Public Function Ping(ByVal hostNameOrAddress As String, _
Optional ByVal timeout As Integer = -1)
If timeout >= 0 Then
Return My.Computer.Network.Ping(hostNameOrAddress, timeout)
Else
Return My.Computer.Network.Ping(hostNameOrAddress)
End If
End Function

That’s it! With jus