Re: Error on StructureToPtr()

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
.