array of struct

G

Guest

in c I had this structure;

typedef struct _TABLE_LIST {
UCHAR ValidEntries;
STATUS_PACKET Status[50];
} TABLE_LIST, *P_TABLE_LIST;

How do I do the same way in c# with the structures I had below?

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct STATUS_PACKET
{
public ETHERNET_HEADER etherHeader;//14
public GRE_HEADER greHeader;//4
public byte Type;//1
public FILL_BYTES fillBytes;//12
public REVISIONS o_rev;//8
public REVISIONS a_rev;//8
public SWITCH Name;//22
}

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct VALIDENTRY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte NumValidEntries;
}
 
V

Vadym Stetsyak

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct TABLE_LIST
{
[MarshalAs(UnmanagedType.U1)]
char ValidEntries;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
STATUS_PACKET [] Status;
}
 
G

Guest

I tried that an it gave me error:
"Unhandled exception of type 'System.Argument Exception' occurred in MyApp.exe
Additional information: TABLE_LIST can not be marshaled as an unmanaged
structure; no meaningful size or off set can be computed."

when I do this:
TABLE_LIST myStatus = new TABLE_LIST();

int SizeOfBuff = (int)(Marshal.SizeOf(myStatus)); <====this is where the
error occured.

Have any ideal?
thanks.


Vadym Stetsyak said:
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct TABLE_LIST
{
[MarshalAs(UnmanagedType.U1)]
char ValidEntries;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
STATUS_PACKET [] Status;
}

--
Vadym Stetsyak aka Vadmyst

ttan said:
in c I had this structure;

typedef struct _TABLE_LIST {
UCHAR ValidEntries;
STATUS_PACKET Status[50];
} TABLE_LIST, *P_TABLE_LIST;

How do I do the same way in c# with the structures I had below?

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct STATUS_PACKET
{
public ETHERNET_HEADER etherHeader;//14
public GRE_HEADER greHeader;//4
public byte Type;//1
public FILL_BYTES fillBytes;//12
public REVISIONS o_rev;//8
public REVISIONS a_rev;//8
public SWITCH Name;//22
}

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct VALIDENTRY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte NumValidEntries;
}
 
V

Vadym Stetsyak

There are 2 ways:
First:
Specify Size of the structure in TABLE_LIST StructLayout and try again and
if this will not help try the second way.

Second:
( http://www.dotnetinterop.com/faq/?q=StructWithStructArray )
( http://www.dotnetinterop.com/faq/default.aspx?q=VariableLengthStruct )

Watch for the word wrap...


--
Vadym Stetsyak aka Vadmyst

ttan said:
I tried that an it gave me error:
"Unhandled exception of type 'System.Argument Exception' occurred in MyApp.exe
Additional information: TABLE_LIST can not be marshaled as an unmanaged
structure; no meaningful size or off set can be computed."

when I do this:
TABLE_LIST myStatus = new TABLE_LIST();

int SizeOfBuff = (int)(Marshal.SizeOf(myStatus)); <====this is where the
error occured.

Have any ideal?
thanks.


Vadym Stetsyak said:
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct TABLE_LIST
{
[MarshalAs(UnmanagedType.U1)]
char ValidEntries;

[MarshalAs(UnmanagedType.ByValArray, SizeConst=50)]
STATUS_PACKET [] Status;
}

--
Vadym Stetsyak aka Vadmyst

ttan said:
in c I had this structure;

typedef struct _TABLE_LIST {
UCHAR ValidEntries;
STATUS_PACKET Status[50];
} TABLE_LIST, *P_TABLE_LIST;

How do I do the same way in c# with the structures I had below?

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct STATUS_PACKET
{
public ETHERNET_HEADER etherHeader;//14
public GRE_HEADER greHeader;//4
public byte Type;//1
public FILL_BYTES fillBytes;//12
public REVISIONS o_rev;//8
public REVISIONS a_rev;//8
public SWITCH Name;//22
}

[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]
public struct VALIDENTRY
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte NumValidEntries;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top