How to reference tlb file in VC++
-
Hi All, I have generated a tlb file through idl for the implementation of COM. I want to use this COM object in my VC++ code. Anybody know how to reference tlb file in VC++ code. For VB I can directly reference the tlb file, but I dont dont know is there any way we can reference tlb file in my VC++ code and start using the methods inside COM object Thanks in Advance JK Reply·
-
Hi All, I have generated a tlb file through idl for the implementation of COM. I want to use this COM object in my VC++ code. Anybody know how to reference tlb file in VC++ code. For VB I can directly reference the tlb file, but I dont dont know is there any way we can reference tlb file in my VC++ code and start using the methods inside COM object Thanks in Advance JK Reply·
Using .tlb in C++ is not so straightforward as in VB, but possible. Generally, the following example may guide you how to use type libraries in VC++;
#import "MyPath\MyTypeLib.tlb" rename_namespace("SmthLooksMeNice") using namespace SmthLooksMeNice;
#include <objbase.h>int main()
{
CoInitialize(NULL);// Assume your COM object is named MyComObjectName
// and there is an interface IMyComObject.
// #import generated a smart pointer over that interface.
IMyComObjectPtr pObject(_uuidof(MyComObjectName));BSTR temp = pObject->SomeProp;
pObject->DoSmth();
//...
CoUninitialize();
return 0;
}-- ===== Arman
-
Using .tlb in C++ is not so straightforward as in VB, but possible. Generally, the following example may guide you how to use type libraries in VC++;
#import "MyPath\MyTypeLib.tlb" rename_namespace("SmthLooksMeNice") using namespace SmthLooksMeNice;
#include <objbase.h>int main()
{
CoInitialize(NULL);// Assume your COM object is named MyComObjectName
// and there is an interface IMyComObject.
// #import generated a smart pointer over that interface.
IMyComObjectPtr pObject(_uuidof(MyComObjectName));BSTR temp = pObject->SomeProp;
pObject->DoSmth();
//...
CoUninitialize();
return 0;
}-- ===== Arman
Hi Arman, Thanks for the sample code I did import my tlb file with rename_namespace("MYNAMESPACE") option I got following two compilation errors 1) error C2664: '_com_issue_errorex' : cannot convert parameter 2 from MYNAMESPACE::MYCLASS *const ' to 'IUnknown *' 2) error C2504: 'AnyObject' : base class undefined I tried import using following command #import "C:\temp\Mytlb.tlb" rename_namespace("MYNAMESPACE") raw_interface_only The first compliation error went off but the second error error C2504: 'AnyObject' : base class undefined still exists. When I see my tlh file my class is deriving from AnyObject base class. I am not sure why it is deriving from AnyObject? Is there any base class called "AnyObject" in C++? If yes what are the include files i need to include? Please help me. Thanks in advance. Regards JK
-
Hi Arman, Thanks for the sample code I did import my tlb file with rename_namespace("MYNAMESPACE") option I got following two compilation errors 1) error C2664: '_com_issue_errorex' : cannot convert parameter 2 from MYNAMESPACE::MYCLASS *const ' to 'IUnknown *' 2) error C2504: 'AnyObject' : base class undefined I tried import using following command #import "C:\temp\Mytlb.tlb" rename_namespace("MYNAMESPACE") raw_interface_only The first compliation error went off but the second error error C2504: 'AnyObject' : base class undefined still exists. When I see my tlh file my class is deriving from AnyObject base class. I am not sure why it is deriving from AnyObject? Is there any base class called "AnyObject" in C++? If yes what are the include files i need to include? Please help me. Thanks in advance. Regards JK
Hi Arman, I found out the solution for error C2504: 'AnyObject' : base class undefined My tlb is dependent on some other tlb so, what i did is I #import the base tlb before importing my tlb. It complies fine. There are no errors. But, I am not able to get pointer to my COM object, when I am running following sample code CoInitialize(NULL); IMyComObject pObject(_uuidof(MyComObjectName)); I am getting following error in CoCreateInstance method 0x80040154 Class Not Registered I checked the registry, there is an entry for my typelib. I am not sure why it is not able find the class. Any suggestions/help please.. Thanks in advance JK