By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,534 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,534 IT Pros & Developers. It's quick & easy.

CopyMemory the .net way?

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
* "Paul W" <no****@vbtips.net> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2

P: n/a
Is FileGet function faster than opening a filestream and reading from that stream?
--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
* "Paul W" <no****@vbtips.net> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #3

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
* "Paul W" <no****@vbtips.net> scripsit:
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?


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.
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.


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4

P: n/a
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" <no****@vbtips.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
* "Paul W" <no****@vbtips.net> scripsit:
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?


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.
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.


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 20 '05 #5

P: n/a

"Monty" <mo*****@inbox.lv> wrote in message
news:uV****************@TK2MSFTNGP09.phx.gbl...
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" <no****@vbtips.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OX**************@tk2msftngp13.phx.gbl...
* "Paul W" <no****@vbtips.net> scripsit:
> 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?


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.
> 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.


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 20 '05 #6

P: n/a
* "Paul W" <no****@vbtips.net> scripsit:
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.


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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

P: n/a
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
"Paul W" <no****@vbtips.net> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl:
"Monty" <mo*****@inbox.lv> wrote in message
news:uV****************@TK2MSFTNGP09.phx.gbl...
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" <no****@vbtips.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message
news:OX**************@tk2msftngp13.phx.gbl...
> * "Paul W" <no****@vbtips.net> scripsit:
> > 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?
>

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.

> > 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.

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

>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>
> <URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 20 '05 #8

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2m************@uni-berlin.de...
* "Paul W" <no****@vbtips.net> scripsit:
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.
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.

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. --
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #9

P: n/a

"David Williams , VB.NET MVP" <da********************@earthlink.net> wrote
in message news:Oz**************@TK2MSFTNGP10.phx.gbl...
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.
"Paul W" <no****@vbtips.net> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl:
"Monty" <mo*****@inbox.lv> wrote in message
news:uV****************@TK2MSFTNGP09.phx.gbl...
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" <no****@vbtips.net> wrote in message
news:O6**************@TK2MSFTNGP11.phx.gbl...
>
> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
> message
> news:OX**************@tk2msftngp13.phx.gbl...
> > * "Paul W" <no****@vbtips.net> scripsit:
> > > 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?
> >
>
> 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.
>
> > > 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.
>
> 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
>
> >
> > --
> > Herfried K. Wagner [MVP]
> > <URL:http://dotnet.mvps.org/>
> > <URL:http://dotnet.mvps.org/dotnet/faqs/>
>
>

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.