How to: Reference COM Objects from Visual Basic
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
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.
On the Project menu, choose Add Reference and then click the COM tab in the dialog box.
Select the component you want to use from the list of COM objects.
To simplify access to the interop assembly, add an
Importsstatement 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
INKEDLibfor objects referenced in the
Microsoft InkEdit Control 1.0library.
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.
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.
Tlbimp.exe (Type Library Importer)
Tlbexp.exe (Type Library Exporter)
Walkthrough: Implementing Inheritance with COM Objects
Imports Statement (.NET Namespace and Type)