Converting unmanaged struct -> CLI value struct

D

DaTurk

I'm having an issue converting an unmanaged struct to a CLI managed
struct. It's a value struct and this cannot be changed.

[StructLayout(LayoutKind::Sequential, CharSet=CharSet::Ansi, Pack=1)]
public value struct UpdateItem
{
public:

[MarshalAs(UnmanagedType::ByValTStr, SizeConst=50)]
System::String ^Name;

[MarshalAs(UnmanagedType::ByValTStr, SizeConst=50)]
System::String ^Address;

[MarshalAs(UnmanagedType::ByValTStr, SizeConst=10)]
System::String ^Envelope;

[MarshalAs(UnmanagedType::ByValTStr, SizeConst=30)]
System::String ^Record;

virtual System::String^ ToString() override;
void SetDefaultValues();
};

But I cannot use

Marshal::ptrToStructure((IntPtr)&item, UpdateItem_); Because it has
issues apparently with the fact the managed struct is a value type. I
could copy these items manually, but this conversion is going to
happen, quite possible, in excess of 11 times a second, and gcnew'ing
these four managed strings each time is unacceptable. And I know
they're managed, and they will get Garbage Collected; but anyone when
this runs, I promise you the GC isn't running enough for me. The
memory continues to creep up, and up. Any solutions?

THank you in advance.
 
M

Mattias Sjögren

But I cannot use

Marshal::ptrToStructure((IntPtr)&item, UpdateItem_); Because it has
issues apparently with the fact the managed struct is a value type.

Use the other PtrToStructure overload.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top