简体   繁体   English

在C ++ Builder中使用COM

[英]Use COM in C++ Builder

I'm new to COM libraries and I'm stuck on using a COM DLL in my C++ Builder (XE2) application. 我是COM库的新手,我坚持在我的C ++ Builder(XE2)应用程序中使用COM DLL。 DLL is registered. DLL已注册。 Which are the steps that allows me to create objects belonging to such DLL and invoke their methods? 哪些步骤允许我创建属于这种DLL的对象并调用它们的方法? I mean statically. 我的意思是静态的。

I couldn't find a tutorial, while I saw different ways: 我找不到教程,而我看到了不同的方式:

  1. Component > Import component > it produces a new wrapper unit... and then what? 组件>导入组件>它生成一个新的包装器单元......然后是什么?
  2. import the DLL with an absolut path (why? it is registered in the system) 用绝对路径导入DLL(为什么?它在系统中注册)

     #import "C:\\Path\\to\\the\\LIB1.dll" rename_namespace ("LIB1") 

    ... and then what? ... 然后什么?

  3. use CoCreateInstance ... how exactly? 使用CoCreateInstance ......到底是怎么回事? without import/include? 没有import / include?

In Visual C# I deal with it simply adding a reference and a using ! 在Visual C#中,我只需添加引用和using即可处理它!

I'm very confused! 我很困惑! Any help is appreciated. 任何帮助表示赞赏。

I found a way (but tell me if there are better ones): 我找到了一种方法(但是告诉我是否有更好的方法):

  • Component > Import component... > Import a Type Library > select the library 组件>导入组件...>导入类型库>选择库
  • Unit Dir Name = and uncheck "Generate Component Wrappers" Unit Dir Name =并取消选中“Generate Component Wrappers”
  • "Add unit to MyProject.cbproj project" > Finish “将单位添加到MyProject.cbproj项目”>完成
  • in the client class > File > Use Unit... > select the unit that was created 在客户端类>文件>使用单位...>中选择已创建的单位
  • in the client class write this code for using the COM DLL: 在客户端类中编写此代码以使用COM DLL:

     CoInitialize(NULL); //Init COM library DLLs ICompany *company; HRESULT hr = CoCreateInstance ( CLSID_Company, NULL, CLSCTX_INPROC_SERVER, IID_ICompany, (void**) &company ); if (SUCCEEDED (hr)) { //TODO here you can use your company object! //and finally release such resource company->Release(); } CoUninitialize(); 

Where Company was the original class, exposed by the DLL, which I wanted to intantiate. Company是原始类,由DLL公开,我想要实例化。

Introduction to COM - What It Is and How to Use It. COM简介 - 它是什么以及如何使用它。 helped me a lot. 帮了我很多忙。

Note that this requires the creation of *_TLB.* and *_OCX.* units . 请注意 ,这需要创建* _TLB。*和* _OCX。*单位 Is there a way that avoids it? 有没有办法避免它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM