创建实现特定接口的 ATL COM 对象 [英] Creating an ATL COM object that implements a specific interface

查看:21
本文介绍了创建实现特定接口的 ATL COM 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个简单的 ATL COM 对象,该对象实现一个特定的接口,我已经为其提供了一个 .tlb 文件和一个 .idl 文件.界面非常简单,只包含一个方法.我过去创建了许多 ATL 对象,但从来没有一个必须实现特定接口.我需要什么来实现这一目标?我假设我需要以某种方式在我的新对象 IDL 中的某处引用接口的 IDL 或 TLB?

欢迎任何指点.

解决方案

它比这里建议的其他答案要自动得多.所有样板代码都是由 Visual Studio 为您编写的.

你很幸运拥有 .idl,我认为这是迄今为止最方便的.

您可以将 .idl 文件的内容粘贴到 ATL COM 项目现有的 .idl 文件中,这样您就可以访问其中的声明.例如,可以将类似的内容粘贴到 IDL 文件中:

<预><代码>[目的,uuid(ecaac0b8-08e6-45e8-a075-c6349bc2d0ac),双,不可扩展,helpstring("IJim 接口"),指针默认(唯一)]接口 IJim : IDispatch{[id(1), helpstring("method SpliceMainbrace")] HRESULT SpliceMainbrace(BSTR* avast);};

然后在班级视图中,右键单击您的班级并选择 Add |实现接口.

请注意,在此对话框中,您实际上可以浏览 .tlb 文件,但我认为最好为这些内容、版本控制等提供纯文本源.

从列表中选择IJim,按>按钮将其添加到要实现的列表中.按完成.

Visual Studio 会将它添加到您的类中(以及一堆其他废话以使其工作):

//IJim 方法民众:STDMETHOD(SpliceMainbrace)(BSTR * avast){//在此处添加您的函数实现.返回 E_NOTIMPL;}

I need to implement a simple ATL COM object that implements a specific interface for which I have been given both a .tlb file and a .idl file. The interface is very simple and only consists of a single method. I have created many ATL objects in the past but never one that has to implement a specific interface. What do I need to achieve this? I'm assuming that I somehow need to reference the interface's IDL or TLB in my new objects IDL somewhere?

Any pointers are welcome.

解决方案

It's much more automatic than the other answers here are suggesting. All the boilerplate code is written for you by Visual Studio.

You're lucky you have the .idl, it's by far the most conveninent, I think.

You could paste the contents of the .idl file into your ATL COM project's existing .idl file, which would give you access to the declarations in it. For example, something like this could be pasted into an IDL file:

[
    object,
    uuid(ecaac0b8-08e6-45e8-a075-c6349bc2d0ac),
    dual,
    nonextensible,
    helpstring("IJim Interface"),
    pointer_default(unique)
]
interface IJim : IDispatch
{
    [id(1), helpstring("method SpliceMainbrace")] HRESULT SpliceMainbrace(BSTR* avast);
};

Then in Class View, right click your class and choose Add | Implement Interface.

Notice that in this dialog, you can actually browse for a .tlb file, but I think it's better to have plain-text source for these things, for version control and the like.

Pick IJim from the list, press the > button to add it to the list to be implemented. Press Finish.

Visual Studio will add this to your class (along with a bunch of other crap to make it work):

// IJim Methods
public:
    STDMETHOD(SpliceMainbrace)(BSTR * avast)
    {
        // Add your function implementation here.
        return E_NOTIMPL;
    }

这篇关于创建实现特定接口的 ATL COM 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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