如何在 Visual Studio 2008 中创建 COM DLL? [英] How do you create a COM DLL in Visual Studio 2008?

查看:17
本文介绍了如何在 Visual Studio 2008 中创建 COM DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我编写 COM dll 以来已经有好几年了.我现在已经创建了几个类,它们继承自一些 COM 接口,但我想测试一下.我知道我必须在某处放置一个 GUID,然后将其注册到 regsvr32,但涉及的步骤是什么?

It's been ages since I've written a COM dll. I've made a couple of classes now, that inherit from some COM interfaces, but I want to test it out. I know I have to put a GUID somewhere and then register it with regsvr32, but what are the steps involved?

抱歉,忘了说我使用的是 C++.

Sorry, forgot to mention I'm using C++.

推荐答案

要创建一个新的 ATL COM 项目,您可以执行以下操作:

To create a new ATL COM project you can proceed as follow:

  1. 文件/新建项目
  2. Visual C++/ATL/ATL 项目
  3. 自定义设置,完成后按完成

你已经创建了一个新的 dll,但是它是空的,要添加一个 COM 对象你可以这样做:

You have created a new dll, but it is empty, to add a COM object you can do this:

  1. 项目/添加类
  2. Visual C++/ATL/ATL 简单对象,按添加
  3. 输入你想要的名称(如 MyObject),然后按完成添加它

如果你想让一个对象实现一个接口

If you want that an object implement an interface

  1. 在类视图中选择对象类 (CMyObject)
  2. 右键/添加/实现界面...
  3. 您可以选择要实现的接口
  1. In the class view select the object class (CMyObject)
  2. Right click/Add/Implement Interface...
  3. You can select which Interface will implement
  1. 来自项目文件中已有的 .idl 文件
  2. 来自嵌入了类型库的 .tlb/.dll/.exe
  3. 来自已注册的对象

  • 完成后按完成
  • PS:在不同的文件夹中创建一个具有相同名称的新 ATL 项目并添加您自定义的文件要容易得多.该向导执行多项任务并创建多个自定义文件.

    PS: It is much easier to create a new ATL project with the same name in a different folder, and add the files you have customized. The wizard does several tasks and create several customized files.

    对于难以逐个文件添加的大型项目,我会这样做,但我没有将文件添加到新项目中,而是开始将新项目中的设置复制到旧项目中,并添加任何其他文件向导已创建并修复了 stdafx.h 等标头以合并新设置.

    For larger projects that are difficult to add file by file, I do the same but instead of adding my files to the new project I start copying the settings from the new projects to the old one, and adding any additional file that the wizard has created and fixing headers like stdafx.h to merge the new settings.

    PPS:如果您希望您的 dll 支持 MFC,您必须选择 MFC/MFC Dll,而不是选择 ATL Project.当您添加 ATL 简单对象时,向导将要求将 ATL 支持添加到项目中.

    PPS: If you want that your dll to support MFC, instead of selecting ATL Project you have to select MFC/MFC Dll. When you add the ATL Simple Object the wizard will ask to add ATL support to the project.

    这篇关于如何在 Visual Studio 2008 中创建 COM DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