Walkthrough: Creating COM Objects with Visual Basic .NET
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Walkthrough: Creating COM Objects with Visual Basic .NET

Although .NET assemblies are the best choice for new applications, you may at times need to employ COM objects instead. This walkthrough demonstrates two ways to create COM objects with Visual Basic .NET: with and without the COM class template.

The easiest way to create COM objects is by using the COM class template. The COM class template creates a new class, then configures your project to generate the class as a COM object and register it with the operating system.

To create a COM object using the COM class template

  1. Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
  2. With Visual Basic Projects highlighted in the Project Types list, select Class Library from the Templates list, and then click OK. The new project is displayed.
  3. Select Add New Item from the Project menu. The Add New Item dialog box is displayed.
  4. Select COM Class from the Templates list, and then click Open. Visual Basic .NET adds a new class and configures the new project for COM interop.
  5. Add code, such as properties, methods, and events to the COM class.
  6. Select Build Solution from the Build menu. Visual Basic .NET builds the assembly and registers the COM object with the operating system.

Creating COM Objects Without the COM Class Template

You can create a COM class manually instead of using the COM class template. This procedure can be helpful when working from the command line or when you want more control over how COM objects are defined.

To set up your project to generate a COM object

  1. Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
  2. With Visual Basic Projects highlighted in the Project Types list, select Class Library from the Templates list, and then click OK. The new project is displayed.
  3. In the Solution Explorer, right-click your project, and then click Properties. The Property Pages dialog box is displayed.
  4. Click the Configuration Properties folder, and then click the Build folder. The Build properties are displayed.
  5. Check the Register for COM Interop check box.

To set up the code in your class to create a COM object

  1. In the Solution Explorer, double-click Class1.vb to display its code.
  2. Rename the class.
  3. Add the ComClass attribute to the class, specifying the guids for the Class ID, Interface ID, and events Events ID as in the following example:
    <ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
    Public Class ComClass1
    
  4. Supply the Globally Unique Identifier (GUID) constants for the ComClass attribute. To create GUIDs for your COM object, click Create GUID on the Tools menu, or launch guidgen.exe to start the guidgen utility. Select Registry Format from the list of formats provided by the guidgen application. Click the New GUID button to generate the GUID and click the Copy button to copy the GUID to the clipboard. Paste the GUID into the Visual Studio .Net code editor. Remove the leading and trailing braces from the GUID provided. For example, if the GUID provided by guidgen is "{2C8B0AEE-02C9-486e-B809-C780A11530FE}" then the GUID should appear as: "2C8B0AEE-02C9-486e-B809-C780A11530FE".
  5. Click the New GUID and Copy buttons until you have generated GUIDS for the ClassId, InterfaceId, and EventsId. For example:
    Public Const ClassId As String = "832F34A5-5CF5-403f-B4A8-428C8351FD02"
    Public Const InterfaceId As String = "3D8B5BA4-FB8C-5ff8-8468-11BF6BD5CF91"
    Public Const EventsId As String = "2B691787-6ED7-401e-90A4-B3B9C0360E31"
    
  6. COM classes must have a parameterless Public Sub New() constructor, or the class will not register correctly. Add a parameterless constructor to the class:
    Public Sub New()
       MyBase.New()
    End Sub
    
  7. Add properties, methods and events to the class, ending it with an End Class statement. Select Build Solution from the Build menu. Visual Basic .NET builds the assembly and registers the COM object with the operating system.
    Note   The COM objects you generate with Visual Studio .NET cannot be used by other Visual Studio .NET applications. Attempts to add references to such COM objects raise an error.

See Also

COM Interop | ComClassAttribute Class | Walkthrough: Implementing Inheritance with COM Objects | #Region Directive

Show:
© 2016 Microsoft