Marshal.SizeOF

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis

    #1

    Marshal.SizeOF

    I have a structure:
    <StructLayout(L ayoutKind.Seque ntial)> Friend Structure KEYBDINPUT
    Dim wVK As Byte
    Dim wScan As Byte
    Dim dwFlags As Integer
    Dim time As Integer
    Dim dwExtraInfo As Integer
    End Structure
    dim k as KEYBDINPUT
    When I use Marshal.sizeof( k), I get the size 16 whidh is two more bytes than
    the structure but when I use Len(k), I get the correct number of 14. What is
    going on? I'm confused
    --
    Dennis in Houston
  • Herfried K. Wagner [MVP]

    #2
    Re: Marshal.SizeOF

    "Dennis" <Dennis@discuss ions.microsoft. com> schrieb:[color=blue]
    >I have a structure:
    > <StructLayout(L ayoutKind.Seque ntial)> Friend Structure KEYBDINPUT
    > Dim wVK As Byte
    > Dim wScan As Byte
    > Dim dwFlags As Integer
    > Dim time As Integer
    > Dim dwExtraInfo As Integer
    > End Structure
    > dim k as KEYBDINPUT
    > When I use Marshal.sizeof( k), I get the size 16 whidh is two more bytes
    > than
    > the structure but when I use Len(k), I get the correct number of 14. What
    > is
    > going on? I'm confused[/color]


    Structure members are aligned at 'DWORD' boundaries:

    1: 'wVK'
    2: 'wScan'
    3--4: Empty
    4--8: 'dwFlags'
    8--12: 'time'
    12--16: 'dwExtraInfo'

    You can control the packing by specifying a value for 'Pack' in
    'StructLayout':

    \\\
    <StructLayout(L ayoutKind.Seque ntial, Pack:=1)> _
    ...
    ///

    For unmanaged structures the default packing size is 4, which is most common
    in the Win32 API (with some exceptions like 'SHFILEOPSTRUCT ').

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Dennis

      #3
      Re: Marshal.SizeOF

      Thanks...I learn something everyday from you on this newsgroup.

      --
      Dennis in Houston


      "Herfried K. Wagner [MVP]" wrote:
      [color=blue]
      > "Dennis" <Dennis@discuss ions.microsoft. com> schrieb:[color=green]
      > >I have a structure:
      > > <StructLayout(L ayoutKind.Seque ntial)> Friend Structure KEYBDINPUT
      > > Dim wVK As Byte
      > > Dim wScan As Byte
      > > Dim dwFlags As Integer
      > > Dim time As Integer
      > > Dim dwExtraInfo As Integer
      > > End Structure
      > > dim k as KEYBDINPUT
      > > When I use Marshal.sizeof( k), I get the size 16 whidh is two more bytes
      > > than
      > > the structure but when I use Len(k), I get the correct number of 14. What
      > > is
      > > going on? I'm confused[/color]
      >
      >
      > Structure members are aligned at 'DWORD' boundaries:
      >
      > 1: 'wVK'
      > 2: 'wScan'
      > 3--4: Empty
      > 4--8: 'dwFlags'
      > 8--12: 'time'
      > 12--16: 'dwExtraInfo'
      >
      > You can control the packing by specifying a value for 'Pack' in
      > 'StructLayout':
      >
      > \\\
      > <StructLayout(L ayoutKind.Seque ntial, Pack:=1)> _
      > ...
      > ///
      >
      > For unmanaged structures the default packing size is 4, which is most common
      > in the Win32 API (with some exceptions like 'SHFILEOPSTRUCT ').
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>
      >
      >[/color]

      Comment

      Working...