This type can not be marshaled as a structure field

  • Thread starter Chakravarthy Bollapalli \(IFIN SCC COM\)
  • Start date
C

Chakravarthy Bollapalli \(IFIN SCC COM\)

Hi,
I get the exception

====exception=======
An unhandled exception of type 'System.TypeLoadException' occurred in
Test.exe

Additional information: Can not marshal field si of type ServersOnHost: This
type can not be marshaled as a structure field.
====exception=======

thrown when i try to collect the following structure returned by a method
in a managed c++ code.

c++ version:

struct servers_on_host_list_t {
u32 n_servers; /* Number of servers found */
server_info_t si[16];
};

c# version:

[StructLayout(LayoutKind.Sequential)]
public class ServersOnHost
{
public uint n_servers; /* Number of servers found */
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16,
ArraySubType=UnmanagedType.Struct)]
public ServerInfo[] si;
};


I have searched all over the internet but couldnt find a solution to this.
Any pointer on how to solve it would be of great help.

TIA

kalyan
 
R

Robert Jordan

c# version:
[StructLayout(LayoutKind.Sequential)]
public class ServersOnHost
{
public uint n_servers; /* Number of servers found */
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16,
ArraySubType=UnmanagedType.Struct)]
public ServerInfo[] si; ========================
};


I have searched all over the internet but couldnt find a solution to this.
Any pointer on how to solve it would be of great help.

Structs within structs cannot be automatically marshaled.

http://www.dotnetinterop.com/faq/?q=StructWithStructArray

bye
Rob
 

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