Marshal.SizeOF

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a structure:
<StructLayout(LayoutKind.Sequential)> 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 said:
I have a structure:
<StructLayout(LayoutKind.Sequential)> 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


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(LayoutKind.Sequential, Pack:=1)> _
...
///

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top