Use a class passed by reference when the unmanaged function demands two levels of indirection.

msdn help states that for passing structures using p/invoke;

Use either a structure passed by reference or a class passed by value when the unmanaged function demands one level of indirection. and

  • Use a class passed by reference when the unmanaged function demands two levels of indirection.

  • I am calling the following 'unmanaged' C++ function from vb.net

    int EXPORTED GetValues(long lSegmentIndex, sStructA * pStructA)

    I need to fetch values for szName of sStructA and var1 of sStructB.
    Is that 2 levels of indirection   If so, do I need to lose my struct, which worked for vb6 and use a class now    
    Thank you.  greg

    typedef struct sStructC
    {
        enumeratedValue enumeratedType;     
        long   var1;                    
        double var2[3];                  
        double var3[3][3];                
        double var4[5][2];           
        double var5[5][2];          
    } sStructC;


    typedef struct sStructB
    {
        long   var1;                      
        long   var2;                  
        double var3;
        double var4[3];         
    } sStructB


    typedef struct sStructA
    {
        char   szName[128];                
        long   var2;                   
        long   var3;                    
        double var4[3];           
        double var5;             
        double var6[3];         
        double var7;                    
        double var8;                   
        int    var9;               
        int    var10;                     
        sStructB   var11[20]; 
        sStructC   var12;                
    } sStructAt;

     

    This worked fine in vb6,  The unmanaged function was called via

    Public Declare Function GetValues _
        Lib "foo.dll" _
            (ByVal lIndex As Long, ByRef sStructA As StructA) As Long

    where values to be fetched are 'name' from StructA and var1 from StructB (a field within StructA)

    Public Type StructA

        name As String * 128    
        var2(19) As StructB

    and another value required is from StructB

    Public Type StructB
        var1 As Long

     looks like a pointer to a pointer to a char. And a pointer to a struct to a struct in c++ is two layers of indirection.  Correct

    MSDN help for VS2005

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxinterop/html/9b92ac73-32b7-4e1b-862e-6d8d950cf169.htm

     



    Answer this question

    Use a class passed by reference when the unmanaged function demands two levels of indirection.

    • DeamonX

      As I try the class approach I get a Design Time error for the following; 


      Value of Type ClassB cannot be converted to '1 dimensional array of ClassB'


        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Public Class ClassB
              Public var1() As Double
              Public Sub New()
                  ReDim var1(2)
              End Sub
          End Class

         <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Public Class ClassA
              Public var1() As ClassB
              Private Sub New()
                 ReDim var1(19)
                 var1 = New ClassB()   <--
              End Sub


    • Use a class passed by reference when the unmanaged function demands two levels of indirection.