| title | Walkthrough: Creating COM Objects with Visual Basic | |||||
|---|---|---|---|---|---|---|
| ms.custom | ||||||
| ms.date | 07/20/2015 | |||||
| ms.prod | .net | |||||
| ms.reviewer | ||||||
| ms.suite | ||||||
| ms.technology |
|
|||||
| ms.topic | article | |||||
| helpviewer_keywords |
|
|||||
| ms.assetid | 7b07a463-bc72-4392-9ba0-9dfcb697a44f | |||||
| caps.latest.revision | 30 | |||||
| author | dotnet-bot | |||||
| ms.author | dotnetcontent |
When creating new applications or components, it is best to create .NET Framework assemblies. However, [!INCLUDEvbprvb] also makes it easy to expose a .NET Framework component to COM. This enables you to provide new components for earlier application suites that require COM components. This walkthrough demonstrates how to use [!INCLUDEvbprvb] to expose [!INCLUDEdnprdnshort] objects as COM objects, both with and without the COM class template.
The easiest way to expose COM objects is by using the COM class template. The COM class template creates a new class, and then configures your project to generate the class and interoperability layer as a COM object and register it with the operating system.
Note
Although you can also expose a class created in [!INCLUDEvbprvb] as a COM object for unmanaged code to use, it is not a true COM object and cannot be used by [!INCLUDEvbprvb]. For more information, see COM Interoperability in .NET Framework Applications.
[!INCLUDEnote_settings_general]
-
Open a new Windows Application project from the File menu by clicking New Project.
-
In the New Project dialog box under the Project Types field, check that Windows is selected. Select Class Library from the Templates list, and then click OK. The new project is displayed.
-
Select Add New Item from the Project menu. The Add New Item dialog box is displayed.
-
Select COM Class from the Templates list, and then click Add. [!INCLUDEvbprvb] adds a new class and configures the new project for COM interop.
-
Add code such as properties, methods, and events to the COM class.
-
Select Build ClassLibrary1 from the Build menu. [!INCLUDEvbprvb] builds the assembly and registers the COM object with the operating system.
You can also create a COM class manually instead of using the COM class template. This procedure is helpful when you are working from the command line or when you want more control over how COM objects are defined.
-
Open a new Windows Application project from the File menu by clicking NewProject.
-
In the New Project dialog box under the Project Types field, check that Windows is selected. Select Class Library from the Templates list, and then click OK. The new project is displayed.
-
In Solution Explorer, right-click your project, and then click Properties. The Project Designer is displayed.
-
Click the Compile tab.
-
Select the Register for COM Interop check box.
-
In Solution Explorer, double-click Class1.vb to display its code.
-
Rename the class to
ComClass1. -
Add the following constants to
ComClass1. They will store the Globally Unique Identifier (GUID) constants that the COM objects are required to have.[!code-vbVbVbalrInterop#2]
-
On the Tools menu, click Create Guid. In the Create GUID dialog box, click Registry Format and then click Copy. Click Exit.
-
Replace the empty string for the
ClassIdwith the GUID, removing the leading and trailing braces. For example, if the GUID provided by Guidgen is"{2C8B0AEE-02C9-486e-B809-C780A11530FE}"then your code should appear as follows.[!code-vbVbVbalrInterop#3]
-
Repeat the previous steps for the
InterfaceIdandEventsIdconstants, as in the following example.[!code-vbVbVbalrInterop#4]
[!NOTE] Make sure that the GUIDs are new and unique; otherwise, your COM component could conflict with other COM components.
-
Add the
ComClassattribute toComClass1, specifying the GUIDs for the Class ID, Interface ID, and Events ID as in the following example:[!code-vbVbVbalrInterop#5]
-
COM classes must have a parameterless
Public Sub New()constructor, or the class will not register correctly. Add a parameterless constructor to the class:[!code-vbVbVbalrInterop#6]
-
Add properties, methods, and events to the class, ending it with an
End Classstatement. Select Build Solution from the Build menu. [!INCLUDEvbprvb] builds the assembly and registers the COM object with the operating system.[!NOTE] The COM objects you generate with [!INCLUDEvbprvb] cannot be used by other [!INCLUDEvbprvb] applications because they are not true COM objects. Attempts to add references to such COM objects will raise an error. For details, see COM Interoperability in .NET Framework Applications.
xref:Microsoft.VisualBasic.ComClassAttribute
COM Interop
Walkthrough: Implementing Inheritance with COM Objects
#Region Directive
COM Interoperability in .NET Framework Applications
Troubleshooting Interoperability