有哪些简单的方法来包装 c++具有 COM 接口的基于对象模型

发布于 2024-08-28 02:55:54 字数 693 浏览 12 评论 0原文

我有一个预先存在的 C++ 对象模型,它代表应用程序的业务层。我希望能够将对象模型公开给用其他语言(即 vbscript、VB、javascript 等)编写的应用程序。我相信最好的方法是用 COM 层包装业务对象。

有什么快速有效的方法可以做到这一点。任何建议、实用“操作方法”文档的链接将不胜感激。

因为我正在为此启动赏金,所以这里有一些针对潜在赏金猎人的额外指南:- 1)我决定采用ATL方法 2)我现在正在专门寻找关于包装预先存在的 C++ 对象模型以使其可由像 javascript 这样的脚本语言使用的非常好的“如何快速”文档的链接 3)一些小的工作示例向我展示哪些代码需要添加到哪些文件中,例如 cpp 、 idl 和 hpp/h 等中包含哪些内容。它必须包含一个示例,我可以编译测试和更改以获得更好的理解。

附录...... 进一步的背景。对我来说,这是大约 10 年后再次访问 COM。我在2000年之前的3年里一直在做MFC和ATL COM。我了解COM是什么,在C++中实现它的原理,线程模型等。任何“如何和快速”的文档都不会盲目地引导我和玻璃化重要的原则,更重要的是,这将是一次有指导的重新学习经历。

如果我有更多时间,我会深入研究 Troelsen 的“COM 和 ATL 3.0 开发人员研讨会”,这是一本非常好的书,但它的启动(重新)启动非常缓慢。

进一步的上下文如下注释............

保持尽可能简单,单线程公寓模型和进程内 dll。

I have a pre-existing c++ object model which represents the business layer tier of an application. I want to be able to expose the object model to applications written in other languages i.e vbscript, VB, javascript etc. I believe the best way of doing this is to wrap the business objects with a COM layer.

What fast and effective methods are there for doing this. Any advice, links to practical "How to" documentation would be very much appreciated.

Because I'm starting a bounty on this , here's a few extra guidelines for potential bounty hunters :-
1)I've decided on an ATL approach
2)I'm now specifically looking for links to really good "how to and quickly" documentation on wrapping a pre-existing c++ object model to make it useable by a scripting language like javascript
3) Something with small working examples showing me what code needs to be added to what files, e.g what goes into the cpp , idl and hpp/h etc. It' must include an example I can compile test and change to get a better understanding.

Addendum .....
Further context. For me this is a re-visit to COM after about 10 years. I did MFC and ATL COM for the 3 years prior to 2000. I understand what COM is , the principles used to implement it in C++, threading models etc. Any "how to and quickly" documentation will not be leading me blindly and glazing over important principles, more it'll be a guided re-learning experience.

If I had more time I'd dig into "developer's workshop to COM and ATL 3.0" by Troelsen, a very good book but it's a very slow kick (re)start.

Further context following comments...........

Keeping it as simple as possible , Single-Threaded Apartment model and an inprocess dll.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2024-09-04 02:55:54

我在 http://www.codeproject.com/KB/COM/index.aspx?#COM/DCOM/COM+%20-%20初学者。这是:

http://www.codeproject.com/KB/COM/hellocom.aspx< /a>, http://www.codeproject.com/KB/COM/comintro2.aspx ,
http://www.codeproject.com/KB/COM/comintro.aspxhttp://www.codeproject.com/KB/COM/com_server_without_mfc_atl.aspx, http://www.codeproject.com/KB/COM/com_in_c1.aspx

但是,如果您开始对 COM 对象进行编程,那么了解 COM 的线程模型就非常重要。这在这里解释得很好

http://www.codeproject.com/KB/COM/CCOMThread .aspxhttp://www.codeproject.com/KB/COM/ CCOMThread2.aspx

但我决定回答你,因为我想建议选择其他一点现代的方式。 COM 技术非常古老。 Dot NET 拥有内部的所有功能,并将永久开发。因此,我从 在 .NET 中构建 COM 服务器 最有趣的。您用 C# 编写完整的 COM,将 COM 对象的开发分为两部分:

  1. 编写仅接口程序集,不带任何实现并注册它。
  2. 您开发一个实现此 COM 接口的 .NET 类。

