Partilhar via


Como: Referenciar Objetos COM a partir do Visual Basic

Em Visual Basic, a adição de referências a COM requer a criação de um assembly de interoperabilidade para abibliotecade COM. References to the members of the COM object are routed to the interop assembly and then forwarded to the actual COM object. Responses from the COM object are routed to the interop assembly and forwarded to your .NET Framework application.

Você pode fazer referência a umobjeto do COMsem usar um assembly de interoperabilidade, incorporando as informações de tipo doobjeto COMem um.NET assembly. Para incorporar informações de tipo, defina a Embed Interop Typesdepropriedade para True para a referência para oobjetodo COM. Se você está compilando usando o compilador delinha de comando, use o /link a opção para fazer referência abibliotecade COM. For more information, see /link (Visual Basic).

Visual Basic automatically creates interop assemblies when you add a reference to a type library from the integrated development environment (IDE). When working from the command line, you can use the Tlbimp utility to manually create interop assemblies.

To add references to COM objects

  1. On the Project menu, choose Add Reference and then click the COM tab in the dialog box.

  2. Select the component you want to use from the list of COM objects.

  3. To simplify access to the interop assembly, add an Imports statement to the top of the class or module in which you will use the COM object. Por exemplo, o exemplo de código a seguir importa o namespace INKEDLib para objetos referenciados na Microsoft InkEdit Control 1.0 biblioteca.

    Imports INKEDLib
    
    Class Sample
        Private s As IInkCursor
    
    End Class
    

To create an interop assembly using Tlbimp

  1. Add the location of Tlbimp to the search path, if it is not already part of the search path and you are not currently in the directory where it is located.

  2. Call Tlbimp from a command prompt, providing the following information:

    • Name and location of the DLL that contains the type library

    • Name and location of the namespace where the information should be placed

    • Name and location of the target interop assembly

    The following code provides an example:

    Tlbimp test3.dll /out:NameSpace1 /out:Interop1.dll
    

    You can use Tlbimp to create interop assemblies for type libraries, even for unregistered COM objects. However, the COM objects referred to by interop assemblies must be properly registered on the computer where they are to be used. You can register a COM object by using the Regsvr32 utility included with the Windows operating system.

Consulte também

Tarefas

Passo a passo: Implementando a herança com objetos COM (Visual Basic)

Solucionando problemas de interoperabilidade (Visual Basic)

Referência

Tlbimp. exe (importador da biblioteca)

Tlbexp. exe (exportador da biblioteca)

Declaração Imports (Tipo e Namespace .NET)

Outros recursos

Interoperabilidade COM (Visual Basic)