Microsoft KB Archive/817248

= How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0 =

Article ID: 817248

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft .NET Framework 2.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



INTRODUCTION
This article describes how use Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 to build a managed assembly that can be called from Microsoft Visual Basic 6.0.



Guidelines for exposing .NET types to COM
When you want to expose types in a Microsoft .NET assembly to Component Object Model (COM) applications, consider the following COM interop requirements at design time. Managed types (class, interface, struct, enum, and others) interact well with COM client applications when you follow these guidelines:
 * Define interfaces and explicitly implement them in classes. COM interop provides a mechanism to automatically generate an interface that contains all members of the class and the members of its base class. However, it is best to provide explicit interfaces and implement them explicitly.
 * Declare all managed types that you want to expose to COM as public. Only public types in an assembly are registered and exported to the type library. Therefore, only public types are visible to COM.
 * Declare all type members (methods, properties, fields, and events) that you want to expose to COM as public. Members of public types must also be public to be visible to COM. By default, all public types and members are visible. Use the ComVisibleAttribute attribute if you have to hide a type or a member from control type or member visibility to COM client applications.
 * Types must have a public default constructor to be instantiated through COM. Managed, public types are visible to COM. However, without a public default constructor (a constructor without arguments), COM clients cannot create an instance of the type. COM clients can still use the type if the type is instantiated in another way and the instance is returned to the COM client. You may include overloaded constructors that accept varying arguments for these types. However, constructors that accept arguments may only be called from managed (.NET) code.
 * Types cannot be abstract. Neither COM clients nor .NET clients can create instances of abstract types.
 * Use the COMClass template in Visual Basic .NET or in Visual Basic 2005. When you add a new class that you intend to expose to COM applications, consider using the COMClass template that is provided by Visual Basic .NET or by Visual Basic 2005. The COMClass template creates a class that includes the COMClassAttribute attribute and generates GUIDs for the CLSID, the Interface ID, and the Event ID that are exposed by your type. Additionally, the COMClass template creates a public constructor without parameters. This is the easiest way to create a new class that follows the guidelines for creating COM callable types.

Registering the .NET assembly for COM interop and creating a type library
For Visual Basic 6.0 to successfully interact with a managed component, you must register the assembly for COM interop and generate a type library. This registration must be performed on each computer where a COM client application interacts with the assembly. The type library provides type information about the exposed types in the assembly to COM client applications. The process for doing this depends on if you are working on the development computer or on the destination computer.

On the development computer, Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 automatically creates a type library and registers it during the build process if the Register for COM Interop check box is selected under the project's Configuration properties. If you used the COMClass template when you created the class, Visual Studio .NET or Visual Studio 2005 automatically selects the Register for COM Interop check box. To verify that the Register for COM Interop check box is selected in Visual Studio .NET or in Visual Studio 2005, follow these steps:
 * 1) Start Visual Studio .NET or Visual Studio 2005.
 * 2) Open the solution that contains the project that you want to build for COM interop.
 * 3) On the View menu, click Solution Explorer.
 * 4) In Solution Explorer, right-click the project that you want to build for COM interop, and then click Properties.
 * 5) Click Configuration Properties, and then click the Build node.

Note In Visual Studio 2005, click Compile in the left pane.
 * 1) Click to select the Register for COM Interop check box. This option is only enabled in class library projects.
 * 2) Click OK to close the Property Pages dialog box.

If Visual Studio .NET or Visual Studio 2005 is not installed or if you have to manually generate and register a type library (.tlb) file for the managed assembly, use the Assembly Registration tool (RegAsm.exe) with the /TLB switch. You should also use the /Codebase switch if the managed assembly is a private assembly and you intend to put the managed assembly in a different folder from the host process (EXE).

A private assembly is deployed with an application and is available for the exclusive use of that application. Other applications do not share the private assembly. Private assemblies are designed to be installed into the same folder as the host process (EXE). With a COM client application, this means that the assembly is located in the same folder as that application. A shared assembly is available for use by multiple applications on the computer. To create a shared assembly, you must sign the assembly with a strong name and install the assembly into the Global Assembly Cache (GAC) on the destination computer.

For more information about how to sign the assembly with a strong name and install the assembly into the Global Assembly Cache (GAC), visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/xc31ft41(vs.71).aspx

You should use both the /tlb: switch and the /Codebase switch when you register the assembly. The /tlb: switch generates and registers a type library, and the /Codebase switch registers the location of the managed assembly in the Windows registry. If you do not use the /Codebase switch and the assembly has not been installed into the Global Assembly Cache (GAC), you must put a copy of the assembly into the folder of each COM client application (EXE) so that the assembly can be located by the common language runtime (CLR).

To generate and register a type library and register the location of the managed assembly, type the following command at the command prompt:

Regasm  /tlb:   /codebase

Create a COM callable assembly in Visual Basic .NET
 Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.

Note In Visual Studio2005 click Visual Basic under Project Types. Under Templates, click Class Library. Name the project TestProj, and then click OK.

By default, Class1 is created. On the View menu, click Solution Explorer. Right-click Class1.vb, and then click Delete. Click OK to confirm the deletion of the Class1.vb source file.</li> On the Project menu, click Add Class.</li> Under Templates, click COM Class.</li>  Name the class COMClass1.vb, and then click Open

COMClass1 is created with the following code. <ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _ Public Class ComClass1

' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = &quot;6DB79AF2-F661-44AC-8458-62B06BFDD9E4&quot; Public Const InterfaceId As String = &quot;EDED909C-9271-4670-BA32-109AE917B1D7&quot; Public Const EventsId As String = &quot;17C731B8-CE61-4B5F-B114-10F3E46153AC&quot;
 * 1) Region &quot;COM GUIDs&quot;
 * 1) End Region

' A creatable COM class must have a Public Sub New ' without parameters. Otherwise, the class will not be    ' registered in the COM registry and cannot be created ' through CreateObject. Public Sub New MyBase.New End Sub

End Class </li>  Add the following function to COMClass1. Public Function myFunction As Integer Return 100 End Function </li> In Solution Explorer, right-click  , and then click Properties.</li> Under Configuration Properties, click Build.</li> Verify that the Register for COM Interop check box is selected, and then click OK.</li> On the Build menu, click Build Solution to build the project.</li> Start Visual Basic 6.0.</li> On the File menu, click New Project, and then click to select Standard EXE in the New Project dialog box.

By default, a form that is named Form1 is created.</li> On the Project menu, click References.</li> In the Available References list, double-click to select TestProj, and then click OK.</li> Add a command button to the form.</li> Double-click Command1 to open the Code window.</li>  Add the following code to the Command1_Click event. Dim myObject As TestProj.COMClass1 Set myObject = New TestProj.COMClass1 MsgBox myObject.myFunction </li> On the Run menu, click Start.</li> <li>Click the command button.

You should receive a message that displays 100.</li></ol>

<div class="references_section">