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
, 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
- 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
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
box is selected in Visual Studio .NET or in Visual Studio 2005, follow these
- Start Visual Studio .NET or Visual Studio 2005.
- Open the solution that contains the project that you want
to build for COM interop.
- On the View menu, click Solution
- In Solution Explorer, right-click the project that you want
to build for COM interop, and then click
- Click Configuration Properties, and then
click the Build node.
Note In Visual Studio 2005, click Compile in the left
- Click to select the Register for COM
Interop check box. This option is only enabled in class library
- 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
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
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:
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 AssemblyName.dll /tlb: FileName.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
Note In Visual Studio2005 click Visual Basic under
- Under Templates, click Class
- Name the project TestProj, and then click
By default, Class1 is created.
- On the View menu, click Solution
- Right-click Class1.vb, and then click
Delete. Click OK to confirm the deletion of
the Class1.vb source file.
- On the Project menu, click Add
- Under Templates, click COM
- Name the class COMClass1.vb, and then click
COMClass1 is created with the following code.
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1
#Region "COM GUIDs"
' 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 = "6DB79AF2-F661-44AC-8458-62B06BFDD9E4"
Public Const InterfaceId As String = "EDED909C-9271-4670-BA32-109AE917B1D7"
Public Const EventsId As String = "17C731B8-CE61-4B5F-B114-10F3E46153AC"
' 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()
- Add the following function to COMClass1.
Public Function myFunction() As Integer
- In Solution Explorer, right-click
Project Name, and then click
- Under Configuration Properties, click
- Verify that the Register for COM Interop
check box is selected, and then click OK.
- On the Build menu, click Build
Solution to build the project.
- Start Visual Basic 6.0.
- 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.
- On the Project menu, click
- In the Available References list,
double-click to select TestProj, and then click
- Add a command button to the form.
- Double-click Command1 to open the Code
- Add the following code to the Command1_Click event.
Dim myObject As TestProj.COMClass1
Set myObject = New TestProj.COMClass1
- On the Run menu, click
- Click the command button.
You should receive a
message that displays 100.
For more information, visit the following Microsoft Web
Article ID: 817248 - Last Review: December 3, 2007 - Revision: 6.7
- 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
|kbhowtomaster kbinterop kbnamespace kbdll kbautomation kbcominterop kbvs2005applies kbvs2005swept KB817248|