Error on StructureToPtr()

G

Guest

I'm trying to copy a structure to a byte array. I get a failure on the
Marshal.StructureToPtr() function. The structure which is as follows:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CONNECT_STRUCT
{
public byte Format;
public short sNumBytes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=25)]
public byte[] szDatabase;
public byte cCommand;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
public byte[] szVerification;
}

CONNECT_STRUCT pConnectStruct = new CONNECT_STRUCT();

pConnectStruct.Format = 2;
pConnectStruct.cCommand = 4;
pConnectStruct.sNumBytes = (short)Marshal.SizeOf(pConnectStruct);
pConnectStruct.szDatabase = StrToByteArray("MY_DATABASE");
pConnectStruct.szVerification = StrToByteArray("ABCDEFGHIJKLMNOP");

int len = Marshal.SizeOf(pConnectStruct);

byte[] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(pConnectStruct, ptr, true); <===FAILS

It fails with: Type could not be marshaled because the length of an embedded
array instance does not match the declared length in the layout.


I compared "len" with the actual size of the structure and it is correct.
Any ideas?

Thanks,
Terry
 
M

Mattias Sjögren

It fails with: Type could not be marshaled because the length of an embedded
array instance does not match the declared length in the layout.


I compared "len" with the actual size of the structure and it is correct.
Any ideas?


It's the size of the byte array you assign to szDatabase or
szVerification that's incorrect.

Since the members apparently are strings, I'd recommend declaring them
that way to save you some work.

....
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=25)]
public string szDatabase;
public byte cCommand;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szVerification;
....
pConnectStruct.szDatabase = "MY_DATABASE";
pConnectStruct.szVerification = "ABCDEFGHIJKLMNOP";


Mattias
 
G

Guest

Setting it to a string then changes the size of the structure making it
variable as the size of the string changes. The structure size must remain
fixed as I'm sending it over the Internet to a server which requires a fixed
size structure.

Terry

Mattias Sjögren said:
It fails with: Type could not be marshaled because the length of an embedded
array instance does not match the declared length in the layout.


I compared "len" with the actual size of the structure and it is correct.
Any ideas?


It's the size of the byte array you assign to szDatabase or
szVerification that's incorrect.

Since the members apparently are strings, I'd recommend declaring them
that way to save you some work.

....
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=25)]
public string szDatabase;
public byte cCommand;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szVerification;
....
pConnectStruct.szDatabase = "MY_DATABASE";
pConnectStruct.szVerification = "ABCDEFGHIJKLMNOP";


Mattias
 
M

Mattias Sjögren

Setting it to a string then changes the size of the structure making it
variable as the size of the string changes.

No it doesn't, not when you specify
MarshalAs(UnmanagedType.ByValTStr).


Mattias
 

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