Rohit
Rohit

Reputation:

COM automation using tlb file

Consider me a novice to Windows environment and COM programming.

I have to automate an application (CANoe) access. CANoe exposes itself as a COM server and provides CANoe.h , CANoe_i.c and CANoe.tlb files. How can I write a C++ client, for accessing the object, functions of the application?

Also, please specify how to access the code present in tlb file from C++.

Upvotes: 6

Views: 22421

Answers (3)

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

Visual Studio has a lot of built-in support for importing type libraries into your C++ project and using the objects thus defined. For example, you can use the #import directive:

#import "CANoe.tlb"

This will import the type library, and convert it to header files and implementation files - also it will cause the implementation files to be built with your project and the header files to be included, so this is lots of magic stuff right there.

Then, you get a whole lot of typedefs for smart pointer wrappers for the types and objects defined in the type library. For example, if there was a CoClass called Application which implemented the interface IApplication, you could do this:

ApplicationPtr app(__uuidof(Application));

This would cause at run time, the coclass application to be created and bound to the variable app, and you can call on it like so:

app->DoSomeCoolStuff();

Error handling is done by checking the result of COM calls, and throwing the appropriate _com_error exception as necessary so this implies you need to write exception safely.

Upvotes: 9

Aamir
Aamir

Reputation: 15566

The easier way is to include both .h and _i.c project in your .cpp file using #include statements.

Since you haven't been given the dll and only tlb is provided, you can register the tlb using regtlibv12.exe which is a part of visual studio (this is the VS2005 version). By registering tlb, appropriate entries will be made in the registry and then you can use the COM library functionality as you need.

EDIT: BTW, you need DLL anyway to instantiate the COM Component successfully.

To create an interface pointer, one of the safer ways is to use CComPTR like:


CComPtr myPtr;
myPtr.CoCreateInstance(__uuidof("ClassNamehere"));
myPtr->Method(....);


Upvotes: 0

sharptooth
sharptooth

Reputation: 170489

Use import directive to import the .tlb file - this will give you a C++ equivalent of the interfaces exposed by the COM component.

You will also need to register the COM component to the registry (run regsvr32 on the .dll file of the component). After that you can call CoCreateInstance() (or _com_ptr_t::CreateInstance() as it is usually more convenient) to create an instance of the class that implements the interface. You can then call methods of the interface - it will work almost the same way as if it was a plain C++ interface and class.

Upvotes: 1

Related Questions