Walkthrough: Creating COM Objects with Visual Basic .NET

本文介绍在Visual Basic.NET中创建COM对象的两种方式。一是使用COM类模板,创建新类并配置项目生成COM对象;二是不使用模板手动创建,需设置项目属性和代码,添加必要属性、GUID、无参构造函数等,但生成的COM对象不能被其他Visual Studio.NET应用使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值