COM automation using tlb file

Rohit picture Rohit · Jun 24, 2009 · Viewed 20.8k times · Source

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. Can anyone specify how to 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++.

Answer

1800 INFORMATION picture 1800 INFORMATION · Jun 24, 2009

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.