您可以在 http://www.codeproject.com 中查看更简单的版本/KB/COM/COMinDotNet.aspxhttp://www.codeproject. com/KB/cs/CreateActiveXDotNet.aspxhttp://msdn .microsoft.com/en-us/library/ms973807.aspx,但我建议您采用 http://www.codeproject.com/KB/COM/BuildCOMServersInDotNet.aspx

评论后更新:抱歉,您正在寻找一种解决复杂问题的简单方法。这是不可能的。

理解这一点非常重要:尽管面向对象的 C++ 和 COM 之间有相似之处,但仍然存在许多主要差异。重要的是要理解,COM 不是一种面向对象的语言,而是一种二进制协议。软件组件(COM 对象)是可重用代码的二进制单元。它解决了基于面向对象方法设计的程序模块分开编译所存在的诸多问题。

例如,如果某人继承了您的类,然后您更改了一些私有成员,则会影响所有继承的类。使用 C++ 对象的完整代码必须重新编译,至少因为 sizeof(object) 已更改。 COM 中不存在这样的问题,因为所有使用您的对象的人都不会继承您的对象类。人们通过接口使用您的对象,在您修改基类的某些私有成员后,其签名和sizeof()不会改变。您可以在线阅读 Don Box 所著的经典书籍《Essential COM》第一章中的一些非常好的示例: http://books.google.co.uk/books ?id=kfRWvKSePmAC&dq=essential+com&printsec=frontcover&source=bn&hl=en&sa=X&oi=book_result&resnum=6&ct=结果。我还建议您阅读一篇小文章 http://edndoc。 esri.com/arcobjects/9.0/ArcGISDevHelp/DevelopmentEnvs/COM/IntroToCOM.htm 了解 COM 和 C++ 对象之间的更多差异。

如果将 C++ 对象重写为 COM 对象,则必须选择线程模型,必须了解如何编写线程安全程序,或者必须使用单线程单元模型。您的所有对象都将存在(分配和运行)在单独的线程中。如果您选择进程外 COM 对象(将创建包含所有对象的 exe),则会创建一个新进程。如果有人调用对象的方法,则会完成所有参数的编组。参数封送意味着在另一个线程中分配内存并将所有参数复制到该线程。
我之前描述的所有这些东西都不是 C++ 对象的术语。我写这篇文章只是为了澄清 COM 确实与 C++ 对象模型不同。

因此,要包装现有的 C++ 对象,您必须定义一些纯虚拟类 - 在对象中实现的接口。该接口必须以IDL/MIDLMicrosoft 接口定义语言)编写。这是最重要的!然后,您必须使用现有的 C++ 类将这些接口实现为 COM coclass

我确信,如果您在 .NET 中完成这些工作,您就不需要研究 COM 开发人员通常需要了解的大量实现细节。您只需在 C#/C++ 中定义一个接口(纯虚拟类)并编译它。您可以为 Visual Studio 内部的强签名程序集生成私钥。只需进入“签名”部分中的项目设置,然后选择“对程序集进行签名”,然后选择“新建”强名称密钥文件。就这样了。通过这种方式你可以完整地定义一个COM接口。将为您生成该界面的 MIDL 版本。

然后,您用 C# 或 C++ 创建一个新项目,并声明继承您之前定义的接口的类。在实现此接口期间,您可以使用整个现有的 C++ 对象。这样您就能够快速丰富您的目标。只需按照 http://www.codeproject.com/KB/COM 中详细描述的方式操作即可/BuildCOMServersInDotNet.aspx通过这种方式,您将不必研究 C++ 中经典 COM 实现所必须了解的大量技术细节。而且您将学习现代 .NET,而不是研究旧的和事实上已经死亡(或未进一步开发)的 COM。

抱歉回答太长。

I find a good collection of articles for beginner in http://www.codeproject.com/KB/COM/index.aspx?#COM/DCOM/COM+%20-%20Beginners. This are:

http://www.codeproject.com/KB/COM/hellocom.aspx, http://www.codeproject.com/KB/COM/comintro2.aspx,
http://www.codeproject.com/KB/COM/comintro.aspx, http://www.codeproject.com/KB/COM/com_server_without_mfc_atl.aspx, http://www.codeproject.com/KB/COM/com_in_c1.aspx

But if you start to program a COM objects it's very important to understand threading models of COM. This is explain very good here

