Marshal.PtrToStructure skipping bytes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spamacon@gmail.com

    #1

    Marshal.PtrToStructure skipping bytes

    Hello,
    I have a strange situation using .Net FW 1.1. I want to use
    Marshal.PtrToSt ructure to fill the structure below. The first 3 fields
    get filled correctly: ulStruct describes how big the structure is in
    bytes (120 bytes, assuming 32-bit IntPtrs), pWmSnapshot gets 0, and
    usNumWmSnapshot s gets 0. The next field, pICView, is a pointer to
    another structure, and gets filled with some garbage location, which is
    actually the lower 2 bytes of the real location, and the 2 bytes of the
    next field, usNumViews. The usNumViews field is then filled with the 2
    bytes that were supposed to go into usItemType, and all subsequent
    fields are off by those 2 bytes.

    If I manually extract the structure from the pointer, using
    Marshal.ReadInt 32, ReadInt16, ReadIntPtr, and PtrToStringAnsi ,
    everything is fine. That is, if I do Marshal.ReadInt Ptr(ptr_to_stru ct,
    10) it reads the correct location for pICView.

    I've tried making the pICView field an integer (MarshalAs U4), and it
    skips the same 2 bytes.

    I haven't had any other problems using PtrToStructure on other
    structures, just this one hiccup. Any ideas?

    Thanks,
    Adam

    Structure:

    <StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)_
    Public Structure SNAPSHOTSTRUCT
    <MarshalAs(Unma nagedType.U4)Pu blic ulStruct As Integer
    Public pWmSnapshot As IntPtr 'PWMSNAPSHOTSTR UCT
    <MarshalAs(Unma nagedType.U2)Pu blic usNumWmSnapshot s As Short
    Public pICView As IntPtr 'PICVIEWSTRUCT
    <MarshalAs(Unma nagedType.U2)Pu blic usNumViews As Short
    <MarshalAs(Unma nagedType.U2)Pu blic usItemType As Short
    <MarshalAs(Unma nagedType.U4)Pu blic ulOpenStatus As Integer
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=17)P ublic
    szItemID As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    useridCheckout As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    tsCreate As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    tsModify As String
    End Structure

  • spamacon@gmail.com

    #2
    Re: Marshal.PtrToSt ructure skipping bytes

    Well, to respond to my own extremely popular topic, I found the
    problem. The combination of integers, integer pointers, and shorts
    (2-bytes) required me to set the packing on the structure to 2, instead
    of the default 8. I can only surmise that the 3rd field, which is a
    short, was properly getting only the 2 bytes, but was padded out to the
    next 2. Set the packing attribute on the structure solves the problem.

    spamacon@gmail. com wrote:
    Hello,
    I have a strange situation using .Net FW 1.1. I want to use
    Marshal.PtrToSt ructure to fill the structure below. The first 3 fields
    get filled correctly: ulStruct describes how big the structure is in
    bytes (120 bytes, assuming 32-bit IntPtrs), pWmSnapshot gets 0, and
    usNumWmSnapshot s gets 0. The next field, pICView, is a pointer to
    another structure, and gets filled with some garbage location, which is
    actually the lower 2 bytes of the real location, and the 2 bytes of the
    next field, usNumViews. The usNumViews field is then filled with the 2
    bytes that were supposed to go into usItemType, and all subsequent
    fields are off by those 2 bytes.
    >
    If I manually extract the structure from the pointer, using
    Marshal.ReadInt 32, ReadInt16, ReadIntPtr, and PtrToStringAnsi ,
    everything is fine. That is, if I do Marshal.ReadInt Ptr(ptr_to_stru ct,
    10) it reads the correct location for pICView.
    >
    I've tried making the pICView field an integer (MarshalAs U4), and it
    skips the same 2 bytes.
    >
    I haven't had any other problems using PtrToStructure on other
    structures, just this one hiccup. Any ideas?
    >
    Thanks,
    Adam
    >
    Structure:
    >
    <StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Ansi)_
    Public Structure SNAPSHOTSTRUCT
    <MarshalAs(Unma nagedType.U4)Pu blic ulStruct As Integer
    Public pWmSnapshot As IntPtr 'PWMSNAPSHOTSTR UCT
    <MarshalAs(Unma nagedType.U2)Pu blic usNumWmSnapshot s As Short
    Public pICView As IntPtr 'PICVIEWSTRUCT
    <MarshalAs(Unma nagedType.U2)Pu blic usNumViews As Short
    <MarshalAs(Unma nagedType.U2)Pu blic usItemType As Short
    <MarshalAs(Unma nagedType.U4)Pu blic ulOpenStatus As Integer
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=17)P ublic
    szItemID As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    useridCheckout As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    tsCreate As String
    <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=27)P ublic
    tsModify As String
    End Structure

    Comment

    Working...