如何发展COM接口?

如何发展COM接口?,com,winapi,ole,Com,Winapi,Ole,众所周知,COM在发布新组件或应用程序时可以实现向后兼容性。这是可能的,因为COM中的接口是稳定的,即它们不会改变 我努力寻找一本关于如何在不同版本之间开发COM接口的参考书或书籍 以下是我的要求: 我们有一个可以由ole自动化驱动的应用程序。此应用程序的新版本可以与旧版本并行安装 此应用程序的COM客户端可以使用独立于版本的PROGID,在这种情况下,它们可以使用最新版本的应用程序,也可以使用依赖于版本的PROGID,在这种情况下,它们可以使用特定版本的应用程序 对COM自动化的更改不应破坏任

众所周知,COM在发布新组件或应用程序时可以实现向后兼容性。这是可能的,因为COM中的接口是稳定的,即它们不会改变

我努力寻找一本关于如何在不同版本之间开发COM接口的参考书或书籍

以下是我的要求:

我们有一个可以由ole自动化驱动的应用程序。此应用程序的新版本可以与旧版本并行安装

此应用程序的COM客户端可以使用独立于版本的PROGID,在这种情况下,它们可以使用最新版本的应用程序,也可以使用依赖于版本的PROGID,在这种情况下,它们可以使用特定版本的应用程序

对COM自动化的更改不应破坏任何客户端

让我们看一个例子:

#include <olectl.h>
#include <fxbase\autoif.h>

[
    uuid(A1B3A66F-806F-46a2-82D9-9C278F415148),
    lcid(-1),
    version(1.0)
]

library LIB
{   
        importlib("stdole2.tlb");

        [
            uuid(82CDE055-790A-4505-BF3E-3282170C8FC6),
            helpstring("Document"),
            oleautomation,
            dual,
            nonextensible
        ]
        interface IDocument : IDispatch
        {
                [id(0x00000001), propget, helpcontext(0x0012c94a)]
                HRESULT Name([out, retval] BSTR* psName);

                [id(0x00000001), propput, helpcontext(0x0012c94a)]
                HRESULT Name([in] BSTR psName);
        }

        [
            uuid(919B9E6E-76C0-4c23-A188-5840E5900997),
            helpstring("Application object."),
            oleautomation,
            dual,
            nonextensible
        ]
        interface IApplication : IDispatch
        {
            [id(0x00000001), propget, helpstring("Returns the active document of the application.")]
            HRESULT ActiveDocument([out, retval] IDocument** retval);
        }

        [
            uuid(E0AA6FCA-AEF1-460b-A1F9-26250C28594B),
            helpstring("Application 1.0 Class"),
            appobject
        ]
        coclass Application
        {
            [default] interface IApplication;
                                interface IDispatch;
        }
}
#包括
#包括
[
uuid(A1B3A66F-806F-46a2-82D9-9C278F41548),
lcid(-1),
版本(1.0)
]
库库
{   
importlib(“stdole2.tlb”);
[
uuid(82CDE055-790A-4505-BF3E-3282170C8FC6),
帮助字符串(“文档”),
自动化,
二重的
非扩展
]
接口IDocument:IDispatch
{
[id(0x00000001)、propget、helpcontext(0x0012c94a)]
HRESULT名称([out,retval]BSTR*psName);
[id(0x00000001)、propput、helpcontext(0x0012c94a)]
HRESULT名称([in]BSTR psName);
}
[
uuid(919B9E6E-76C0-4c23-A188-5840E5900997),
帮助字符串(“应用程序对象”),
自动化,
二重的
非扩展
]
接口IAApplication:IDispatch
{
[id(0x00000001)、propget、helpstring(“返回应用程序的活动文档”。)]
HRESULT ActiveDocument([out,retval]i文档**retval);
}
[
uuid(E0AA6FCA-AEF1-460b-A1F9-26250C28594B),
帮助字符串(“应用程序1.0类”),
对象
]
辅类应用
{
[默认]接口i应用程序;
界面IDispatch;
}
}
假设我想发布这个应用程序的2.0版,它扩展了一些接口。以下是我对2.0版的天真做法:

#include <olectl.h>
#include <fxbase\autoif.h>

[
    uuid(3D4688A2-91F8-4cd8-989A-845810A05557),
    lcid(-1),
    version(2.0)
]

