|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Overlaping structureI have an little problem and need an helping hand. I want like in C++ create an Array and cast it to a struct, for easy access. Well that didn't work, so I take a look on the Attributes that can be used with struct. Some Attributes sounds good but didn't work like expected. I tried.... -------------------------- using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace TestStruct { class Program { [StructLayout(LayoutKind.Explicit, Size = 20)] struct test { [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 20)] public byte[] test1; [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, SizeConst = 5)] public int[] test2; } static void Main(string[] args) { test x; x.test1 = new byte[20]; x.test1[1] = 0xFF; x.test1[0] = 0xFF; x.test1[2] = 0xFF; x.test1[3] = 0xFF; } } } ---------------------------------- Well the int-Array should share the memory with the byte-Array, but it looks like the int-Array also gehts 20 elements of bytes that will cast to integer on acces the goal ist that after the last step the x.test2[0] = 0xFFFFFFFF. How is that going to work, or is it just imposible. Thanks Hello Michael,
I'm afraid all these attributes apply only when you marshal the structure to the unmanaged realm. They have no effect when you work with the structure exclusively in the managed code. Show quote "Michael Sch?ller" <swt***@yahoo.com> wrote in message news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... > Hello, > > I have an little problem and need an helping hand. > I want like in C++ create an Array and cast it to a struct, for easy > access. > Well that didn't work, so I take a look on the Attributes that can be used > with struct. > Some Attributes sounds good but didn't work like expected. > > I tried.... > -------------------------- > using System; > using System.Collections.Generic; > using System.Text; > using System.Runtime.InteropServices; > namespace TestStruct > { > class Program > { > [StructLayout(LayoutKind.Explicit, Size = 20)] > struct test > { > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.U1, SizeConst = 20)] > public byte[] test1; > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.I4, SizeConst = 5)] > public int[] test2; > } > > static void Main(string[] args) > { > test x; > x.test1 = new byte[20]; > x.test1[1] = 0xFF; > x.test1[0] = 0xFF; > x.test1[2] = 0xFF; > x.test1[3] = 0xFF; > } > } > } > ---------------------------------- > > Well the int-Array should share the memory with the byte-Array, but it > looks like the int-Array also gehts 20 elements of bytes that will cast to > integer on acces the goal ist that after the last step the x.test2[0] = > 0xFFFFFFFF. > > How is that going to work, or is it just imposible. > > > > Thanks > > Thanks for the Info.
Show quote "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> schrieb im Newsbeitrag news:%23r6VzaNTGHA.4900@TK2MSFTNGP09.phx.gbl... > Hello Michael, > > I'm afraid all these attributes apply only when you marshal the structure > to the unmanaged realm. They have no effect when you work with the > structure exclusively in the managed code. > > "Michael Schöller" <swt***@yahoo.com> wrote in message > news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... >> Hello, >> >> I have an little problem and need an helping hand. >> I want like in C++ create an Array and cast it to a struct, for easy >> access. >> Well that didn't work, so I take a look on the Attributes that can be >> used with struct. >> Some Attributes sounds good but didn't work like expected. >> >> I tried.... >> -------------------------- >> using System; >> using System.Collections.Generic; >> using System.Text; >> using System.Runtime.InteropServices; >> namespace TestStruct >> { >> class Program >> { >> [StructLayout(LayoutKind.Explicit, Size = 20)] >> struct test >> { >> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >> ArraySubType = UnmanagedType.U1, SizeConst = 20)] >> public byte[] test1; >> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >> ArraySubType = UnmanagedType.I4, SizeConst = 5)] >> public int[] test2; >> } >> >> static void Main(string[] args) >> { >> test x; >> x.test1 = new byte[20]; >> x.test1[1] = 0xFF; >> x.test1[0] = 0xFF; >> x.test1[2] = 0xFF; >> x.test1[3] = 0xFF; >> } >> } >> } >> ---------------------------------- >> >> Well the int-Array should share the memory with the byte-Array, but it >> looks like the int-Array also gehts 20 elements of bytes that will cast >> to integer on acces the goal ist that after the last step the x.test2[0] >> = 0xFFFFFFFF. >> >> How is that going to work, or is it just imposible. >> >> >> >> Thanks >> >> > Just for information.
I found out the my example works only the debugger is showing wrong values. If you make output via writeLine the expected values are shown up !!BUT!!!!!!!! The CLR thinks that the integer has a length of 20 an you can write data to that indexes. So with this code it is possible to write data on memory that is not owned by your application just like in good old pointer time.... Only that it is managed code (sounds realy bad) Show quote "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> schrieb im Newsbeitrag news:%23r6VzaNTGHA.4900@TK2MSFTNGP09.phx.gbl... > Hello Michael, > > I'm afraid all these attributes apply only when you marshal the structure > to the unmanaged realm. They have no effect when you work with the > structure exclusively in the managed code. > > "Michael Sch?ller" <swt***@yahoo.com> wrote in message > news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... >> Hello, >> >> I have an little problem and need an helping hand. >> I want like in C++ create an Array and cast it to a struct, for easy >> access. >> Well that didn't work, so I take a look on the Attributes that can be >> used with struct. >> Some Attributes sounds good but didn't work like expected. >> >> I tried.... >> -------------------------- >> using System; >> using System.Collections.Generic; >> using System.Text; >> using System.Runtime.InteropServices; >> namespace TestStruct >> { >> class Program >> { >> [StructLayout(LayoutKind.Explicit, Size = 20)] >> struct test >> { >> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >> ArraySubType = UnmanagedType.U1, SizeConst = 20)] >> public byte[] test1; >> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >> ArraySubType = UnmanagedType.I4, SizeConst = 5)] >> public int[] test2; >> } >> >> static void Main(string[] args) >> { >> test x; >> x.test1 = new byte[20]; >> x.test1[1] = 0xFF; >> x.test1[0] = 0xFF; >> x.test1[2] = 0xFF; >> x.test1[3] = 0xFF; >> } >> } >> } >> ---------------------------------- >> >> Well the int-Array should share the memory with the byte-Array, but it >> looks like the int-Array also gehts 20 elements of bytes that will cast >> to integer on acces the goal ist that after the last step the x.test2[0] >> = 0xFFFFFFFF. >> >> How is that going to work, or is it just imposible. >> >> >> >> Thanks >> >> > Thanks for following up. Another option might be to use a MemoryStream with
a binary writer to simulate this behavior (with liekly a small performance penalty). -- Patrice "Michael Schöller" <swt***@yahoo.com> a écrit dans le message de news: OyOo5UOTGHA.1***@TK2MSFTNGP10.phx.gbl...Show quote > Just for information. > I found out the my example works only the debugger is showing wrong > values. > If you make output via writeLine the expected values are shown up > > !!BUT!!!!!!!! > > The CLR thinks that the integer has a length of 20 an you can write data > to that indexes. So with this code it is possible to write data on memory > that is not owned by your application just like in good old pointer > time.... > Only that it is managed code (sounds realy bad) > > "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> schrieb im > Newsbeitrag news:%23r6VzaNTGHA.4900@TK2MSFTNGP09.phx.gbl... >> Hello Michael, >> >> I'm afraid all these attributes apply only when you marshal the structure >> to the unmanaged realm. They have no effect when you work with the >> structure exclusively in the managed code. >> >> "Michael Sch?ller" <swt***@yahoo.com> wrote in message >> news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... >>> Hello, >>> >>> I have an little problem and need an helping hand. >>> I want like in C++ create an Array and cast it to a struct, for easy >>> access. >>> Well that didn't work, so I take a look on the Attributes that can be >>> used with struct. >>> Some Attributes sounds good but didn't work like expected. >>> >>> I tried.... >>> -------------------------- >>> using System; >>> using System.Collections.Generic; >>> using System.Text; >>> using System.Runtime.InteropServices; >>> namespace TestStruct >>> { >>> class Program >>> { >>> [StructLayout(LayoutKind.Explicit, Size = 20)] >>> struct test >>> { >>> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >>> ArraySubType = UnmanagedType.U1, SizeConst = 20)] >>> public byte[] test1; >>> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >>> ArraySubType = UnmanagedType.I4, SizeConst = 5)] >>> public int[] test2; >>> } >>> >>> static void Main(string[] args) >>> { >>> test x; >>> x.test1 = new byte[20]; >>> x.test1[1] = 0xFF; >>> x.test1[0] = 0xFF; >>> x.test1[2] = 0xFF; >>> x.test1[3] = 0xFF; >>> } >>> } >>> } >>> ---------------------------------- >>> >>> Well the int-Array should share the memory with the byte-Array, but it >>> looks like the int-Array also gehts 20 elements of bytes that will cast >>> to integer on acces the goal ist that after the last step the x.test2[0] >>> = 0xFFFFFFFF. >>> >>> How is that going to work, or is it just imposible. >>> >>> >>> >>> Thanks >>> >>> >> > > I found an much better solution...after hours of thinking and searching here is the solution that solves my problem perfektly.
It was mixed up of 3 to 4 comments of peoples who says thinks like "There is this <link> but I don't think it could help you"...*g* It does so a great thank you to all..and sory for the date of teh first post I didn't realize that my clock was wrong. [StructLayout(LayoutKind.Explicit, Size = 20)] unsafe struct test { //public fixed char pathName[128]; [FieldOffset(0)] public fixed byte test1[20]; [FieldOffset(0)] public fixed sbyte test2[20]; [FieldOffset(0)] public fixed int test3[5]; [FieldOffset(4)] public fixed int test4[4]; [FieldOffset(1)] public uint test5; } static void Main(string[] args) { test x; unsafe { x.test1[0] = 0xFF; x.test1[1] = 0xFF; x.test1[2] = 0xFF; x.test1[3] = 0xFF; x.test1[4] = 0xFF; x.test3[0] = 0x00; x.test5 = 0xFFFFFFFF; x.test4[1] = -1; } Show quote "Patrice" <a@bc.c> schrieb im Newsbeitrag news:uyQIFMRTGHA.1688@TK2MSFTNGP11.phx.gbl... > Thanks for following up. Another option might be to use a MemoryStream with > a binary writer to simulate this behavior (with liekly a small performance > penalty). > > -- > Patrice > > "Michael Schöller" <swt***@yahoo.com> a écrit dans le message de news: > OyOo5UOTGHA.1***@TK2MSFTNGP10.phx.gbl... >> Just for information. >> I found out the my example works only the debugger is showing wrong >> values. >> If you make output via writeLine the expected values are shown up >> >> !!BUT!!!!!!!! >> >> The CLR thinks that the integer has a length of 20 an you can write data >> to that indexes. So with this code it is possible to write data on memory >> that is not owned by your application just like in good old pointer >> time.... >> Only that it is managed code (sounds realy bad) >> >> "Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> schrieb im >> Newsbeitrag news:%23r6VzaNTGHA.4900@TK2MSFTNGP09.phx.gbl... >>> Hello Michael, >>> >>> I'm afraid all these attributes apply only when you marshal the structure >>> to the unmanaged realm. They have no effect when you work with the >>> structure exclusively in the managed code. >>> >>> "Michael Sch?ller" <swt***@yahoo.com> wrote in message >>> news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... >>>> Hello, >>>> >>>> I have an little problem and need an helping hand. >>>> I want like in C++ create an Array and cast it to a struct, for easy >>>> access. >>>> Well that didn't work, so I take a look on the Attributes that can be >>>> used with struct. >>>> Some Attributes sounds good but didn't work like expected. >>>> >>>> I tried.... >>>> -------------------------- >>>> using System; >>>> using System.Collections.Generic; >>>> using System.Text; >>>> using System.Runtime.InteropServices; >>>> namespace TestStruct >>>> { >>>> class Program >>>> { >>>> [StructLayout(LayoutKind.Explicit, Size = 20)] >>>> struct test >>>> { >>>> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >>>> ArraySubType = UnmanagedType.U1, SizeConst = 20)] >>>> public byte[] test1; >>>> [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, >>>> ArraySubType = UnmanagedType.I4, SizeConst = 5)] >>>> public int[] test2; >>>> } >>>> >>>> static void Main(string[] args) >>>> { >>>> test x; >>>> x.test1 = new byte[20]; >>>> x.test1[1] = 0xFF; >>>> x.test1[0] = 0xFF; >>>> x.test1[2] = 0xFF; >>>> x.test1[3] = 0xFF; >>>> } >>>> } >>>> } >>>> ---------------------------------- >>>> >>>> Well the int-Array should share the memory with the byte-Array, but it >>>> looks like the int-Array also gehts 20 elements of bytes that will cast >>>> to integer on acces the goal ist that after the last step the x.test2[0] >>>> = 0xFFFFFFFF. >>>> >>>> How is that going to work, or is it just imposible. >>>> >>>> >>>> >>>> Thanks >>>> >>>> >>> >> >> > > It seems that your post has "overlaped" several weeks into the future.
Bob Lehmann Show quote "Michael Schöller" <swt***@yahoo.com> wrote in message news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... > Hello, > > I have an little problem and need an helping hand. > I want like in C++ create an Array and cast it to a struct, for easy access. > Well that didn't work, so I take a look on the Attributes that can be used > with struct. > Some Attributes sounds good but didn't work like expected. > > I tried.... > -------------------------- > using System; > using System.Collections.Generic; > using System.Text; > using System.Runtime.InteropServices; > namespace TestStruct > { > class Program > { > [StructLayout(LayoutKind.Explicit, Size = 20)] > struct test > { > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.U1, SizeConst = 20)] > public byte[] test1; > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.I4, SizeConst = 5)] > public int[] test2; > } > > static void Main(string[] args) > { > test x; > x.test1 = new byte[20]; > x.test1[1] = 0xFF; > x.test1[0] = 0xFF; > x.test1[2] = 0xFF; > x.test1[3] = 0xFF; > } > } > } > ---------------------------------- > > Well the int-Array should share the memory with the byte-Array, but it looks > like the int-Array also gehts 20 elements of bytes that will cast to integer > on acces the goal ist that after the last step the x.test2[0] = 0xFFFFFFFF. > > How is that going to work, or is it just imposible. > > > > Thanks > > It seems you clock is overlapping the future by about 3 weeks.
Show quote "Michael Schöller" <swt***@yahoo.com> wrote in message news:OWa0erLTGHA.4608@tk2msftngp13.phx.gbl... > Hello, > > I have an little problem and need an helping hand. > I want like in C++ create an Array and cast it to a struct, for easy > access. > Well that didn't work, so I take a look on the Attributes that can be used > with struct. > Some Attributes sounds good but didn't work like expected. > > I tried.... > -------------------------- > using System; > using System.Collections.Generic; > using System.Text; > using System.Runtime.InteropServices; > namespace TestStruct > { > class Program > { > [StructLayout(LayoutKind.Explicit, Size = 20)] > struct test > { > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.U1, SizeConst = 20)] > public byte[] test1; > [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, > ArraySubType = UnmanagedType.I4, SizeConst = 5)] > public int[] test2; > } > > static void Main(string[] args) > { > test x; > x.test1 = new byte[20]; > x.test1[1] = 0xFF; > x.test1[0] = 0xFF; > x.test1[2] = 0xFF; > x.test1[3] = 0xFF; > } > } > } > ---------------------------------- > > Well the int-Array should share the memory with the byte-Array, but it > looks like the int-Array also gehts 20 elements of bytes that will cast to > integer on acces the goal ist that after the last step the x.test2[0] = > 0xFFFFFFFF. > > How is that going to work, or is it just imposible. > > > > Thanks > > |
|||||||||||||||||||||||