By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Marshal.SizeOF

P: n/a
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 in Houston
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Dennis" <De****@discussions.microsoft.com> schrieb:
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').

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

Nov 21 '05 #2

P: n/a
Thanks...I learn something everyday from you on this newsgroup.

--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
"Dennis" <De****@discussions.microsoft.com> schrieb:
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').

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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.