Mashaling a VB6 Array Of Struct to C# Array of Struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cybertof

    Mashaling a VB6 Array Of Struct to C# Array of Struct

    Hello,

    Is it possible to convert a VB6 Array of Struct
    to
    a C# Array Of Struct ?

    The test context is a C# application calling a VB6 ActiveX DLL Function
    using UDT (User Defined Type) and array of UDT.


    *************** *************** *********
    Example : (VB6AX is an ActiveX VB6 DLL)
    *************** *************** *********

    (VB6 Side)

    Type VB6Struct
    d1 as double
    d2 as double
    l1 as long
    End Type


    (C# Side)

    struct CSharpStruct
    {
    double d1;
    double d2;
    double d3;
    long l1;
    }
    private VB6AX.CTest AXInstance; // ActiveX Class Instance

    AXInstance = new VB6AX.CTest(); // Instanciate vb6 class

    AXInstance.VB6S truct myData = new AXInstance.VB6S truct();
    AXInstance.Fill Struct(ref myData);
    // (FillStruct is a vb6 class method to fill the structure)

    IT WORKS, BUT...IF I DO
    CSharpStruct myData = new CSharpStruct();
    AXInstance.Fill Struct(ref myData);

    I have a
    'cannot convert from ref CSharpStruct to ref AXInstance.VB6S truct'.

    How to convert the ActiveX DLL Struct to the C# managed struct ?
    The end of the story is that I would like my VB6 ActiveX DLL to fill an
    array of structure, called by the C# client.
    The filled array of structure should be a 'managed' array of struct so I
    do have fast access speed to its elements later on in the application.

    -> How to convert/mashal a VB6 Array Of Struct to a C# Array of Struct
    (the C# array should be definied as an array of managed struct and not
    through the ActiveX unmanaged structure declaration)

    VB6AX.VB6Struct[] array1 = null; // array of unmanaged VB6 Struct
    CSharpStruct[] array2 = null; // array of C# managed Struct
    // bot struct represents the same thg


    -> How to convert a ref VB6AX.VB6Struct to a ref CSharpStruct ?

    -> How to convert a ref VB6AX.VB6Struct[] to a ref CSharpStruct[] ?


    Can you help ?


    Thanks.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Mashaling a VB6 Array Of Struct to C# Array of Struct

    Cybertof,

    The VB6Struct that is defined in the interop assembly is actually a
    managed structure. When you receive the array back from the call to the
    method, the structure and the array are both managed, no different from any
    other array or structure, as the marshaller has already done the conversion
    for you. There is no need to create another definition, as they will work
    exactly the same.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Cybertof" <cybertofNOSPAM @ifrance.com> wrote in message
    news:MPG.1cc8bc 5383fc87f698968 a@news.wanadoo. fr...[color=blue]
    > Hello,
    >
    > Is it possible to convert a VB6 Array of Struct
    > to
    > a C# Array Of Struct ?
    >
    > The test context is a C# application calling a VB6 ActiveX DLL Function
    > using UDT (User Defined Type) and array of UDT.
    >
    >
    > *************** *************** *********
    > Example : (VB6AX is an ActiveX VB6 DLL)
    > *************** *************** *********
    >
    > (VB6 Side)
    >
    > Type VB6Struct
    > d1 as double
    > d2 as double
    > l1 as long
    > End Type
    >
    >
    > (C# Side)
    >
    > struct CSharpStruct
    > {
    > double d1;
    > double d2;
    > double d3;
    > long l1;
    > }
    > private VB6AX.CTest AXInstance; // ActiveX Class Instance
    >
    > AXInstance = new VB6AX.CTest(); // Instanciate vb6 class
    >
    > AXInstance.VB6S truct myData = new AXInstance.VB6S truct();
    > AXInstance.Fill Struct(ref myData);
    > // (FillStruct is a vb6 class method to fill the structure)
    >
    > IT WORKS, BUT...IF I DO
    > CSharpStruct myData = new CSharpStruct();
    > AXInstance.Fill Struct(ref myData);
    >
    > I have a
    > 'cannot convert from ref CSharpStruct to ref AXInstance.VB6S truct'.
    >
    > How to convert the ActiveX DLL Struct to the C# managed struct ?
    > The end of the story is that I would like my VB6 ActiveX DLL to fill an
    > array of structure, called by the C# client.
    > The filled array of structure should be a 'managed' array of struct so I
    > do have fast access speed to its elements later on in the application.
    >
    > -> How to convert/mashal a VB6 Array Of Struct to a C# Array of Struct
    > (the C# array should be definied as an array of managed struct and not
    > through the ActiveX unmanaged structure declaration)
    >
    > VB6AX.VB6Struct[] array1 = null; // array of unmanaged VB6 Struct
    > CSharpStruct[] array2 = null; // array of C# managed Struct
    > // bot struct represents the same thg
    >
    >
    > -> How to convert a ref VB6AX.VB6Struct to a ref CSharpStruct ?
    >
    > -> How to convert a ref VB6AX.VB6Struct[] to a ref CSharpStruct[] ?
    >
    >
    > Can you help ?
    >
    >
    > Thanks.[/color]


    Comment

    • Cybertof

      #3
      Re: Mashaling a VB6 Array Of Struct to C# Array of Struct

      In article <#qNHpRRQFHA.33 36@TK2MSFTNGP10 .phx.gbl>,
      mvp@spam.guard. caspershouse.co m says...[color=blue]
      > Cybertof,
      >
      > The VB6Struct that is defined in the interop assembly is actually a
      > managed structure. When you receive the array back from the call to the
      > method, the structure and the array are both managed, no different from any
      > other array or structure, as the marshaller has already done the conversion
      > for you. There is no need to create another definition, as they will work
      > exactly the same.
      >
      > Hope this helps.
      >
      >
      >[/color]

      Thanks Nicholas, but :

      If i want my C# application to work only with my Struct defined on the
      C# side, how can i pass my
      ref ArrayOfC#SideSt ruct[]
      to the vb6 activex function which is waiting for a
      ref ArrayOfInternal VB6StructDefini edInsindeAXClas s[]

      ?

      I would like my application to work with array of items and i would like
      the type of these items not depending on the type inside the activex
      dll.

      Is it possible ?


      Thanks,
      Cybertof.

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Mashaling a VB6 Array Of Struct to C# Array of Struct

        Cybertof,

        The only way you could do this would be to write a custom marshaller, or
        a wrapper for the VB component that would transform the results from the
        structure defined in the interop assembly.

        The thing is, why? For asthetic reasons? There is absolutely NO
        difference between the two structures, with the exception of the name.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Cybertof" <cybertofNOSPAM @ifrance.com> wrote in message
        news:MPG.1cc8c4 0d69ea87eb98968 c@news.wanadoo. fr...[color=blue]
        > In article <#qNHpRRQFHA.33 36@TK2MSFTNGP10 .phx.gbl>,
        > mvp@spam.guard. caspershouse.co m says...[color=green]
        >> Cybertof,
        >>
        >> The VB6Struct that is defined in the interop assembly is actually a
        >> managed structure. When you receive the array back from the call to the
        >> method, the structure and the array are both managed, no different from
        >> any
        >> other array or structure, as the marshaller has already done the
        >> conversion
        >> for you. There is no need to create another definition, as they will
        >> work
        >> exactly the same.
        >>
        >> Hope this helps.
        >>
        >>
        >>[/color]
        >
        > Thanks Nicholas, but :
        >
        > If i want my C# application to work only with my Struct defined on the
        > C# side, how can i pass my
        > ref ArrayOfC#SideSt ruct[]
        > to the vb6 activex function which is waiting for a
        > ref ArrayOfInternal VB6StructDefini edInsindeAXClas s[]
        >
        > ?
        >
        > I would like my application to work with array of items and i would like
        > the type of these items not depending on the type inside the activex
        > dll.
        >
        > Is it possible ?
        >
        >
        > Thanks,
        > Cybertof.[/color]


        Comment

        • Cybertof

          #5
          Re: Mashaling a VB6 Array Of Struct to C# Array of Struct

          In article <ehVatbRQFHA.39 88@tk2msftngp13 .phx.gbl>,
          mvp@spam.guard. caspershouse.co m says...[color=blue]
          > Cybertof,
          >
          > The only way you could do this would be to write a custom marshaller, or
          > a wrapper for the VB component that would transform the results from the
          > structure defined in the interop assembly.
          >
          > The thing is, why? For asthetic reasons? There is absolutely NO
          > difference between the two structures, with the exception of the name.
          >
          >
          >[/color]

          Yes for 'asthetic' coding reasons, not to see the vb6ax interop name,
          when accessing an inner data through the structure.

          Would making a custom marshaller/wrapper would cost additional
          processing time somewhere ?

          The only thing i'm looking for is, how to pass a
          ref CSharpStruct[] array of struct
          to a function inside my ActiveXDll which is waiting for a
          ref VB6AX.VB6Struct[] array of 'inner vb6ax struct


          I actually have a
          'Cannot convert ref CSharpStruct[] to ref VB6AX.VB6Struct[]'


          Regards,
          Cybertof.

          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Mashaling a VB6 Array Of Struct to C# Array of Struct

            Cybertof,

            Creating a marshaller is overkill in this situation. I would just
            create another class which wraps access to the COM object in VB6 and
            converts the arguments as they come in and out.


            --
            - Nicholas Paldino [.NET/C# MVP]
            - mvp@spam.guard. caspershouse.co m

            "Cybertof" <cybertofNOSPAM @ifrance.com> wrote in message
            news:MPG.1cc8c7 b1c8976bd198968 d@news.wanadoo. fr...[color=blue]
            > In article <ehVatbRQFHA.39 88@tk2msftngp13 .phx.gbl>,
            > mvp@spam.guard. caspershouse.co m says...[color=green]
            >> Cybertof,
            >>
            >> The only way you could do this would be to write a custom marshaller,
            >> or
            >> a wrapper for the VB component that would transform the results from the
            >> structure defined in the interop assembly.
            >>
            >> The thing is, why? For asthetic reasons? There is absolutely NO
            >> difference between the two structures, with the exception of the name.
            >>
            >>
            >>[/color]
            >
            > Yes for 'asthetic' coding reasons, not to see the vb6ax interop name,
            > when accessing an inner data through the structure.
            >
            > Would making a custom marshaller/wrapper would cost additional
            > processing time somewhere ?
            >
            > The only thing i'm looking for is, how to pass a
            > ref CSharpStruct[] array of struct
            > to a function inside my ActiveXDll which is waiting for a
            > ref VB6AX.VB6Struct[] array of 'inner vb6ax struct
            >
            >
            > I actually have a
            > 'Cannot convert ref CSharpStruct[] to ref VB6AX.VB6Struct[]'
            >
            >
            > Regards,
            > Cybertof.[/color]


            Comment

            Working...