COM基本示例
我一直在读Essential COM,这是一本非常好的书,非常具有指导性且简单易懂。 现在我想加快速度并实现一个简单的 COM 对象,将其编译为 .dll,最后从客户端应用程序中使用它。
如果有人能展示如何做到这一点的最基本示例,我将不胜感激?
我正在使用 Microsoft Visual Studio .Net 2003 使用 Visual C++
提前致谢!
I've been reading Essential COM, it is a very good book, very instructive and simple to understand. Now I want to speed things up and implement a simple COM object, compile it into a .dll and finally use it from a client application.
I would really appreciate if anybody could show the most basic sample of how to do that?
I've been trying with this step by step but, besides founding some errors, I could not make it work. The reason for this is that I've created a simple Win32 application, I started coding the COM from scratch (as the step by step shows) and fails to compile/link (lot of errors), I must be forgetting some configuration or some includes in stdafx.h or whatever.
I'm working in Visual C++ with Microsoft Visual Studio .Net 2003
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最无摩擦的方法之一是使用向导生成的解决方案之一,特别是 VS 2008 中的“ATL 项目”向导(不确定 2003 年是否如此)。 当询问您想要的服务器类型时,您只需选择“DLL”,然后您就会得到一个非常有用的 COM DLL 框架,可以在其中填写您的代码。
提示:要添加新的 COM 接口和组件类,最简单的方法是转到“类视图”,右键单击该项目,然后选择“添加”->“类...”,然后选择“ATL Simple Object”并回答向导问题。
编辑:回答Toto在评论中的附加问题(如何创建客户端以使用新创建的COM DLL),答案是“这取决于语言”,因为你有一个巨大的选择这里。 您可以使用 VB(“添加引用”)、C#/VB.NET(“添加引用”、COM 选项卡)、VBScript (WScript.CreateObject),当然也可以使用 C++。 简而言之,从 C++ 客户端,您需要包含服务器的 IDL 并链接到服务器的 LIB 文件。
One of the most friction-free ways is to use one of the wizard-generated solutions, and in particular the "ATL Project" wizard in VS 2008 (not sure about 2003). You just select "DLL" when asked the server type you want, and you're left with a very usable COM DLL skeleton where to fill in your code.
Tip: to add a new COM interface and coclass, the easiest way is to go to Class View, right-click on the project, and select Add->Class..., then select "ATL Simple Object" and answer the wizard questions.
EDIT: to answer Toto's additional question in the comments (how to create a client to use the freshly-created COM DLL), the answer is "it depends on the language", as you have an enormous choice here. You can use VB ("add reference"), C#/VB.NET ("add reference", COM tab), VBScript (WScript.CreateObject), and of course C++ too. In a nutshell, from a C++ client you need to include the server's IDL and link to the server's LIB file.