Thursday, March 26, 2009

Pack:=1 and ASP.NET: The beginning

Uff, just fighted for two days of getting that byte-array into a structure. There were always strange values starting from a specific property. After a few hours, I found out that there was a off by 1 byte error: if I shift every byte back one byte, I will reach the right result. Code tells more than 1000 words:


Structure Test
Dim a As Byte
Dim b As Int16
Dim c as Byte
End Structure

Dim handle As GCHandle = GCHandle.Alloc(byteArray, _
GCHandleType.Pinned)
Dim t As New Test
t = CType(Marshal.PtrToStructure(_
handle.AddrOfPinnedObject(), _
GetType(Test)), Test)


This will give you false values for b and c. This is why: "The default behaviour in the framework is for values in the structure to be aligned at 4-byte boundaries, so values start at FieldOffset 0,4,8,12 ..." (see forum). To avoid this, use as a structure-attribute and everything's fine now :)

Another bad book read: Programmieren mit ASP.NET AJAX. The first chapter is good - everything else just confusing. No good examples. Maybe I didn't get the point - I haven't done anything in ASP.NET yet. I'm watching now and the first 6 videos were fine :)

Ah, I forgot to mention the free first chapter of Professional ASP.NET MVC 1.0 from Rob Conery, Scott Hanselman, Phil Haack and Scott "ScottGu" Guthrie - famous bloggers writing a book. I'm curious to read it :)

No comments:

Post a Comment