Results 1 to 8 of 8

Thread: [RESOLVED] Help with CopyMemory API function.

Threaded View

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width