473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

API - CopyMemory

I am trying to use CopyMemory to copy arrays as follows:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal
pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)

dim kin(11) as Integer
dim kout(11) as Integer

kin(1)= 1 'set values for two elements to see if Kout is the same
kin(5) = 5

CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.

--
Dennis in Houston
Nov 21 '05 #1
10 13015
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I am trying to use CopyMemory to copy arrays as follows:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal
pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)

dim kin(11) as Integer
dim kout(11) as Integer

kin(1)= 1 'set values for two elements to see if Kout is the same
kin(5) = 5

CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.

--
Dennis in Houston


Try changing Long to Integer in the API declare. Looks like a VB6
declaration. VB6 Long = VB.Net Integer

--
Al Reid
Nov 21 '05 #2
Tried that, no change. I found an example on the web that timed the various
methods of copying arrays, i.e., for loop, assignment, and CopyMemory written
for vb.net. It doesn't work either as it only copies zeros. The author
apparently didn't test it by actually putting values in the source array.
--
Dennis in Houston
"Al Reid" wrote:
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
I am trying to use CopyMemory to copy arrays as follows:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal
pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)

dim kin(11) as Integer
dim kout(11) as Integer

kin(1)= 1 'set values for two elements to see if Kout is the same
kin(5) = 5

CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.

--
Dennis in Houston


Try changing Long to Integer in the API declare. Looks like a VB6
declaration. VB6 Long = VB.Net Integer

--
Al Reid

Nov 21 '05 #3
Dennis wrote:
Tried that, no change. I found an example on the web that timed the various
methods of copying arrays, i.e., for loop, assignment, and CopyMemory written
for vb.net. It doesn't work either as it only copies zeros. The author
apparently didn't test it by actually putting values in the source array.

I would have the that you should be passing by ref not by val but ....
Nov 21 '05 #4
"Dennis" <De****@discussions.microsoft.com> schrieb:
CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.


I am curious why you would want to use 'CopyMemory'. You may want to use
'Buffer.BlockCopy' instead, which is pretty fast.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
Dennis,

First, pDst and pSrc should be declared as ByRef. They are pointers, and
passing pointers ByVal doesn't make much sense.
Second, their type must match the type of an array.

So, your declatation should look like:

~
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Integer, _
ByRef Source As Integer, _
ByVal Length As Integer _
)
~

Third, you can declare them as arrays, so it will be:

~
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
<MarshalAs(UnmanagedType.LPArray)> ByVal Destination() As
Integer, _
<MarshalAs(UnmanagedType.LPArray)> ByVal Source() As Integer, _
ByVal Length As Integer _
)
~

Fourth, why don't you use the Array.Copy method?

Roman

"Dennis" <De****@discussions.microsoft.com> /
: news:56**********************************@microsof t.com...
I am trying to use CopyMemory to copy arrays as follows:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)

dim kin(11) as Integer
dim kout(11) as Integer

kin(1)= 1 'set values for two elements to see if Kout is the same
kin(5) = 5

CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.

--
Dennis in Houston

Nov 21 '05 #6
Thanks for the corrections to my code. You are absolutely correct in that I
need byRef. However, on the type in the declaration of CopyMemory, when
passed by reference, isn't an integer or long passed to the Com routine as
the address? For example, if my array is of type byte and I pass the array
by reference, what is actually passed...isn't it a 4 byte address?

As for using Array.CopyTo, in the end, I actually will be using CopyMemory
to copy a structure into an array for use with another com object.
--
Dennis in Houston
"Dragon" wrote:
Dennis,

First, pDst and pSrc should be declared as ByRef. They are pointers, and
passing pointers ByVal doesn't make much sense.
Second, their type must match the type of an array.

So, your declatation should look like:

~
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByRef Destination As Integer, _
ByRef Source As Integer, _
ByVal Length As Integer _
)
~

Third, you can declare them as arrays, so it will be:

~
Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
<MarshalAs(UnmanagedType.LPArray)> ByVal Destination() As
Integer, _
<MarshalAs(UnmanagedType.LPArray)> ByVal Source() As Integer, _
ByVal Length As Integer _
)
~

Fourth, why don't you use the Array.Copy method?

Roman

"Dennis" <De****@discussions.microsoft.com> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ
ÓÌÅÄÕÀÝÅÅ: news:56**********************************@microsof t.com...
I am trying to use CopyMemory to copy arrays as follows:

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"

(ByVal
pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)

dim kin(11) as Integer
dim kout(11) as Integer

kin(1)= 1 'set values for two elements to see if Kout is the same
kin(5) = 5

CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.

--
Dennis in Houston


