为什么需要向IDL接口的* end *添加新事件? [英] Why is it necessary to add new events to the *end* of an IDL interface?

查看:232
本文介绍了为什么需要向IDL接口的* end *添加新事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我向现有的COM / IDL接口添加新事件时,有时会遇到一些奇怪的问题,除非它们添加到接口的末尾。

I have found that when I add new events to an existing COM/IDL interface, I sometimes run into strange issues unless they are added to the end of the interface.

例如,假设我有以下界面:

For example, say I have the following interface:

interface IMyEvents
{
    HRESULT FooCallback(
        [in] long MyParam1,
        [in] long MyParam2,
        [in] long MyParam3);

    HRESULT BarCallback(
        [in] long MyParam1,
        [in] BSTR MyParam2,
        [in] BSTR MyParam3);
};

现在假设我想添加一个新的回调事件 NewCallback 。如果我添加它像这样,当事件被触发COM时,我倾向于没有任何问题:

Now let's say I want to add a new callback event, NewCallback. If I add it like this, I tend not to have any problems when the event is fired across COM:

interface IMyEvents
{
    HRESULT FooCallback(
        [in] long MyParam1,
        [in] long MyParam2,
        [in] long MyParam3);

    HRESULT BarCallback(
        [in] long MyParam1,
        [in] BSTR MyParam2,
        [in] BSTR MyParam3);

    /* New event added to the end */
    HRESULT NewCallback(
        [in] BSTR MyParam1,
        [in] BSTR MyParam2,
        [in] BSTR MyParam3);
};

但是如果我这样添加它,我可以遇到各种问题(例如缓冲区溢出)当事件被触发时。

But if I add it like this, I can run into all sorts of problems (e.g. buffer overruns) when the event is fired.

interface IMyEvents
{
    HRESULT FooCallback(
        [in] long MyParam1,
        [in] long MyParam2,
        [in] long MyParam3);

    /* New event added to the middle */
    HRESULT NewCallback(
        [in] BSTR MyParam1,
        [in] BSTR MyParam2,
        [in] BSTR MyParam3);

    HRESULT BarCallback(
        [in] long MyParam1,
        [in] BSTR MyParam2,
        [in] BSTR MyParam3);
};



我猜想它与DLL入口点,地址偏移或类似的东西有关。或者也许是因为我没有正确地重新构建一个东西,并且添加它到最后允许它运行纯粹的运气。

I'm guessing it has something to do with DLL entry points, address offsets, or something similar. Or maybe it's because I haven't re-built something properly, and adding it to the end allows it to work by sheer luck.

任何人都可以解释这个行为? / p>

Can anyone explain this behaviour?

推荐答案

您不应该修改现有的COM接口。没有编译更改的客户端不知道它,并将继续调用,因为他们在更改之前。

You are not supposed to modify an existing COM interface. Clients that were not compiled with the change are not aware of it and will continue calling as they had done before the change.

结果是现有客户端使用一个长整数调用BarCallback,而是获取NewCallback,认为此长整数是一个BSTR。结果通常不太重要。

The result is that existing clients call BarCallback with a long integer, but instead get NewCallback that thinks this long integer is a BSTR. The results are often unpleasent.

在结束时添加新函数会遇到类似的问题。

You'll get similar problems with adding new functions at the end. Older COM object do not have the new function implemented and will likely just crash when you try to call it.

但是,如果你没有现有的客户端在野外使用旧接口,只需确保注销一切,并替换您生成的对象,客户端和代理和存根。

However, if you do not have existing clients in the wild using the old interface, just make sure you unregister everything and replace the object, clients and proxies and stubs you generated.

这篇关于为什么需要向IDL接口的* end *添加新事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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