Results 1 to 10 of 10

Thread: RtlCompareMemory Help please[RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    Resolved RtlCompareMemory Help please[RESOLVED]

    Hi all, Please could someone tell me how i use the RtlCompareMemory api to compare 2 2d arrays.

    Thanks in advance...
    Last edited by Adrian26; Nov 7th, 2005 at 04:11 AM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: RtlCompareMemory Help please

    I'm sure you have a more imaginative example in mind, but here:

    (Note, this is very ugly, and could be optimised by storing LBound/UBound values. Even better, use zero-based dimensions exclusively, that will be much faster overall).

    VB Code:
    1. Private Declare Function RtlCompareMemory Lib "ntdll" ( _
    2.     ByRef Source1 As Any, _
    3.     ByRef Source2 As Any, _
    4.     ByVal Length As Long _
    5. ) As Long
    6. '
    7.  
    8. Private Sub Command1_Click()
    9.     Dim array1(3, 6) As Long
    10.     Dim array2(3, 6) As Long
    11.  
    12.     array1(2, 4) = 50
    13.     array2(2, 4) = 50
    14.  
    15.     ' Usage to compare two 2D arrays:
    16.     Dim size1 As Long, size2 As Long
    17.  
    18.     size1 = (UBound(array1, 1) - LBound(array1, 1)) * _
    19.             (UBound(array1, 2) - LBound(array1, 2)) * _
    20.             LenB(array1(LBound(array1, 1), LBound(array1, 2)))
    21.  
    22.     size2 = (UBound(array2, 1) - LBound(array2, 1)) * _
    23.             (UBound(array2, 2) - LBound(array2, 2)) * _
    24.             LenB(array2(LBound(array2, 1), LBound(array2, 2)))
    25.  
    26.     If (size1 = size2) Then
    27.         If (RtlCompareMemory(ByVal VarPtr(array1(LBound(array1, 1), LBound(array1, 2))), _
    28.                              ByVal VarPtr(array2(LBound(array2, 1), LBound(array2, 2))), _
    29.                              size1) = size1) _
    30.         Then
    31.             MsgBox "Arrays are equal"
    32.         Else
    33.             MsgBox "Arrays are not equal"
    34.         End If
    35.     Else
    36.         MsgBox "Arrays have different dimensions"
    37.     End If
    38. End Sub

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: RtlCompareMemory Help please

    If you do use zero-based dimensions, it is a hell of a lot neater.

    VB Code:
    1. Dim size1 As Long, size2 As Long
    2.  
    3.     size1 = UBound(array1, 1) * UBound(array1, 2) * LenB(array1(0, 0))
    4.     size2 = UBound(array2, 1) * UBound(array2, 2) * LenB(array2(0, 0))
    5.    
    6.     If (size1 = size2) Then
    7.         If (RtlCompareMemory(ByVal VarPtr(array1(0, 0)), _
    8.                              ByVal VarPtr(array2(0, 0)), _
    9.                              size1) = size1) _
    10.         Then
    11.             MsgBox "Arrays are equal"
    12.         Else
    13.             MsgBox "Arrays are not equal"
    14.         End If
    15.     Else
    16.         MsgBox "Arrays have different dimensions"
    17.     End If

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: RtlCompareMemory Help please

    Penagate,
    You and Joacim like varptr don't you?
    VB Code:
    1. If RtlCompareMemory(array1(0, 0), array2(0, 0), size1) = size1 then

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: RtlCompareMemory Help please

    VarPtr() does my dishes.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    Re: RtlCompareMemory Help please

    Excellent! works like a dream many thanks...

    One last question though, the way you work out the array sizes can i use the same method for the copyMemory api, again the arrays are 2d.

    Cheers...

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: RtlCompareMemory Help please

    Yes you can.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    Thumbs up Re: RtlCompareMemory Help please

    Now that was a fast reply, thanks once again.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: RtlCompareMemory Help please[RESOLVED]

    No problem!

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: RtlCompareMemory Help please[RESOLVED]

    If interested, there is a guide in the FAQ section on how to use one dimensional arrays instead of multidimensional

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