Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Property (Variant) of interface is not initialized in Unity. Same command works in Visual Studio.

Discussion in 'Experimental Scripting Previews' started by psantosbartolome, Sep 18, 2019.

  1. psantosbartolome

    psantosbartolome

    Joined:
    Jul 29, 2019
    Posts:
    8
    Hello everyone, this is the situation:

    I have a type library (.tlb) that allows me to communicate with a program run in windows (Aspen Hysys 10, but I suspect this is not relevant).
    • In this library there is a certain property (RealFlexVariable.Values) defined as Variant.
    • The library has been converted to a .dll with tlbimp.
    When I run the exact same code in Unity 2018.3.11f1 and in Visual Studio 2017, referencing the same dll and communicating with the same instance of Aspen Hysys, this happens:
    • The property is returned as a dynamic in VS, everything works properly.
    • The property is returned as a System.Object in Unity, and is not initialized when I call it. It will return an error whenever I use it later, indicating that it is not set to a reference of an object.
    It is important to note that all other methods and properties (booleans, doubles, interfaces, etc) are obtained correctly in both VS and Unity, it is only in the ones defined as Variant that this difference appears.

    I think some people will feel the need to ask to post the code. But the whole point of the error is that the same commands work on Visual Studio. The code is not the problem, the problem must be in differences in the communication protocol of Unity and Visual Studio. Whether there is a solution or not I do not know, but I hope someone does.

    Thank you for your help.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    psantosbartolome likes this.
  3. psantosbartolome

    psantosbartolome

    Joined:
    Jul 29, 2019
    Posts:
    8
    Indeed I mean the dynamic keyword. I think this might be more a symptom of the problem than the cause, because in VS I can return the variant property as a generic System.Object and it will work fine as well. My problem is not whether it returns a dynamic or a System.Object, but rather that it does not return at all.

    In any case, would you have any suggestion in how to get the property in Unity?
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Can you inspect the managed .dll you get from tlbimp with ILSpy or a similar IL decompiler? That might help indicate what the actual type of that proper is in IL code.
     
    psantosbartolome likes this.
  5. psantosbartolome

    psantosbartolome

    Joined:
    Jul 29, 2019
    Posts:
    8
    Indeed, it shows the following:

    Code (CSharp):
    1. // hysys.RealFlexVariable
    2. [DispId(0)]
    3. object Values
    4. {
    5.     [DispId(0)]
    6.     [MethodImpl(MethodImplOptions.InternalCall)]
    7.     [return: MarshalAs(UnmanagedType.Struct)]
    8.     get;
    9.     [DispId(0)]
    10.     [MethodImpl(MethodImplOptions.InternalCall)]
    11.     [param: MarshalAs(UnmanagedType.Struct)]
    12.     set;
    13. }
    14.  
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    This looks like it should be System.Object. I'm not sure what happens in Unity to make this not work.
     
  7. psantosbartolome

    psantosbartolome

    Joined:
    Jul 29, 2019
    Posts:
    8
    Well Josh, thank you very much for your help anyway. All the extra information to give that I can think of is:
    1. That the property is a double array (of unknown length)
    2. The code itself:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class runHysys : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         hysys.Application emptyApp = new hysys.Application();
    11.  
    12.         emptyApp.SimulationCases.Open(@"<filename>");
    13.  
    14.         hysys._SimulationCase simulation = (hysys._SimulationCase)emptyApp.SimulationCases[0];
    15.  
    16.         hysys.ProcessStream In = (hysys.ProcessStream)simulation.Flowsheet.MaterialStreams["In"];
    17.  
    18.         double temp = In.TemperatureValue;
    19.  
    20.         Debug.Log(temp); //This one works correctly
    21.  
    22.         System.Object desiredValues = In.ComponentMassFlowValue; //After this, any use of desiredValues will return an error in Unity, but work properly in Visual Studio
    23.  
    24.         desiredValues.ToString(); //just an example of use. It will return an error in Unity, while in Visual Studio it will return "System.Double[]"
    25.     }
    26. }
    27.  
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    What is the specific error message that Unity gives in this case?
     
  9. psantosbartolome

    psantosbartolome

    Joined:
    Jul 29, 2019
    Posts:
    8
    Here it is:

     
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Ok, so In.ComponentMassFlowValue is a null value. I'm not sure why that happens, but maybe someone else can help.