Click here to Skip to main content
15,895,283 members
Home / Discussions / C#
   

C#

 
AnswerRe: the offset is calculated by byte or by double bytes? Pin
sreejith ss nair27-Oct-04 18:32
sreejith ss nair27-Oct-04 18:32 
GeneralRe: the offset is calculated by byte or by double bytes? Pin
momer27-Oct-04 20:48
momer27-Oct-04 20:48 
GeneralRe: the offset is calculated by byte or by double bytes? Pin
Heath Stewart27-Oct-04 20:58
protectorHeath Stewart27-Oct-04 20:58 
GeneralRe: the offset is calculated by byte or by double bytes? Pin
momer27-Oct-04 21:30
momer27-Oct-04 21:30 
GeneralRe: the offset is calculated by byte or by double bytes? Pin
Heath Stewart27-Oct-04 21:41
protectorHeath Stewart27-Oct-04 21:41 
GeneralRe: the offset is calculated by byte or by double bytes? Pin
momer27-Oct-04 21:51
momer27-Oct-04 21:51 
GeneralRe:Stewart Pin
momer27-Oct-04 21:51
momer27-Oct-04 21:51 
AnswerRe: the offset is calculated by byte or by double bytes? Pin
Heath Stewart27-Oct-04 21:56
protectorHeath Stewart27-Oct-04 21:56 
You'll definitely get stack overflows and corruption with that one. You should read about and understand the data types you're using. A DWORD, for example, is 32 bits while a managed ulong is 64 bits. You would overflow the stack and cause undeterministic problems.

In most cases - as I mentioned in a previous reply to this thread - you only need to use LayoutKind.Explicit for a struct when a union is involved. In this case, however, since you have CPU-dependent types (namely LPVOID and DWORD_PTR, both 32 bits on a 32-bit CPU and 64 bits on a 64-bit CPU), it's better to declare nested structs (not supported on .NET CF currently). You would then use the LayoutKind.Sequential for SYSTEM_INFO and LayoutKind.Explicit for SYSTEM_INFO_Union1:
[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_INFO
{
  public SYSTEM_INFO_Union1 union1;
  public uint dwPageSize;
  public IntPtr lpMinimumApplicationAddress;
  public IntPtr lpMaximumApplicationAddress;
  public IntPtr dwActiveProcessorMask;
  public uint dwNumberOfProcessors;
  public uint dwAllocationGranularity;
  public ushort wProcessLevel;
  public ushort wPRocessRevision;
}
 
[StructLayout(LayoutKind.Explicit, Size=4)]
internal struct SYSTEM_INFO_Union1
{
  [FieldOffset(0)] public uint dwOemId;
  [FieldOffset(0)] public ushort wProcessorArchitecture;
  [FieldOffset(2)] public ushort wReserved;
}
Specifying the StructLayoutAttribute.Size field in the attribute speeds up calculations when marshaling and is a good idea in cases where unions must be used, but it's not necessary (don't do it if you must define reference fields of IntPtrs, which all depend on the size of an address for a particular CPU.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralXML Elements with escape characters in their names Pin
Guillermo Jimenez27-Oct-04 16:53
Guillermo Jimenez27-Oct-04 16:53 
GeneralRe: XML Elements with escape characters in their names Pin
Nick Parker27-Oct-04 17:50
protectorNick Parker27-Oct-04 17:50 
GeneralRe: XML Elements with escape characters in their names Pin
Guillermo Jimenez27-Oct-04 18:11
Guillermo Jimenez27-Oct-04 18:11 
GeneralRe: XML Elements with escape characters in their names Pin
Heath Stewart27-Oct-04 20:57
protectorHeath Stewart27-Oct-04 20:57 
GeneralColor shading like the Windows Media Player v10 Pin
Tony D. Abel27-Oct-04 15:57
Tony D. Abel27-Oct-04 15:57 
GeneralRe: Color shading like the Windows Media Player v10 Pin
Nick Parker27-Oct-04 17:53
protectorNick Parker27-Oct-04 17:53 
GeneralDataSet designer Pin
Corneliu Tusnea27-Oct-04 13:51
Corneliu Tusnea27-Oct-04 13:51 
GeneralRe: DataSet designer Pin
sreejith ss nair27-Oct-04 18:35
sreejith ss nair27-Oct-04 18:35 
GeneralRe: DataSet designer Pin
Corneliu Tusnea27-Oct-04 19:48
Corneliu Tusnea27-Oct-04 19:48 
Questionwhat's your number? Pin
Sled Dog27-Oct-04 11:54
Sled Dog27-Oct-04 11:54 
QuestionProtection level ? Pin
wtdoor196527-Oct-04 10:13
wtdoor196527-Oct-04 10:13 
AnswerRe: Protection level ? Pin
Christian Graus27-Oct-04 10:16
protectorChristian Graus27-Oct-04 10:16 
AnswerRe: Protection level ? Pin
sreejith ss nair27-Oct-04 18:19
sreejith ss nair27-Oct-04 18:19 
GeneralRe: Protection level ? Pin
Heath Stewart27-Oct-04 21:02
protectorHeath Stewart27-Oct-04 21:02 
GeneralGet the IP from HttpWebRequest Pin
Justin Garrick27-Oct-04 10:09
Justin Garrick27-Oct-04 10:09 
GeneralRe: Get the IP from HttpWebRequest Pin
Justin Garrick27-Oct-04 15:43
Justin Garrick27-Oct-04 15:43 
GeneralRe: Get the IP from HttpWebRequest Pin
Nick Parker27-Oct-04 17:12
protectorNick Parker27-Oct-04 17:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.