We have created our COM component using C++ and we have tested our COM component in C++ based client application. We found that our COM component is working fine.
Since our component is built on COM, it should work with client applications which developed using other languages like Visual Basic, Java, Visual C++ etc., This is one goal of COM specification. The components developed based on COM should work with other programming languages.
Let’s test our COM component using Visual Basic. Create a Visual Basic client application to test our COM component using Visual Studio 2012 IDE.
Now add our COM component as a reference to the Visual Basic application. Unfortunately, Visual Studio is not able to add our COM component as a reference to our Visual Basic test application. Visual Studio is displaying the below error message.
--------------------------- Microsoft Visual Studio --------------------------- A reference to 'HelloComponent.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component. --------------------------- OK ---------------------------
It seems Visual Basic is not able to recognize our COM component.
Yes, Visual Basic will not understand the C++ COM component as it is. Instead of it, we should provide Type Library information along with our COM component. A type library is a binary representation of an Interface Definition Language (IDL) file. A type library contains complete information about the methods exposed by a component. Visual Basic understands this type library and through this it invokes the methods defined in a COM component.
So, we need to add Type Library information along with our COM component. To add type library information, we need to slightly modify our “Hello.idl” file. This is the first file we have created in this series of articles.
We must have to add library keyword into our “Hello.idl” file to instruct the MIDL compiler to generate Type Library. A type library should have GUID associated with it and coclass keyword to mention component supported interfaces.
We have to add library section to our “Hello.idl” file. After adding this our “Hello.idl” file looks like below.
// Hello.idl : IDL source for IHello // import "unknwn.idl"; [ uuid(2BEFC176-884D-4B33-A9FB-B0F86F2699A5), oleautomation, version(1.0) ] interface IHello : IUnknown { [id(1)] HRESULT SayHello([in] BSTR message); }; [ uuid(5FD055AD-EE01-4533-97F9-570C97BB0FE1), helpstring("HelloComponent Type Library"), version(1.0) ] library HelloComponentTypeLib { importlib("stdole32.tlb"); [ uuid(23893EE4-8514-4BD6-8830-A7EBDDF5C944) ] coclass HelloComponent { interface IHello; } };
uuid for Type Library is generated using GUID generator tool. Observe that coclass‘s uuid is already generated and we used it as CLSID_HelloComponent in our code.
Now compile “Hello.idl” file and generate Type Library from it. Below is the command:
midl Hello.idl
Above command, compiles “Hello.idl” file and generates the type library file “Hello.tlb”. This type library file now contains the information about our COM component which is easily recognized by Visual Basic.
Now add this “Hello.tlb” file to our Visual Basic test application as a reference.
Visual Studio is showing different error when we tried to add “Hello.tlb” as a reference.
--------------------------- Microsoft Visual Studio --------------------------- A reference to 'Hello.tlb' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component. --------------------------- OK ---------------------------
It seems still there is a problem in Visual Basic to identify our COM component. Actually when we add a “Hello.tlb” as a reference to Visual Basic, Visual Basic will check for an entry in the registry. We are not yet added our Type Library to Windows registry. So, we need to register our Type Library with Windows Registry.
Lets discuss how to add our Type Library to Windows Registry in our next article.
**