Should DispID be unique across interfaces?

I am using COM with an old VB6 application.

I changed my code to use DispID in interfaces, as it works better than when using [ClassInterface(ClassInterfaceType.AutoDual)].

But is it allowed to start counting from DispID (1) in each interface, even if the class uses two interfaces?

Does it work stably? Or am I missing something?

[ComVisible(true)]
[Guid("9E1125A6-...")]
public interface IMyInterface1
{
    [DispId(1)]
    string Name1 { get; }
}

[ComVisible(true)]
[Guid("123425A6-...")]
public interface IMyInterface2
{
    [DispId(1)]
    string Name2 { get; }
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class MyClass : IMyInterface1, IMyInterface2
{
    public string Name1 { get { return "Name1"; } }
    public string Name2 { get { return "Name2"; } }
}
+5
source share
2 answers

Is it allowed to start at each interface, counting from DispID (1), even if the class uses two interfaces?

DISPIDs must be unique only in the interface. You can go with two interfaces, each of which has its own (different) DISPID 1 properties, even if both interfaces are implemented by the same COM object.

, VB6 , , VB6 2+, COM, "" /. DISPID ( ), , VB6 , 2+ . , , MSDN :

IDispatch, , IDispatch, .

, , VB6. , , " " . DISPIDs .

+5

COM IDispatch, , , , IDispatch:: Invoke, , DISPID COM-.

EDIT: , , , . ClassInterfaceType None, , .NET IMyInterface1 ( , , ComDefaultInterfaceAttribute ).

ClassInterfaceType AutoDual AutoDispatch, DISPIDs , .

.NET , , , ".NET open as COM", DISPID ( ), , DISPID , , regasm .

++, :

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);
    IDispatch *pDispatch;
    CoCreateInstance(__uuidof(MyClass), NULL, CLSCTX_ALL, IID_IDispatch, (void**)&pDispatch);
    DISPID dispid;
    LPOLESTR name1 = L"Name1";
    LPOLESTR name2 = L"Name2";
    HRESULT hr;
    hr = pDispatch->GetIDsOfNames(IID_NULL, &name1, 1, 0, &dispid);
    printf("Name1:%i hr=0x%08X\n", dispid, hr);
    hr = pDispatch->GetIDsOfNames(IID_NULL, &name2, 1, 0, &dispid);
    printf("Name2:%i hr=0x%08X\n", dispid, hr);
    pDispatch->Release();
    CoUninitialize();
    return 0;
}

:

Name1:1 hr=0x00000000 (S_OK)
Name2:-1 hr=0x80020006 (DISP_E_UNKNOWNNAME)

AutoDispatch AutoDual, ( ):

Name1:1610743812 hr=0x00000000
Name2:1610743813 hr=0x00000000
+2

All Articles