Hi all, Please could someone tell me how i use the RtlCompareMemory api to compare 2 2d arrays.
Thanks in advance...
Printable View
Hi all, Please could someone tell me how i use the RtlCompareMemory api to compare 2 2d arrays.
Thanks in advance...
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:
Private Declare Function RtlCompareMemory Lib "ntdll" ( _ ByRef Source1 As Any, _ ByRef Source2 As Any, _ ByVal Length As Long _ ) As Long ' Private Sub Command1_Click() Dim array1(3, 6) As Long Dim array2(3, 6) As Long array1(2, 4) = 50 array2(2, 4) = 50 ' Usage to compare two 2D arrays: Dim size1 As Long, size2 As Long size1 = (UBound(array1, 1) - LBound(array1, 1)) * _ (UBound(array1, 2) - LBound(array1, 2)) * _ LenB(array1(LBound(array1, 1), LBound(array1, 2))) size2 = (UBound(array2, 1) - LBound(array2, 1)) * _ (UBound(array2, 2) - LBound(array2, 2)) * _ LenB(array2(LBound(array2, 1), LBound(array2, 2))) If (size1 = size2) Then If (RtlCompareMemory(ByVal VarPtr(array1(LBound(array1, 1), LBound(array1, 2))), _ ByVal VarPtr(array2(LBound(array2, 1), LBound(array2, 2))), _ size1) = size1) _ Then MsgBox "Arrays are equal" Else MsgBox "Arrays are not equal" End If Else MsgBox "Arrays have different dimensions" End If End Sub
If you do use zero-based dimensions, it is a hell of a lot neater.
VB Code:
Dim size1 As Long, size2 As Long size1 = UBound(array1, 1) * UBound(array1, 2) * LenB(array1(0, 0)) size2 = UBound(array2, 1) * UBound(array2, 2) * LenB(array2(0, 0)) If (size1 = size2) Then If (RtlCompareMemory(ByVal VarPtr(array1(0, 0)), _ ByVal VarPtr(array2(0, 0)), _ size1) = size1) _ Then MsgBox "Arrays are equal" Else MsgBox "Arrays are not equal" End If Else MsgBox "Arrays have different dimensions" End If
Penagate,
You and Joacim like varptr don't you?
VB Code:
If RtlCompareMemory(array1(0, 0), array2(0, 0), size1) = size1 then
VarPtr() does my dishes.
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...
Yes you can.
Now that was a fast reply, thanks once again.
No problem! :)
If interested, there is a guide in the FAQ section on how to use one dimensional arrays instead of multidimensional :)