0

I am taking my first steps using P/Invoke and try to represent these C/C++ structs:

#ifndef struct_emxArray_char_T_1024
#define struct_emxArray_char_T_1024
struct emxArray_char_T_1024
{
    char_T data[1024];
    int32_T size[1];
};

#ifndef typedef_e_struct_T
#define typedef_e_struct_T
typedef struct
{
    emxArray_char_T_1024 value1;
    real_T value2;
    uint32_T value3;
    boolean_T value4;
} e_struct_T;

using this in C#:

[StructLayout(LayoutKind.Sequential)]
class emxArray_char_T_1024
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
    public string data;
    [In, MarshalAs(UnmanagedType.I4)]
    public int size;
}

StructLayout(LayoutKind.Sequential)]
class e_struct_T
{
    emxArray_char_T_1024 value1;
    double value2;
    uint value3;
    bool value4;
}

Does this look sufficient? I am not too sure about the comments like this in the tutorial:

compile with: /target:module

PS:

The above 'types' seem to be defined like this:

typedef double real_T;
typedef unsigned int uint32_T;
typedef unsigned char boolean_T;
typedef char char_T;
typedef int int32_T;
6
  • Just to start: how real_T is defined? float or double? boolean_T? byte or int? char_T? wchar_t or char? Commented Dec 2, 2012 at 10:35
  • Thanks Adriano. Did not realize that these 'types' are not common. Had a look in the header files and found the stuff in PS (see edited question). Commented Dec 2, 2012 at 10:44
  • OK, System.Boolean is 4 bytes so it has be decorared with MarshalAs. Everything else looks OK to me. Commented Dec 2, 2012 at 10:45
  • Thanks. Would you mind posting an answer then with the correct attribute for Boolean please? Commented Dec 2, 2012 at 10:53
  • You can use [MarshalAs(UnmanagedType.I1)] Commented Dec 2, 2012 at 11:00

1 Answer 1

1

Final struct looks OK to me, the only change you should do is how your boolean_T is marshaled. Default C-style bool is one byte signed integer so it must be marshaled as I1. You declared boolean_T it as unsigned char so it should be U1:

[StructLayout(LayoutKind.Sequential)]
class e_struct_T
{
    emxArray_char_T_1024 value1;
    double value2;
    uint value3;

    [MarshalAs(UnmanagedType.U1)] 
    bool value4;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am having a closer look at this. value2 seems to be always zero. Maybe it is too naive to assume that I can just use double for real_T? Thanks.
No, it should be OK. Try to replace value2 with, for example, an int to check what you get there. It may be a disalignment of the other struct.
Mmmh, just looking at emxArray_char_T_1024. Is int32_T size[1] -> [In, MarshalAs(UnmanagedType.I4)] public int size actaully correct?
By chance I thought it could be, can you try with [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] public int[] size?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.