library LIB
{   
        importlib("stdole2.tlb");

        [
            uuid(82CDE055-790A-4505-BF3E-3282170C8FC6),
            helpstring("Document"),
            oleautomation,
            dual
        ]
        interface IDocument10 : IDispatch
        {
                [id(0x00000001), propget, helpcontext(0x0012c94a)]
                HRESULT Name([out, retval] BSTR* psName);

                [id(0x00000001), propput, helpcontext(0x0012c94a)]
                HRESULT Name([in] BSTR psName);
        }

        [
            uuid(AF404510-216A-407e-99F4-0636AF071B68),
            helpstring("Document"),
            oleautomation,
            dual,
            nonextensible
        ]
        interface IDocument : IDocument10
        {
                [id(0x00000001), propget, helpcontext(0x0012c94a)]
                HRESULT Type([out, retval] BSTR* psType);

                [id(0x00000001), propput, helpcontext(0x0012c94a)]
                HRESULT Type([in] BSTR psType);
        }

        [
            uuid(919B9E6E-76C0-4c23-A188-5840E5900997),
            helpstring("Application object."),
            oleautomation,
            dual
        ]
        interface IApplication10 : IDispatch
        {
            [id(0x00000001), propget, helpstring("Returns the active document of the application.")]
            HRESULT ActiveDocument([out, retval] IDocument** retval);
        }

        [
            uuid(6A851C3F-21DF-4f5e-A4D6-2EF5A9D234C6),
            helpstring("Application object."),
            oleautomation,
            dual,
            nonextensible
        ]
        interface IApplication : IApplication10
        {
            [id(0x00000002), propget, helpstring("Is the application visible.")]
            HRESULT Visible([out, retval] BOOL* retval);
        }

        [
            uuid(AA760349-1682-4ab6-BF0C-C02E620715CF),
            helpstring("Application 2.0 Class"),
            appobject
        ]
        coclass Application
        {
            [default] interface IApplication;
                                interface IDispatch;
        }
}
#包括
#包括
[
uuid(3D4688A2-91F8-4cd8-989A-845810A0557),
lcid(-1),
版本(2.0)
]
库库
{   
importlib(“stdole2.tlb”);
[
uuid(82CDE055-790A-4505-BF3E-3282170C8FC6),
帮助字符串(“文档”),
自动化,
二重的
]
接口IDocument10:IDispatch
{
[id(0x00000001)、propget、helpcontext(0x0012c94a)]
HRESULT名称([out,retval]BSTR*psName);
[id(0x00000001)、propput、helpcontext(0x0012c94a)]
HRESULT名称([in]BSTR psName);
}
[
uuid(AF404510-216A-407e-99F4-0636AF071B68),
帮助字符串(“文档”),
自动化,
二重的
非扩展
]
接口IDocument:IDocument10
{
[id(0x00000001)、propget、helpcontext(0x0012c94a)]
HRESULT类型([out,retval]BSTR*psType);
[id(0x00000001)、propput、helpcontext(0x0012c94a)]
HRESULT类型([in]BSTR psType);
}
[
uuid(919B9E6E-76C0-4c23-A188-5840E5900997),
帮助字符串(“应用程序对象”),
自动化,
二重的
]
接口IAApplication10:IDispatch
{
[id(0x00000001)、propget、helpstring(“返回应用程序的活动文档”。)]
HRESULT ActiveDocument([out,retval]i文档**retval);
}
[
uuid(6A851C3F-21DF-4f5e-A4D6-2EF5A9D234C6),
帮助字符串(“应用程序对象”),
自动化,
二重的
非扩展
]
接口i应用程序:i应用程序10
{
[id(0x00000002)、propget、helpstring(“应用程序是否可见”)]
HRESULT可见([out,retval]BOOL*retval);
}
[
uuid(AA760349-1682-4ab6-BF0C-C02E620715CF),
帮助字符串(“应用程序2.0类”),
对象
]
辅类应用
{
[默认]接口i应用程序;
界面IDispatch;
}
}
这是正确的方法吗

我是否应该向注册表中添加一个类Application10和Application20,以便为脚本客户端实例化应用程序的不同版本

更改类型库的版本和GUID是否正确

版本2.0中的I文档有一个新的IID。我仍然可以使用iaapplication.ActiveDocument中的IDocument吗

如何在不同版本的Windows注册表中注册coclass或接口

请注意,我不使用ATL或WIN32-API以外的其他库

如果你知道我在哪里可以找到这方面的信息(书籍,参考资料等),请建议一个


非常感谢您的帮助。

是的,如果希望多个版本共存,您需要更改对象的名称。但是,您可以维护一个独立于版本的应用程序,如“Word.Application”或“InternetExplorer.Application”。向后兼容性很难保持。MSXML显然放弃了独立于版本的progID

如果决定保留旧接口(推荐),则需要在新对象中实现新接口和旧接口

您可能需要检查Microsoft Office的注册表项。它很好地保持了向后兼容性

建议的新接口名称为interfact名称加上版本号su