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 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
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
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 ....
"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/>
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
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
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 ....
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/>
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: akumar |
last post by:
Hello
Please send me the syntax of CopyMemory function in VB.net
thnax
akumar
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |