COM – Creating a COM Component using C++ – Add Type Library to Visual Basic Application

In our previous article, we have created Type Library “Hello.tlb”. We were facing issues when we tried to add “HelloComponent.dll” as a reference to our Visual Basic test application. Hence we have created “Hello.tlb” Type Library. And we observed that even with “Hello.tlb” Type Library, Visual Basic test application (console based application) is showing error while adding as a reference.

To resolve this, we need to register our “Hello.tlb” Type Library with Windows Registry. Add the following entries into “HelloTypeLib6432.reg” file and run this file from command prompt to add them into Windows Registry. We have added our Type Library information under TypeLib key under GUID of our type library. Location of the type library should be mentioned in win32 key.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Wow6432Node\TypeLib\{5FD055AD-EE01-4533-97F9-570C97BB0FE1}\1.0]
@="HelloComponent Type Library"

[HKEY_CLASSES_ROOT\Wow6432Node\TypeLib\{5FD055AD-EE01-4533-97F9-570C97BB0FE1}\1.0\0\win32]
@="c:\\Visual Studio Projects 2012\\Hello.tlb"

Now we have registered our Type Library with Windows Registry.

Its time to add our Type Library into Visual Basic test application. Observe that now our type library is successfully added into Visual Basic test application as a reference. Create an instance of our COM component and call its SayHello method. The Visual Basic code would be like below:

Module Module1

    Sub Main()
        Dim obj As New HelloComponentTypeLib.HelloComponent

        Call obj.SayHello("Code Steps")
    End Sub

End Module

Once you run Visual Basic application, it will display “Hello, CodeSteps” message on console window.

We are successfully called our HelloComponent from Visual Basic application.

**

4 Comments

Add a Comment

Your email address will not be published.