c++comautomationtypelib

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++.


Solution

  • 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.