Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy memory

Status
Not open for further replies.

MickTheBelgian

Programmer
Jan 11, 2001
160
0
0
DK
I need to convert a home-made structure to a byte-array, so I can load it into an IBM library which will accept nothing else. In good ol' VB6 days that would have been a MEMCOPY API call, but somehow I don't think that will survive the dot-net engine.
Is there a better way then encoding the lot one by one? I've been looking at streaming but that doesn't seem to cut it either.
 
I found this in an article at This will convert a string to a byte array. I'm not sure what your structure is, but maybe this will help get you on your way.
' Declare a UTF8Encoding object so we may use the GetByte
' method to transform the plainText into a Byte array.
Dim utf8encoder As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
Dim inputInBytes() As Byte = utf8encoder.GetBytes(strInput)
 
This is for changing 2-byte charactersets (unicode) to oldfashioned ASCII. My structure doesn't even contain strings (thanks god, it is already complicated enough).
After a night in the helpfiles I did come up with the system.io.interop.marshall class which seems to be the place to be. Turns out .NET will insert empty bytes in any "managed" structure at its own leisure (read: at random)and if you want it out in a fixed position, you need to go through these interop thingies.
I'll post the code if it works.
 
Turns out .NET will insert empty bytes in any "managed" structure at its own leisure (read: at random)and if you want it out in a fixed position, you need to go through these interop thingies.

It's actually not at random - it's to get the individual entries in the structure to line up on a particular Word or Byte boundary. This happened a lot in the old Win16 days, and the way to get around it is to define your structure in decreasing byte size.. QuadWord (64bit) values first, then DoubleWord (32bit) values next (includes pointers), then Word (16bit) values, and lastly, fixed-length strings and variable-length strings.

Things have changed a bit with .NET (of course!), especially the strings - they would be lumped in with the DoubleWord values, as they are now objects in their own right. But the same technique has value in (a) reducing the memory impact of structures, and (b) preventing wasted bytes from being inserted to get the values lined up on the correct Word/Byte boundaries.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for all the input. I did do some of those aligning things in the VB6 days but on my current project, the structure was laid out by IBM (MQseries, publish/subscribe) and I am afraid they did not take my wishes into account.
dotNet does allow you to fix the offset of each variable in a structure with these <offset> parameters, but if you use an array in a structure this invariably gets replaced with a pointer to an array outside the actual memory of the structure. I did not manage to get around that and finally loaded all the elements of the structure into the IBM driver one by one. Luckily my structure didn't have too many elements.
Did learn a lot though. As long as you don't use arrays, you can even declare two variables on the same memory location. Easy if you need to get the lower byte of an INT32 for example.
 
Hmm Interesting.
Could you give some details? How do you declare 2 variables in the same memory area?
And closely related: the VarPtr, VarPtrArray, and StrPtr does not exist in dotNet. Can you get the memory address in another way or do you have to use Marshal.AllocHGlobal to allocate a new variable in the unmanaged heap?

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top