Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2007

Tip: Looking for answers? Try searching our database.

Error on StructureToPtr()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TNCoder - 01 Aug 2007 20:56 GMT
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
Mattias Sjögren - 01 Aug 2007 23:34 GMT
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.