|
-
Jul 21st, 2007, 11:10 PM
#1
[RESOLVED] Help with CopyMemory API function.
I'm still trying to learn how to use this the right way, I thought I knew how, but apparently not.
The first problem is now I'm getting an "Out of memory error" with this code.
It will probably be easier just to paste this into VB so you can see the code and test it.
Warning: This might crash the VB IDE so be warned. Or look @ the code before running it in case I'm doing something seriously wrong. 
In a module:
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'Append one byte array to another byte array.
Public Sub AppendByteToByte(Destination() As Byte, Source() As Byte)
Dim lonSrcLen As Long
Dim lonDestLen As Long
Dim lonNewLen As Long
lonSrcLen = SafeUBByte(Source())
lonDestLen = SafeUBByte(Destination)
lonNewLen = lonDestLen + lonSrcLen
Debug.Print lonNewLen
ReDim Preserve Destination(0 To lonNewLen) As Byte
CopyMemory Destination(lonDestLen), Source(0), lonNewLen
End Sub
'UBound function with local error handling.
Private Function SafeUBByte(ByteArray() As Byte) As Long
On Error GoTo ErrorHandler
SafeUBByte = UBound(ByteArray())
Exit Function
ErrorHandler:
SafeUBByte = 0
Exit Function
End Function
'LBound function with local error handling.
Private Function SafeLBByte(ByteArray() As Byte) As Long
On Error GoTo ErrorHandler
SafeLBByte = LBound(ByteArray())
Exit Function
ErrorHandler:
SafeLBByte = 0
Exit Function
End Function
In a form:
Code:
Private Sub Form_Load()
Dim bytTest() As Byte, s As String
Dim bytSrc() As Byte
bytSrc = StrConv("1234", vbFromUnicode)
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
s = StrConv(bytTest, vbUnicode)
Debug.Print s & " (" & Len(s) & ")"
End Sub
Last edited by DigiRev; Jul 21st, 2007 at 11:14 PM.
-
Jul 22nd, 2007, 01:30 AM
#2
Re: Help with CopyMemory API function.
One line change
Code:
CopyMemory Destination(lonDestLen), Source(0), lonSrcLen
-
Jul 22nd, 2007, 01:53 AM
#3
Re: Help with CopyMemory API function.
 Originally Posted by randem
One line change
Code:
CopyMemory Destination(lonDestLen), Source(0), lonSrcLen
Hm, that fixed the error, but I still get a weird output.
123123123123123123123 (22)
Should be:
1234123412341234123412341234 (28)
vb Code:
Dim bytTest() As Byte, s As String
Dim bytSrc() As Byte
bytSrc = StrConv("1234", vbFromUnicode)
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
s = StrConv(bytTest, vbUnicode)
Debug.Print s & " (" & Len(s) & ")
-
Jul 22nd, 2007, 02:05 AM
#4
Re: Help with CopyMemory API function.
Try:
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim bytTest() As Byte, s As String
Dim bytSrc() As Byte
bytSrc = StrConv("1234", vbFromUnicode)
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
AppendByteToByte bytTest, bytSrc
s = StrConv(bytTest, vbUnicode)
Debug.Print s & " (" & Len(s) & ")"
End Sub
'Append one byte array to another byte array.
Public Sub AppendByteToByte(Destination() As Byte, Source() As Byte)
Dim lonSrcLen As Long
Dim lonDestLen As Long
Dim lonNewLen As Long
lonSrcLen = SafeUBByte(Source)
lonDestLen = SafeUBByte(Destination)
lonNewLen = lonDestLen + lonSrcLen
Debug.Print lonNewLen
ReDim Preserve Destination(lonNewLen - 1) As Byte
CopyMemory Destination(lonDestLen), Source(0), lonSrcLen
End Sub
'UBound function with local error handling.
Private Function SafeUBByte(ByteArray() As Byte) As Long
On Error GoTo ErrorHandler
SafeUBByte = UBound(ByteArray()) + 1
Exit Function
ErrorHandler:
SafeUBByte = 0
Exit Function
End Function
'LBound function with local error handling.
Private Function SafeLBByte(ByteArray() As Byte) As Long
On Error GoTo ErrorHandler
SafeLBByte = LBound(ByteArray())
Exit Function
ErrorHandler:
SafeLBByte = 0
Exit Function
End Function
-
Jul 22nd, 2007, 02:11 AM
#5
Re: Help with CopyMemory API function.
I get this:
121212121212123 (15)

Wonder why this is happening?
-
Jul 22nd, 2007, 01:40 PM
#6
Re: Help with CopyMemory API function.
Not with my changes I get
1234123412341234123412341234 (28)
Just open a new project create a form with a command button then copy and paste my code into it.
-
Jul 22nd, 2007, 02:37 PM
#7
Re: Help with CopyMemory API function.
 Originally Posted by randem
Not with my changes I get
1234123412341234123412341234 (28)
Just open a new project create a form with a command button then copy and paste my code into it.
Ah, you're right, sorry. Didn't notice you had also fixed some of the other code. 
Works real well, thanks.
-
Jul 22nd, 2007, 08:57 PM
#8
Re: [RESOLVED] Help with CopyMemory API function.
That is why I posted the whole code block... Did not fix it earlier for you only asked about one part...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|