C# Calling COM API results in Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

1

I'm upgrading an old vba app to a C# app. The app automates Solid Edge (cad 3D program from Siemens). I'm having trouble calling a specific method of the Solid Edge API. The method in question has a bunch of ref and out parameters, I only care about one, representing the mass of the current Solid Edge drawing. I've decided to wrap the method and have a return value.

public unsafe double GetMass()
{
    //object arr = System.Reflection.Missing.Value;
    Array Arr = null;
    //The interface doesn't let us just get phm. Thats why we need 1 assignable of each type to satisfy the interface and get our out value.
    AssemblyDocument.PhysicalProperties.GetAssemblyPhysicalProperties(
        out double Mass, 
        out double AssignableDouble,
        out double AssignableDouble2,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        ref Arr,
        out bool Bool1,
        out bool Bool2
        );
    return Mass;
}

I also tried making it an unsafe method as you can see, just trial and error at this point. Calling GetAssemblyPhysicalProperties returns the error:

Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

Here is the method signature according to the documentation:

http://dl2.plm.automation.siemens.com/solidedge/api/sesdk_web/SolidEdgeAssembly~PhysicalProperties~GetAssemblyPhysicalProperties.html

In the old vba application we call the method with all doubles except for the two booleans. In C# metadata, the method signature has changed to:

void GetAssemblyPhysicalProperties(out double Mass, out double Volume, out double Area, ref Array CenterOfMass, ref Array CenterOfVolume, ref Array GlobalMoments, ref Array PrincipalAxis1, ref Array PrincipalAxis2, ref Array PrincipalAxis3, ref Array PrincipalMoments, ref Array RadiiOfGyration, out bool IsSick, out bool UpdateStatus);

Seeing the documentation, multiple coordinates are being put in the arrays. In our vba code we pass in doubles as arrays.

Questions: 1. Why does this call work in vba when it's all doubles and not arrays? Why doesn't it cause errors? 2. How can I make this code work? I've tried passing in diffrent types but it won't let me compile any other way then with Array types. I've also searched 2 pages deep on google on both Solid Edge and this specific error. In most cases they are not related to ref Array arguments. Once again, I only care about getting the Mass argument

Edit: Thanks to Hans Passant I now have the following working code:

public double GetMass()
        {
            //object arr = System.Reflection.Missing.Value;
            Array Arr = new double[0];
            //The interface doesn't let us just get phm. Thats why we need 1 assignable of each type to satisfy the interface and get our out value.
            AssemblyDocument.PhysicalProperties.GetAssemblyPhysicalProperties(
                out double Mass, 
                out double AssignableDouble,
                out double AssignableDouble2,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                ref Arr,
                out bool Bool1,
                out bool Bool2
                );
            return Mass;
        }
c#
.net
asked on Stack Overflow Sep 26, 2017 by fstam • edited Sep 26, 2017 by fstam

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0