473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Marshal.SizeOF

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
2 2041
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
2
by: Beringer | last post by:
Why do I get the following run time error: Additional information: Type System.Object can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. When the...
6
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented...
1
by: Claire | last post by:
char a = 'p'; if (System.Runtime.InteropServices.Marshal.SizeOf(a) == 1) dothis(); else dothat(); SizeOf(a) is returned as 1. I thought chars were 16 bits in size. Why is it returning 1 to me?...
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
4
by: marcosegurini | last post by:
Hi. Is is possible to mark a class-member-variable to avoid its marshaling? class MyClass { int i_; IntPtr point_;
2
by: scottt | last post by:
I need to call into a C++ DLL from my C# code. The function is expecting a void pointer to an unsigned short. Which would be more correct? UInt16 wRegData; IntPtr p =...
2
by: RYoung | last post by:
Given this native struct: typedef struct vendor { char name; } VENDOR I want to make managed equivalent, so I did this: public value struct Vendor
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
1
by: Charming12 | last post by:
Hi All, I am using System.Runtime.InteropServices; to marshal a structure using Marshal.structureToPtr(). But to get the size of structure when i get Marshal.sizeOf(), it gives me improper sizes....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.