How to: Reference COM Objects from Visual BasicĀ
In Visual Basic, adding references to COM objects that have type libraries is similar to doing so in previous versions. However, Visual Basic adds the creation of an interop assembly to the procedure. 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.
To add references to COM objects
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 Imports statement to the top of the class or module in which you will use the COM object.
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 create an interop assembly using Tlbimp
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.
See Also
Tasks
Walkthrough: Implementing Inheritance with COM Objects
Troubleshooting Interoperability
Reference
Type Library Importer (Tlbimp.exe)
Type Library Exporter (Tlbexp.exe)
Imports Statement