none
C# COM client get TypeLoadException when call C++ COM server function. RRS feed

  • Question

  • I have a question. 
    I'm developing C# COM client and C++ COM server.

    I created a function that returns SafeArray with a custom structure declared in C++. When I call this function in C# COM client as below, a TypeLoadException occurs.

    ----------- .idl file (C++) ---------------
    interface ICustomInterface : IDispatch
    {
    HRESULT GetSafeArray([out, retval]SAFEARRAY(CustomStruct)* pStructArray);
    };

    ----------- C# client ---------------
    try
    {
        ICustomInterface custom = new CustomInterface();

        Array arr = custom.GetSafeArray(); // TypeLoadException
        if (arr != null)
        {
        }
    }
    catch (TypeLoadException ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }


    So I tried several ways to solve this problem, and I found out that Exception does not occur when I try to do the following.

    ----------- C# client ---------------
    try
    {
        ICustomInterface custom = new CustomInterface();

        CustomStruct customStruct = new CustomStruct(); // TypeLoadException is not occur with this line of code.

        Array arr = custom.GetSafeArray(); // TypeLoadException
        if (arr != null)
        {
        }
    }
    catch (TypeLoadException ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }

    The only difference is that declared the custom structure before calling the interface.
    I really want to know why this is happening.

    Thanks in advance.
    Monday, April 8, 2019 4:40 AM

All replies

  • Hi

    Thank you for posting here.

    For you your question, could provide more details about the TypeLoadException?

    We are waiting for your update.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 5:14 AM
    Moderator
  • Hi Jack,

    In my question, I have this code in second C# client : [CustomStruct customStruct = new CustomStruct ();].

    In fact, I don't need a customStruct object at all, but if this code doesn't exist, a call to custom.GetSafeArray(); will throw a TypeLoadException with the following message: Could not load type 'MyCustomLib.CustomStruct' from assembly.

    GetSafeArray() function returns an array of CustomStruct. I should declare the CustomStruct type once in C# before calling this function. I do not know why should i declare the type.
    Wednesday, April 10, 2019 6:01 AM
  • Hi

    Thanks for the feedback.

    Can you give me a complete code to reproduce your problem?

    We are waiting for your update.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 11, 2019 6:38 AM
    Moderator