C++ 使用 tlb 文件的 COM 自动化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1037532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
COM automation using tlb file
提问by 1800 INFORMATION
Consider me a novice to windows environment and COM programming.
考虑我是 windows 环境和 COM 编程的新手。
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.
我必须自动化应用程序 (CANoe) 访问。CANoe 将自身暴露为 COM 服务器并提供 CANoe.h 、 CANoe_i.c 和 CANoe.tlb 文件。任何人都可以指定如何编写 C++ 客户端,以访问应用程序的对象和函数。
Also, please specify how to access the code present in tlb file from C++.
另外,请指定如何从 C++ 访问 tlb 文件中存在的代码。
回答by 1800 INFORMATION
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:
Visual Studio 有很多内置支持,可以将类型库导入 C++ 项目并使用由此定义的对象。例如,您可以使用#import
指令:
#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:
然后,您将获得大量用于类型库中定义的类型和对象的智能指针包装器的 typedef。例如,如果有一个Application
实现了 interface的 CoClass 调用IApplication
,你可以这样做:
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:
这将导致在运行时创建 coclass 应用程序并将其绑定到变量app
,您可以像这样调用它:
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.
错误处理是通过检查 COM 调用的结果并在必要时抛出适当的 _com_error 异常来完成的,因此这意味着您需要安全地编写异常。
回答by sharptooth
Use import
directive to import the .tlb file - this will give you a C++ equivalent of the interfaces exposed by the COM component.
使用import
指令导入 .tlb 文件 - 这将为您提供 COM 组件公开的接口的 C++ 等效项。
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.
您还需要将 COM 组件注册到注册表(在组件的 .dll 文件上运行 regsvr32)。之后,您可以调用 CoCreateInstance()(或 _com_ptr_t::CreateInstance(),因为它通常更方便)来创建实现该接口的类的实例。然后您可以调用接口的方法 - 它的工作方式几乎与普通 C++ 接口和类相同。
回答by Aamir
The easier way is to include both .h and _i.c project in your .cpp file using #include
statements.
更简单的方法是使用#include
语句在 .cpp 文件中同时包含 .h 和 _i.c 项目。
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.
由于您没有得到 dll 并且只提供了 tlb,您可以使用 regtlibv12.exe 注册 tlb,它是 Visual Studio 的一部分(这是 VS2005 版本)。通过注册 tlb,将在注册表中创建适当的条目,然后您可以根据需要使用 COM 库功能。
EDIT: BTW, you need DLL anyway to instantiate the COM Component successfully.
编辑:顺便说一句,无论如何,您都需要 DLL 才能成功实例化 COM 组件。
To create an interface pointer, one of the safer ways is to use CComPTR like:
要创建接口指针,一种更安全的方法是使用 CComPTR,例如:
CComPtr myPtr;
myPtr.CoCreateInstance(__uuidof("ClassNamehere"));
myPtr->Method(....);