Search code examples
comuuididl

Why do we have to create unique uuid for every interface in a idl?


Why do we have to create unique uuid for each interface in a idl ?

For example in this interface there's a unique uuid for every interface in TradingLib.

import "oaidl.idl";

[uuid(7C948DDC-8C22-46CF-91AD-1E043A0E1A10), object]
interface IInventory : IUnknown
{
    HRESULT GetStock([out, retval] long* pStock);
    HRESULT Buy([in] long quantity, [in] float rate);
    HRESULT Sell([in] long quantity, [in] float rate);
};

[uuid(F7CF450D-C4BE-4943-A384-AA5BB4A89202), object]
interface IAccount : IUnknown
{
    HRESULT GetBalance([out, retval] double* pBalance);
    HRESULT Credit([in] double amount);
    HRESULT Debit([in] double amount);
};

[uuid(9791C352-4665-403C-8A37-3EC8485A87D5), version(1.0), helpstring("XYZ Trading Library")]
library TradingLib
{
    importlib("stdole32.tlb");

    [uuid(03698856-A173-417F-93CF-AEBC27EB042A)]
    coclass Trader
    {
        [default] interface IInventory;
        interface IAccount;
    };

    [uuid(E596BE02-0DCE-4B7C-B8D4-4F621F675BF0)]
    enum TradingErrors
    {
        TRADER_E_OUTOFSTOCK = 0x80040101,
        TRADER_E_INSUFFICIENTFUNDS = 0x80040102
    };
};

Why can't we just use a single uuid for the TradingLib library itself? Why is it necessary to create uuid for every interface?

Thanks.


Solution

  • Each COM interface must have a unique id associated with it. Each time you introduce a new interface you have to assign it a new unique id.

    That's one of COM conventions - if you know an interface id you know what the interface is (all it's method with exact signatures). So in your example the answer is - you can't reuse an id because each distinct interface must have its own unique id.