![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | C++/CLI question Oh, and I forgot to mention this is all managed code - so my example would work if A was a managed class from your example. )"Jeff Winn" <jwinn@xxxxxx> wrote in message news:35C7C838-CD6E-455D-983B-FC2B36C286A4@xxxxxx Quote: > I'm not really all that well versed on unions, they more less annoy me > when i'm trying to do managed pinvoke. Whether anyone should actually do > this I'm not sure - however here's the C# code to accomplish what you were > wanting to do. The classes would remain the same in C++/CLI, you'll just > need to handle the conversion yourself. As long as the object definitions > match completely I don't see this being a problem. > > This was more to satisfy my own curiosity whether it was possible or not. > Basically, I initialized a new instance of the struct and set the members > with the values I wanted in it, allocated the memory on the heap that > matched the struct size and copied the existing struct into that pointer > ensuring to destroy the old location to prevent any memory leaks. > > Afterward I just marshalled the struct back to an instance of the MyObject > class. And lastly releasing the pointer created earlier for the struct. > Since Marshal.PtrToStructure creates a new copy of the object at the > pointer specified you can safely release the old pointer without having to > worry about the garbage collector releasing the new object instance. > > It's important to note the StructLayout attribute decorating the MyObject > class below. Without that, the marshaller wouldn't know what to do with > the class. > > using System; > using System.Runtime.InteropServices; > > namespace ConsoleApplication1 { > class Program { > > static void Main(string[] args) { > MyStruct struc = new MyStruct(); > struc.A = 123; > struc.B = 456; > > IntPtr pStruct = IntPtr.Zero; > > try { > int size = Marshal.SizeOf(typeof(MyStruct)); > pStruct = Marshal.AllocHGlobal(size); > > Marshal.StructureToPtr(struc, pStruct, true); > > MyObject obj = (MyObject)Marshal.PtrToStructure(pStruct, > typeof(MyObject)); > } > finally { > if (pStruct != IntPtr.Zero) Marshal.FreeHGlobal(pStruct); > } > } > } > > struct MyStruct { > public int A; > public int B; > } > > [StructLayout(LayoutKind.Sequential)] > class MyObject { > int _a; > int _b; > > public MyObject() { > } > > public int A { > get { return this._a; } > set { this._a = value; } > } > > public int B { > get { return this._b; } > set { this._b = value; } > } > } > } > > "ajk" <anders43@xxxxxx> wrote in message > news:2fff6280-af52-43b9-adda-7c7760226cf3@xxxxxx Quote: >> Hi >> >> Given an array of bytes i.e. cli::array<Byte>^ar I would like convert >> from and to a structure/class >> >> What is the best way to do this in C++/CLI ? >> >> I have looked at using a union >> >> class A >> { shorrt a; short b; }; >> >> union cv >> { >> A s; >> unsigned char b[4] >> } >> >> but that isn't good way >> >> then there is using pin_pointer >> >> pin_pointer<Bytes> pb = &bytearray[0]; >> mystruct s = *reinterpret_cast<mystruct*>(pb); >> >> but I don't want to use pin_pointers is there any better "C++/CLI" >> way? >> >> tia >> Ajk >> |
My System Specs![]() |
| | #2 (permalink) |
| | Re: C++/CLI question On Jun 23, 1:50 pm, ajk <ander...@xxxxxx> wrote: Quote: > > class A > { shorrt a; short b; }; > > union cv > { > A s; > unsigned char b[4] > > } then I need to have a structure inside the union that is the same as the contents of the A's members which is crappy design. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: C++/CLI question On Jun 23, 1:53 pm, ajk <ander...@xxxxxx> wrote: Quote: > On Jun 23, 1:50 pm, ajk <ander...@xxxxxx> wrote: > > > Quote: > > class A > > { shorrt a; short b; }; Quote: > > union cv > > { > > A s; > > unsigned char b[4] Quote: > > } > oh yeah i forgot, the above is not possible if A is a managed class > > then I need to have a structure inside the union that is the same as > the contents of the A's members which is crappy design. memorystream and binaryread can be used to readbytes and convert. e.g. msclr::auto_gcroot<System::IO::MemoryStream^> ms ( gcnew System::IO::MemoryStream(bytes) ); System::IO::BinaryReader br( ms.get() ); System: ecimal dec = br.ReadDecimal();the eq for BinaryWrite can be use to convert to a byte array msclr::auto_gcroot<System::IO::MemoryStream^> ms( gcnew System::IO::MemoryStream() ); System::IO::BinaryWriter bw( ms.get() ); bw.Write(dec); hth/ajk if somebody wonders. :-) |
My System Specs![]() |
| | #4 (permalink) |
| | Re: C++/CLI question I'm not really all that well versed on unions, they more less annoy me when i'm trying to do managed pinvoke. Whether anyone should actually do this I'm not sure - however here's the C# code to accomplish what you were wanting to do. The classes would remain the same in C++/CLI, you'll just need to handle the conversion yourself. As long as the object definitions match completely I don't see this being a problem. This was more to satisfy my own curiosity whether it was possible or not. Basically, I initialized a new instance of the struct and set the members with the values I wanted in it, allocated the memory on the heap that matched the struct size and copied the existing struct into that pointer ensuring to destroy the old location to prevent any memory leaks. Afterward I just marshalled the struct back to an instance of the MyObject class. And lastly releasing the pointer created earlier for the struct. Since Marshal.PtrToStructure creates a new copy of the object at the pointer specified you can safely release the old pointer without having to worry about the garbage collector releasing the new object instance. It's important to note the StructLayout attribute decorating the MyObject class below. Without that, the marshaller wouldn't know what to do with the class. using System; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyStruct struc = new MyStruct(); struc.A = 123; struc.B = 456; IntPtr pStruct = IntPtr.Zero; try { int size = Marshal.SizeOf(typeof(MyStruct)); pStruct = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(struc, pStruct, true); MyObject obj = (MyObject)Marshal.PtrToStructure(pStruct, typeof(MyObject)); } finally { if (pStruct != IntPtr.Zero) Marshal.FreeHGlobal(pStruct); } } } struct MyStruct { public int A; public int B; } [StructLayout(LayoutKind.Sequential)] class MyObject { int _a; int _b; public MyObject() { } public int A { get { return this._a; } set { this._a = value; } } public int B { get { return this._b; } set { this._b = value; } } } } "ajk" <anders43@xxxxxx> wrote in message news:2fff6280-af52-43b9-adda-7c7760226cf3@xxxxxx Quote: > Hi > > Given an array of bytes i.e. cli::array<Byte>^ar I would like convert > from and to a structure/class > > What is the best way to do this in C++/CLI ? > > I have looked at using a union > > class A > { shorrt a; short b; }; > > union cv > { > A s; > unsigned char b[4] > } > > but that isn't good way > > then there is using pin_pointer > > pin_pointer<Bytes> pb = &bytearray[0]; > mystruct s = *reinterpret_cast<mystruct*>(pb); > > but I don't want to use pin_pointers is there any better "C++/CLI" > way? > > tia > Ajk > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: C++/CLI question Oh, and I forgot to mention this is all managed code - so my example would work if A was a managed class from your example. )"Jeff Winn" <jwinn@xxxxxx> wrote in message news:35C7C838-CD6E-455D-983B-FC2B36C286A4@xxxxxx Quote: > I'm not really all that well versed on unions, they more less annoy me > when i'm trying to do managed pinvoke. Whether anyone should actually do > this I'm not sure - however here's the C# code to accomplish what you were > wanting to do. The classes would remain the same in C++/CLI, you'll just > need to handle the conversion yourself. As long as the object definitions > match completely I don't see this being a problem. > > This was more to satisfy my own curiosity whether it was possible or not. > Basically, I initialized a new instance of the struct and set the members > with the values I wanted in it, allocated the memory on the heap that > matched the struct size and copied the existing struct into that pointer > ensuring to destroy the old location to prevent any memory leaks. > > Afterward I just marshalled the struct back to an instance of the MyObject > class. And lastly releasing the pointer created earlier for the struct. > Since Marshal.PtrToStructure creates a new copy of the object at the > pointer specified you can safely release the old pointer without having to > worry about the garbage collector releasing the new object instance. > > It's important to note the StructLayout attribute decorating the MyObject > class below. Without that, the marshaller wouldn't know what to do with > the class. > > using System; > using System.Runtime.InteropServices; > > namespace ConsoleApplication1 { > class Program { > > static void Main(string[] args) { > MyStruct struc = new MyStruct(); > struc.A = 123; > struc.B = 456; > > IntPtr pStruct = IntPtr.Zero; > > try { > int size = Marshal.SizeOf(typeof(MyStruct)); > pStruct = Marshal.AllocHGlobal(size); > > Marshal.StructureToPtr(struc, pStruct, true); > > MyObject obj = (MyObject)Marshal.PtrToStructure(pStruct, > typeof(MyObject)); > } > finally { > if (pStruct != IntPtr.Zero) Marshal.FreeHGlobal(pStruct); > } > } > } > > struct MyStruct { > public int A; > public int B; > } > > [StructLayout(LayoutKind.Sequential)] > class MyObject { > int _a; > int _b; > > public MyObject() { > } > > public int A { > get { return this._a; } > set { this._a = value; } > } > > public int B { > get { return this._b; } > set { this._b = value; } > } > } > } > > "ajk" <anders43@xxxxxx> wrote in message > news:2fff6280-af52-43b9-adda-7c7760226cf3@xxxxxx Quote: >> Hi >> >> Given an array of bytes i.e. cli::array<Byte>^ar I would like convert >> from and to a structure/class >> >> What is the best way to do this in C++/CLI ? >> >> I have looked at using a union >> >> class A >> { shorrt a; short b; }; >> >> union cv >> { >> A s; >> unsigned char b[4] >> } >> >> but that isn't good way >> >> then there is using pin_pointer >> >> pin_pointer<Bytes> pb = &bytearray[0]; >> mystruct s = *reinterpret_cast<mystruct*>(pb); >> >> but I don't want to use pin_pointers is there any better "C++/CLI" >> way? >> >> tia >> Ajk >> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| IE8 Question | Vista General | |||
| escape character question - hopefully an easy question | PowerShell | |||
| vista genral question and ultimate question | Vista General | |||
| Dual boot system question and family deal discount question | Vista General | |||
| RC1 Cd Key Question | Vista installation & setup | |||