Criando objetos COM com o Visual Basic
When creating new applications or components, it is best to create .NET Framework assemblies. However, Visual Basic 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 Visual Basic to expose .NET Framework 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.
Observação |
---|
Although you can also expose a class created in Visual Basic as a COM object for unmanaged code to use, it is not a true COM object and cannot be used by Visual Basic. For more information, see Interoperabilidade COM em aplicativos .NET Framework (Visual Basic). |
Observação |
---|
Seu computador pode mostrar nomes ou locais diferentes para alguns dos elementos da interface do usuário do Visual Studio nas instruções a seguir. A edição do Visual Studio que você possui e as configurações que você usa determinam esses elementos. Para obter mais informações, consulte Configurações do Visual Studio. |
To create a COM object by using the COM class template
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.
Selecione classeCOM partir do modelos e, em seguida, clique Adicionar. Visual BasicAdiciona uma nova classe e configura o novo projeto para interoperabilidade COM .
Add code such as properties, methods, and events to the COM class.
Selecione Build classlibrary1 da Build menu. Visual Basiccria o assembly e registra oobjeto do COMo sistema operacional.
Creating COM Objects without the COM Class Template
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.
To set up your project to generate a COM object
Abra um novo Windows Application projeto da Arquivo menu clicando em Novo projeto.
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.
To set up the code in your class to create a COM object
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.
Public Const ClassId As String = "" Public Const InterfaceId As String = "" Public Const EventsId As String = ""
Sobre o Ferramentas menu, clique em Criar Guid. No Criar GUID caixa de diálogo, clique em Formato do registro e, em seguida, clique em Copy. Clique em Sair.
Replace the empty string for the ClassId with 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.
Public Const ClassId As String = "2C8B0AEE-02C9-486e-B809-C780A11530FE"
Repeat the previous steps for the InterfaceId and EventsId constants, as in the following example.
Public Const InterfaceId As String = "3D8B5BA4-FB8C-5ff8-8468-11BF6BD5CF91" Public Const EventsId As String = "2B691787-6ED7-401e-90A4-B3B9C0360E31"
Observação Make sure that the GUIDs are new and unique; otherwise, your COM component could conflict with other COM components.
Add the ComClass attribute to ComClass1, specifying the GUIDs for the Class ID, Interface ID, and Events ID as in the following example:
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> Public Class ComClass1
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
Add properties, methods, and events to the class, ending it with an End Class statement. Selecione Build Solution partir do Build menu. Visual Basiccria o assembly e registra oobjeto do COMo sistema operacional.
Observação The COM objects you generate with Visual Basic cannot be used by other Visual Basic applications because they are not true COM objects. Attempts to add references to such COM objects will raise an error. For details, see Interoperabilidade COM em aplicativos .NET Framework (Visual Basic).
Consulte também
Tarefas
Passo a passo: Implementando a herança com objetos COM (Visual Basic)
Solucionando problemas de interoperabilidade (Visual Basic)
Referência
Outros recursos
Interoperabilidade COM (Visual Basic)
Interoperabilidade COM em aplicativos .NET Framework (Visual Basic)