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
>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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
TNCoder - 07 Aug 2007 01:10 GMT
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
> >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.
[quoted text clipped - 19 lines]
>
> Mattias
Mattias Sjögren - 07 Aug 2007 07:01 GMT
>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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
TNCoder - 07 Aug 2007 19:48 GMT
thanks, that fixed it.
Terrt
> >Setting it to a string then changes the size of the structure making it
> >variable as the size of the string changes.
[quoted text clipped - 3 lines]
>
> Mattias