COM and C#.net Interoperability RRS feed

  • Question

  • Hi all,

    i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help.

    i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way.


    the above statement will return a value, assume where 123 is the parameter which could id or some thing like that

    comObject.methodName(123) = 100 

    the above statement should set the value 100 something internal member having id 123

    in both the above statement the methodName is the same. 

    so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. 

    i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#.

    Thanks for your help,


    Wednesday, July 26, 2017 6:24 PM

All replies

  • You can define an interface in .NET and expose it to COM. It really is no different than creating any other .NET type. But your second example isn't going to work. The left side of an assignment must be an lvalue and a function call isn't one. You could expose it in one of a couple of ways. The most obvious is to use the indexer property: comObject.myValue[123] = 100.

    The second option is to simply create a method with 2 parameters: comObject.methodName(123, 100). 

    If you're used to working with ATL then be aware that the generated interface you worked with was often a wrapper around the standard COM interface with some C++-specific features. .NET will actually do the same thing when you import a COM interface. But the interface itself remains unchanged.

    Michael Taylor

    Thursday, July 27, 2017 3:36 AM
  • thanks for your time Michael.

    the second example is working currently. i have a COM component developed in ATL and when i examine the code and typelibary created.


    STDMETHOD(get_methodA)(/*[in]*/ VARIANT storeID, /*[out, retval]*/ VARIANT *pVal);
    STDMETHOD(put_methodA)(/*[in]*/ VARIANT storeID, /*[in]*/ VARIANT newVal);
    STDMETHOD(get_methodB)(/*[in]*/ VARIANT storeID, /*[in]*/ VARIANT index, /*[out, retval]*/ VARIANT *pVal);
    STDMETHOD(put_methodB)(/*[in]*/ VARIANT storeID, /*[in]*/ VARIANT index, /*[in]*/ VARIANT newVal);

    when i extract the typelib for this 

     [id(0x00000001), propget, helpstring("property methodA")]
        HRESULT methodA([in] VARIANT storeID, [out, retval] VARIANT* pVal);

        [id(0x00000001), propput, helpstring("property methodA")]
        HRESULT methodA([in] VARIANT storeID, [in] VARIANT pVal);

        [id(0x00000004), propget, helpstring("property methodB")]
        HRESULT methodB([in] VARIANT storeID, [in] VARIANT index, [out, retval] VARIANT* pVal);

        [id(0x00000004), propput, helpstring("property methodB")]
        HRESULT methodB([in] VARIANT storeID, [in] VARIANT index, [in] VARIANT pVal);

    infact it works if the method has 2 arguments also. 

    methodA(123) -- return a value 
    methodA(123) = 100 -- this will set a value

    methodB(123, 3) -- return a value
    methodB(123, 3) = 100 -- this will set a value.

    i still don't know how to achieve this.

    Thursday, July 27, 2017 5:39 PM
  • How to: Use Indexed Properties in COM Interop Programming (C# Programming Guide) says: "You cannot create indexed properties of your own. The feature only supports consumption of existing indexed properties."

    However, if you define the interface in IDL, compile it to TLB, and use TLBIMP to import that to an interop DLL, then you can refer to that DLL in a C# project and make a C# class implement the interface. The properties show up as methods in the implementing class:

    dynamic get_methodA(object storeID);
    dynamic get_methodB(object storeID, object index);
    void set_methodA(object storeID, object pVal);
    void set_methodB(object storeID, object index, object pVal);

    but the calling side should be able to use them as indexed properties regardless.

    Alternatively, I suppose you could define the interface in VB.NET, which supports indexed properties.

    Thursday, July 27, 2017 11:25 PM