CopyMemory the .net way?

  • Thread starter Thread starter Paul W
  • Start date Start date
P

Paul W

Hello Group,

I'm a .net newbie with a couple of questions. First, Debug.WriteLine()
doesn't send any output to the immediate window, anybody know why that might
be?

Second, what is the .net equivilant to CopyMemory? I need to read the first
128 bytes from a file and move those bytes into a structure. I'm assuming
that a BinaryReader on a file stream is the way to read the data, but what's
the best way to move those bytes into the struct? Thanks in advance.

Paul
 
* "Paul W said:
I'm a .net newbie with a couple of questions. First, Debug.WriteLine()
doesn't send any output to the immediate window, anybody know why that might
be?

It's sent to the "Output" window.
Second, what is the .net equivilant to CopyMemory? I need to read the first
128 bytes from a file and move those bytes into a structure. I'm
assuming

Depending on how data is stored in the file. 'FileGet' is what you are
looking for.
 

Thanks for your reply.
It's sent to the "Output" window.

OK, I've got it now, not only was I looking at the wrong window, I also
expected it to work when running the project without debugging (pressing
Ctrl+F5 instead of just F5), duh.
Depending on how data is stored in the file. 'FileGet' is what you are
looking for.

After a quick look at the documentation, I don't believe FileGet is what I
want. The data in the file isn't stored as a record, it's just a bunch of
bytes. What I'm trying to do is read a PCX file header and put the data into
a structure. The way this was usually done with VB6 was to read the number
of bytes (128 in the case of a PCX file header) into a byte array using any
of a few different methods, and then using CopyMemory to move the data
directly into a structure (UDT).

I've discovered today that not only don't I know how to do this the .net
way, but I can't even get it to work the old way. In VB6, CopyMemory would
be declared using 'As Any' and that's not acceptable in .net so now I'm
really stuck. Any more help would be appreciated.

Paul W
 
CopyMemory can be declared in .net but all the parameters needs to be
declared as valid data type as .net doesnt support ANY.

So convert the all the *any type* parameters to the data type that your
expecting to send .
ex: you can use intptr type to send any pointer type.

Monty

Paul W said:

Thanks for your reply.
It's sent to the "Output" window.

OK, I've got it now, not only was I looking at the wrong window, I also
expected it to work when running the project without debugging (pressing
Ctrl+F5 instead of just F5), duh.
Depending on how data is stored in the file. 'FileGet' is what you are
looking for.

After a quick look at the documentation, I don't believe FileGet is what I
want. The data in the file isn't stored as a record, it's just a bunch of
bytes. What I'm trying to do is read a PCX file header and put the data into
a structure. The way this was usually done with VB6 was to read the number
of bytes (128 in the case of a PCX file header) into a byte array using any
of a few different methods, and then using CopyMemory to move the data
directly into a structure (UDT).

I've discovered today that not only don't I know how to do this the .net
way, but I can't even get it to work the old way. In VB6, CopyMemory would
be declared using 'As Any' and that's not acceptable in .net so now I'm
really stuck. Any more help would be appreciated.

Paul W
 
Monty said:
CopyMemory can be declared in .net but all the parameters needs to be
declared as valid data type as .net doesnt support ANY.

So convert the all the *any type* parameters to the data type that your
expecting to send .
ex: you can use intptr type to send any pointer type.

Monty

Thanks Monty, I've got CopyMemory working now.

I'd still be interested in knowing the .net way of doing this if anyone
knows.

Paul W
 
* "Paul W said:
Thanks Monty, I've got CopyMemory working now.

I'd still be interested in knowing the .net way of doing this if anyone
knows.

Stop! .NET's Garbage Collector can move objects around in memory, so
'CopyMemory' doesn't work in all situations and/or may cause unexpected
behavior. Instead, use 'Marshal.Copy' when possible.
 
For what you are doing, I would suggest that you use a FileStream and a
BinaryReader. Read the number of bytes that you need into a Byte array
and then place them into a Structure that matches the header structure.
Then you can do what ever you want with it. This avoids the use of
unmanaged code (CopyMemory) and prevents a lot of headaches.

HTH

David
 
Herfried K. Wagner said:
Stop! .NET's Garbage Collector can move objects around in memory, so
'CopyMemory' doesn't work in all situations and/or may cause unexpected
behavior. Instead, use 'Marshal.Copy' when possible.
Yeah, that's starting to get through to me, you just don't do things the way
you used to, not with managed memory.

I took a look at Marshal.Copy today, but I was unable to get it to work.
For now I'm using a kludge, I'm taking the bytes from the array and moving
them directly into the structure members, until I can figure out a cleaner
way of doing it. Thanks again for your help.
 
David Williams said:
For what you are doing, I would suggest that you use a FileStream and a
BinaryReader. Read the number of bytes that you need into a Byte array
and then place them into a Structure that matches the header structure.
Then you can do what ever you want with it. This avoids the use of
unmanaged code (CopyMemory) and prevents a lot of headaches.

HTH

David
Hi David,

The question is, what is the cleanest, quickest way to move the bytes from
the array into the structure? The old way worked great, but as you and
Herfried have pointed out, that just isn't going to work anymore. This is
how I'm doing it now:

Structure PCXHEADER
Dim version As Byte
Dim manufacture As Byte
...
Dim palletteType As Short
Dim bytesPerLine As Byte
End Structure

Dim buffer(128) As Byte
Dim header As PCXHEADER

'Code to read the pcx file header omitted

header.manufacture = CType(pbBuffer(0), Byte)
header.version = CType(pbBuffer(1), Byte)
...
header.palletteType = CType(pbBuffer(64), Short)
header.bytesPerLine = CType(pbBuffer(66), Byte)

I've left a lot out but I think you can see how tedious this is, as there
are 18 members in the PCXHEADER. If you have a better way, please let me
know. Thanks.
 
Back
Top