CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    28

    COM Interop problem

    Hi all,

    We have a COM component which was built with VC++ 6.0 and we are using it for more than 10 years. But, now we creating some client interfaces using c# and we are using this COM component as reusable.

    Now, when we load the COM object as reference in VS2008, it generates a wrapper assembly by default. When we look into the assembly, the interface was not properly translated because some of the parameters were not properly marshalled to their required types. So, we decided to create our own wrapper assembly. In this way, all the calls will be properly marshalled to their respective types.

    When we created our own assembly, some of the methods in COM object are not called even we are calling it from our .NET application. Our interfaces does not support IDispatch interface so there is no late binding over here.

    Below is the COM interface and its .NET wrapper: -

    ///////////////////////////////
    /// COM interface
    ///////////////////////////////
    [
    object,
    uuid(799D3581-58C3-11d4-930F-004005E1576C),
    helpstring("IInstrument Interface"),
    pointer_default(unique)
    ]
    interface IInstrument
    {
    HRESULT GetModel([out,retval] long *pVal);
    HRESULT Connect([in] IInstrumentSink *pSink);
    HRESULT Disconnect();
    HRESULT QueryInstCap([in] INSTRUMENT_COMMAND pCmd,[out,retval] BOOL *pVal);
    };


    [
    uuid(F7D90C10-9DC2-11D4-930F-004005E1576C),
    helpstring("TestInstrument Class")
    ]
    coclass TestInstrument
    {
    [default] interface IInstrument;
    };



    ///////////////////////////////////////////////////////////
    Custom .NET COM Wrapper
    ///////////////////////////////////////////////////////////


    [ComImport]
    [Guid("799D3581-58C3-11d4-930F-004005E1576C")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInstrument
    {
    int GetModel();

    void Connect([In] IInstrumentSink pSink);

    void Disconnect();

    [return: MarshalAs(UnmanagedType.Bool)]
    bool QueryInstCap([In] INSTRUMENT_COMMAND pCmd);
    }


    [ComImport]
    [Guid("F7D90C10-9DC2-11D4-930F-004005E1576C")]
    class TestInstrument
    {
    }




    Here, whenever I try to call 'GetModel' method in .NET, it is not called. But, when I try to call 'Connect' method, instead of calling the same method, it calls 'QueryInstCap'. It is wired, I do not understand it.

    Any help will be highly appreciated?

    Thanks in advance.

    Regards.

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: COM Interop problem

    Our interfaces does not support IDispatch interface so there is no late binding over here.
    I think your COM component needs to implement the IDispatch interface. Without it the calling clients will be calling the methods based on their position. So I think late binding is must if you want to consume it in a .NET client application. I might be wrong since it's been a long time since I've done COM.

    I assume you can't change the COM component. The next best thing would be to wrap up the COM component in C++/CLI component. That component would be able to consume the COM component natively and expose it's functionality to managed clients.

  3. #3
    Join Date
    Jan 2006
    Posts
    28

    Re: COM Interop problem

    Thanks nelo for your reply.

    We have two separate components. one is working absolutely fine whereas the other is not working. I do not know.

    Also, is it possible if the methods declared in wrapper are attributed with 'DispId'. Could it be helpful.

    Once again thanks a lot.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: COM Interop problem

    Firstly, don't use DispId. It won't do you any good. It only applies to IDispatch based interfaces.

    Try making the C# interfaces look EXACTLY like the COM interfaces i.e.

    Code:
    ComImport]
    [Guid("799D3581-58C3-11d4-930F-004005E1576C")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInstrument
    {
        HRESULT GetModel(ref int model);
    }
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured