Structure array inside structure when passing reference to C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mikko Penkkimäki

    #1

    Structure array inside structure when passing reference to C++

    I have a struct in C#-project and C++-project. I use the struct in C# but it
    has to be filled in C++.

    I know that arrays work like this:

    In C++ code:

    struct teststruct {
    char str[100];
    float myvar[2];
    };

    CPPPROJECTDLLDE CL int DLLCALL callteststruct( teststruct *data) {
    strcpy(data->str, "hello");
    data->myvar[0] = 123.45f;
    data->myvar[1] = 543.21f;
    return 0;
    }

    In C# code:

    using System.Runtime. InteropServices ;

    [Serializable()]
    [StructLayout(La youtKind.Sequen tial, Pack=1, CharSet=CharSet .Ansi)]
    public struct teststruct {
    [MarshalAs(Unman agedType.ByValT Str, SizeConst=100)]
    public string str;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=2)]
    public float[] myvar;
    };

    [DllImport("CppP roject.dll")]
    static public extern unsafe int callteststruct( ref teststruct data);

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    teststruct data = new teststruct();
    callteststruct( ref data);
    }


    Now I need to add a structure array inside the "teststruct ". It should look
    something like this in C++:

    struct teststruct2 {
    int intvalue;
    int anotherintvalue
    char text[100];
    }

    struct teststruct {
    char str[100];
    float myvar[2];
    teststruct2 teststruct2arra y[100];
    };

    But how to do it in C#?
    I really would appreciate some help.
    Thanks.

    Mikko


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Structure array inside structure when passing reference to C++

    Mikko,

    You would do it the same way. First, declare the teststruct2 structure,
    like so:

    [StructLayout(La youtKind.Sequen tial, Pack=1, CharSet=CharSet .Ansi)]
    struct teststruct2
    {
    int intvalue;
    int anotherintvalue
    [MarshalAs(Unman agedType.ByValT Str, SizeConst=100)]
    char text[100];
    }

    Then alter your definition of teststruct like so:

    [StructLayout(La youtKind.Sequen tial, Pack=1, CharSet=CharSet .Ansi)]
    public struct teststruct
    {
    [MarshalAs(Unman agedType.ByValT Str, SizeConst=100)]
    public string str;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=2)]
    public float[] myvar;
    [MarshalAs(Unman agedType.ByValA rray, SizeConst=100)]
    public teststruct2 teststruct2arra y[];
    }

    Hope this helps.


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

    "Mikko Penkkimäki" <mpenkkim@hotma il.com> wrote in message
    news:3f9fd57e$0 $8743$1b6aedd2@ news.songnet.fi ...[color=blue]
    > I have a struct in C#-project and C++-project. I use the struct in C# but[/color]
    it[color=blue]
    > has to be filled in C++.
    >
    > I know that arrays work like this:
    >
    > In C++ code:
    >
    > struct teststruct {
    > char str[100];
    > float myvar[2];
    > };
    >
    > CPPPROJECTDLLDE CL int DLLCALL callteststruct( teststruct *data) {
    > strcpy(data->str, "hello");
    > data->myvar[0] = 123.45f;
    > data->myvar[1] = 543.21f;
    > return 0;
    > }
    >
    > In C# code:
    >
    > using System.Runtime. InteropServices ;
    >
    > [Serializable()]
    > [StructLayout(La youtKind.Sequen tial, Pack=1, CharSet=CharSet .Ansi)]
    > public struct teststruct {
    > [MarshalAs(Unman agedType.ByValT Str, SizeConst=100)]
    > public string str;
    > [MarshalAs(Unman agedType.ByValA rray, SizeConst=2)]
    > public float[] myvar;
    > };
    >
    > [DllImport("CppP roject.dll")]
    > static public extern unsafe int callteststruct( ref teststruct data);
    >
    > private void Page_Load(objec t sender, System.EventArg s e)
    > {
    > teststruct data = new teststruct();
    > callteststruct( ref data);
    > }
    >
    >
    > Now I need to add a structure array inside the "teststruct ". It should[/color]
    look[color=blue]
    > something like this in C++:
    >
    > struct teststruct2 {
    > int intvalue;
    > int anotherintvalue
    > char text[100];
    > }
    >
    > struct teststruct {
    > char str[100];
    > float myvar[2];
    > teststruct2 teststruct2arra y[100];
    > };
    >
    > But how to do it in C#?
    > I really would appreciate some help.
    > Thanks.
    >
    > Mikko
    >
    >[/color]


    Comment

    Working...