http://www.codeproject.com/KB/COM/CCOMThread.aspx, http://www.codeproject.com/KB/COM/CCOMThread2.aspx

But I decide to answer you because I want suggest to choose a little other modern way. COM technology is very old. Dot NET has all the features inside and will permanently developed. So I find an approach from Building COM Servers in .NET the most interesting. You write your COM full in C# divide the development of your COM objects in two parts:

  1. You write interface only assembly without any implementation and register it.
  2. You develop a .NET classes which implements this COM interface.

You can look at the more easy version of the same in http://www.codeproject.com/KB/COM/COMinDotNet.aspx, http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx, http://msdn.microsoft.com/en-us/library/ms973807.aspx, but I recommend you the way "Building COM Servers in .NET" described in http://www.codeproject.com/KB/COM/BuildCOMServersInDotNet.aspx.

UPDATED after the comment: Sorry, but you are searching an easy way to solve a complex problem. It is impossible.

It is very important to understand, that despite of quasi similarity there are a lot of principal differences between object oriented C++ and COM. It’s important to understand, that COM isn't an object-oriented language but a binary protocol. A software component (COM objects) is a binary unit of reusable code. It solves a lot of problems existing in separate compilation of modules of programs designed based on object-oriented approach.

If, for example, somebody inherited you classes and you change later some private members, it makes influence to the all inherited classes. The full code used your C++ objects must be recompiled at least because sizeof(object) has been changed. Such problem not exists in COM, because all who use your objects don’t inherit your object classes. One uses your objects through interfaces, which signature and sizeof() will not be changed after you modify some private members of your base classes. You can read online some very good examples in the first chapter of the classic book "Essential COM" by Don Box: http://books.google.co.uk/books?id=kfRWvKSePmAC&dq=essential+com&printsec=frontcover&source=bn&hl=en&sa=X&oi=book_result&resnum=6&ct=result. I recommend you also to read a small article http://edndoc.esri.com/arcobjects/9.0/ArcGISDevHelp/DevelopmentEnvs/COM/IntroToCOM.htm to understand more differences between COM and C++ objects.

If you rewrite your C++ objects as a COM objects you have to choose threading model, you have to understand how to write a thread safe program or you have to use Single-Threaded Apartment model. All your objects will be exist (allocated and running) in a separate thread. If you choose Out-Of-Process COM object (an exe with all your objects will be created), then a new process will be created. If somebody calls a method of your object a marshaling of all parameters will be done. Marshaling of parameters means allocation of memory in another thread and copying of all parameter to the thread.
All this things which I described before are not a terms of C++ objects. I write this only to clear that COM really not the same as C++ object model.

So to wrap your existing C++ objects you have to define some pure virtual classes – interfaces which you implement in your objects. This interface must be in written in IDL/MIDL (Microsoft Interface Definition Language). This is the most important thing! Then you have to implement these interfaces as COM coclasses using your existing C++ classes.

I am sure if you do these work in .NET you have not be required to study a lot of implementation details typically needed to know for COM developer. You just define an Interface in C#/C++ (a pure virtual class) and compile it. You can generate a private key for strong signed assembly inside of Visual Studio. Just go in the project settings in "Signing" part and select "Sign the assembly" then choose "New" strong name key file. It’s all. With this way you full define a COM interface. A MIDL version of the interface will be generated for you.

Then you create a new project in C# or C++ and declare classes which inherit the interface which you define before. During implementing of this interface you can use your entire existing C++ object. So you will be able quickly rich your targets. Just follow the way described in details in http://www.codeproject.com/KB/COM/BuildCOMServersInDotNet.aspx. With this way you will not have to study a lot of technical details which you have to know for classical COM implementation in C++. And you will study a modern .NET instead of studying the old and de facto dead (or not further developed) COM.

Sorry for the long answer.

美人如玉 2024-09-04 02:55:54

http://www.lambdasoft.dk/Comet/index.htm

Comet 是 COM 和 C++ 之间的语言绑定。它允许您进行 COM 客户端和 COM 服务器编程,而不依赖于 ATL 或 MFC。换句话说,Comet 是 ATL 的替代品。

http://www.lambdasoft.dk/Comet/index.htm

Comet is language binding between COM and C++. It allows you to do both COM client and COM server programming, without any dependency on either ATL or MFC. In other words Comet is a replacement for ATL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文