Results 1 to 8 of 8

Thread: [RESOLVED] Help with CopyMemory API function.

  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.

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Help with CopyMemory API function.

    One line change
    Code:
     CopyMemory Destination(lonDestLen), Source(0), lonSrcLen

  3. #3

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

    Re: Help with CopyMemory API function.

    Quote 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:
    1. Dim bytTest() As Byte, s As String
    2.     Dim bytSrc() As Byte
    3.    
    4.     bytSrc = StrConv("1234", vbFromUnicode)
    5.     AppendByteToByte bytTest, bytSrc
    6.     AppendByteToByte bytTest, bytSrc
    7.     AppendByteToByte bytTest, bytSrc
    8.     AppendByteToByte bytTest, bytSrc
    9.     AppendByteToByte bytTest, bytSrc
    10.     AppendByteToByte bytTest, bytSrc
    11.     AppendByteToByte bytTest, bytSrc
    12.    
    13.     s = StrConv(bytTest, vbUnicode)
    14.     Debug.Print s & " (" & Len(s) & ")

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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

  5. #5

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

    Re: Help with CopyMemory API function.

    I get this:
    121212121212123 (15)



    Wonder why this is happening?

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  7. #7

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

    Re: Help with CopyMemory API function.

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

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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
  •  



Click Here to Expand Forum to Full Width