How to: Reference COM Objects from Visual Basic

In Visual Basic, adding references to COM objects that have type libraries requires the creation of an interop assembly for the COM library. 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.

You can reference a COM object without using an interop assembly by embedding the type information for the COM object in a .NET assembly. To embed type information, set the Embed Interop Types property to True for the reference to the COM object. If you are compiling by using the command-line compiler, use the /link option to reference the COM library. 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. For example, the following code example imports the namespace INKEDLib for objects referenced in the Microsoft InkEdit Control 1.0 library.

    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.

See also