Nov 21 '05 #7
Thanks, you are correct in that I should be using byref. I was trying code
from an example I got from the net (it didn't work either) and that's why.
--
Dennis in Houston
"Jack Russell" wrote:
Dennis wrote:
Tried that, no change. I found an example on the web that timed the various
methods of copying arrays, i.e., for loop, assignment, and CopyMemory written
for vb.net. It doesn't work either as it only copies zeros. The author
apparently didn't test it by actually putting values in the source array.

I would have the that you should be passing by ref not by val but ....

Nov 21 '05 #8
Thanks Herfried but in the end, I want to copy a structure into an array
contained in another array for passing to another com object. That's why I
didn't use Buffer class.
--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
"Dennis" <De****@discussions.microsoft.com> schrieb:
CopyMemory(kout(0), kin(0), 48)

It compiles and runs but all I get in Kout is "0" for all elements.


I am curious why you would want to use 'CopyMemory'. You may want to use
'Buffer.BlockCopy' instead, which is pretty fast.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
"Dennis" <De****@discussions.microsoft.com> /
: news:32**********************************@microsof t.com...
Thanks for the corrections to my code. You are absolutely correct in that I need byRef. However, on the type in the declaration of CopyMemory, when passed by reference, isn't an integer or long passed to the Com routine as the address? For example, if my array is of type byte and I pass the array by reference, what is actually passed...isn't it a 4 byte address?
First, CopyMemory isn't a COM routine. 8=]

Yes, passing ByRef means passing IntPtr (platform-dependent integer)
value which represents address of a variable, so if you wanted to
specify an address (ByVal), you should declare it as IntPtr. But when
you declare parameter ByRef you specify a type of variable itself, not
type of pointer to it, don't you?

If you pass an array by reference you get garbage 8=]. You can either
pass its first element by reference, or pass the whole array by value.
You will get the same result though.

As for using Array.CopyTo, in the end, I actually will be using CopyMemory to copy a structure into an array for use with another com object.
You can use Marshal.StructureToPtr() method for this, for example:

~
Dim a(Marshal.SizeOf(GetType(Decimal)) - 1) As Byte
Marshal.StructureToPtr(23568.23573D,
Marshal.UnsafeAddrOfPinnedArrayElement(a, 0), False)
Stop
~
--
Dennis in Houston


Roman
Nov 21 '05 #10
Dennis,
Rather then risk memory, or at least to minimize, memory corruption.

I would recommend using Buffer.BlockCopy to copy arrays (as you initially
asked).

Alternatively I would use Marshal.Copy, Marshal.PtrToStructure,
Marshal.StructureToPtr and possible other methods on the
System.Runtime.InteropServices.Marshal class to marshal from the Managed to
Unmanaged world.

I would not attempt to implement my own marshaller with CopyMemory as the
..NET Framework team has already done the work for me...

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:56**********************************@microsof t.com...
|I am trying to use CopyMemory to copy arrays as follows:
|
| Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal
| pDst As Long, ByVal pSrc As Long, ByVal ByteLen As Integer)
|
| dim kin(11) as Integer
| dim kout(11) as Integer
|
| kin(1)= 1 'set values for two elements to see if Kout is the same
| kin(5) = 5
|
| CopyMemory(kout(0), kin(0), 48)
|
| It compiles and runs but all I get in Kout is "0" for all elements.
|
| --
| Dennis in Houston
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mikael Lundberg | last post by:
Hi! Just one question Is it legal to copy the array descriptor in this way : Dim lngArr1() as Long Dim lngArr2() as Long Redim lngArr1(2) as Long
5
by: RaviAmbani | last post by:
I am facing a problem in copymemory api in vb.net . the problem is that i have used copymemory api extensively in my project originally written in vb6.0 , now when i'm migrating my project to...
5
by: active | last post by:
Has anyone used CopyMemory with DotNet? In this instance I define it as: Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByRef hpvDest As DEVMODE, ByRef hpvSource As Byte,...
9
by: Paul W | last post by:
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...
4
by: Turtle | last post by:
I've been using SendKeys to automate a small utility which doesn't expose an automation interface, but ran into a problem, for which I found the suggestion that I use the WinAPI SendInput call...
0
by: android | last post by:
Hi all. I should migrate old vb6 code in vb.net but I been clashed with the copymemory api! Here below is a vb6 method that manages the current sample of a waveform file in a buffer for retrieving...
0
by: akumar | last post by:
Hello Please send me the syntax of CopyMemory function in VB.net thnax akumar
3
by: vb newbie | last post by:
I need to copy the contents of one byte array to another one. I was hoping to do it quickly uisng the CopyMemory api call. However, I'm getting: FatalExecutionEngineError was detected...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.