Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - C++/CLI question

Reply
 
Old 06-23-2008   #1 (permalink)
ajk


 
 

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 SpecsSystem Spec
Old 06-23-2008   #2 (permalink)
ajk


 
 

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]
>
> }
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.
My System SpecsSystem Spec
Old 06-23-2008   #3 (permalink)
ajk


 
 

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.
to answer my own question:

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 SpecsSystem Spec
Old 06-23-2008   #4 (permalink)
Jeff Winn


 
 

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 SpecsSystem Spec
Old 06-23-2008   #5 (permalink)
Jeff Winn


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46