| 1 | ; =====================================================================================================================
|
|---|
| 2 | ; Test script to prove the bug in _WinAPI_RegEnumKey and verify the fix in _WinAPI_RegEnumKey_Fixed
|
|---|
| 3 | ; =====================================================================================================================
|
|---|
| 4 |
|
|---|
| 5 | #include <WinAPIReg.au3>
|
|---|
| 6 |
|
|---|
| 7 | ; =====================================================================================================================
|
|---|
| 8 | ; CORRECTED VERSION
|
|---|
| 9 | ; =====================================================================================================================
|
|---|
| 10 | Func _WinAPI_RegEnumKey_Fixed($hKey, $iIndex)
|
|---|
| 11 | Local $tLastWriteTime = DllStructCreate($tagFILETIME)
|
|---|
| 12 | Local $aCall = DllCall('advapi32.dll', 'long', 'RegEnumKeyExW', 'ulong_ptr', $hKey, 'dword', $iIndex, 'wstr', '', _
|
|---|
| 13 | 'dword*', 256, 'dword', 0, 'ptr', 0, 'ptr', 0, 'ptr', DllStructGetPtr($tLastWriteTime))
|
|---|
| 14 | If @error Then Return SetError(@error, @extended, '')
|
|---|
| 15 | If $aCall[0] Then Return SetError(10, $aCall[0], '')
|
|---|
| 16 |
|
|---|
| 17 | ; FIXED: $aCall[4] is the actual character count (lpcchName output parameter)
|
|---|
| 18 | Return SetExtended($aCall[4], $aCall[3])
|
|---|
| 19 | EndFunc
|
|---|
| 20 |
|
|---|
| 21 | ; =====================================================================================================================
|
|---|
| 22 | ; TEST
|
|---|
| 23 | ; =====================================================================================================================
|
|---|
| 24 | ConsoleWrite("=============================================================================" & @CRLF)
|
|---|
| 25 | ConsoleWrite("Testing _WinAPI_RegEnumKey bug and fix" & @CRLF)
|
|---|
| 26 | ConsoleWrite("=============================================================================" & @CRLF & @CRLF)
|
|---|
| 27 |
|
|---|
| 28 | ; Open HKCU\Software which should have subkeys
|
|---|
| 29 | Local $hKey = _WinAPI_RegOpenKey($HKEY_CURRENT_USER, "Software", BitOR($KEY_ENUMERATE_SUB_KEYS, $KEY_QUERY_VALUE))
|
|---|
| 30 | If @error Then
|
|---|
| 31 | ConsoleWrite("ERROR: Failed to open registry key. Error code: " & @error & @CRLF)
|
|---|
| 32 | Exit 1
|
|---|
| 33 | EndIf
|
|---|
| 34 |
|
|---|
| 35 | ConsoleWrite("Successfully opened HKCU\Software" & @CRLF & @CRLF)
|
|---|
| 36 |
|
|---|
| 37 | ; Test the BUGGY version
|
|---|
| 38 | ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
|
|---|
| 39 | ConsoleWrite("TEST 1: Original _WinAPI_RegEnumKey (BUGGY)" & @CRLF)
|
|---|
| 40 | ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
|
|---|
| 41 |
|
|---|
| 42 | Local $sName1 = _WinAPI_RegEnumKey($hKey, 0)
|
|---|
| 43 | Local $iExt1 = @extended
|
|---|
| 44 | Local $iErr1 = @error
|
|---|
| 45 |
|
|---|
| 46 | ConsoleWrite("Subkey name returned: '" & $sName1 & "'" & @CRLF)
|
|---|
| 47 | ConsoleWrite("@error : " & $iErr1 & @CRLF)
|
|---|
| 48 | ConsoleWrite("@extended : " & $iExt1 & @CRLF)
|
|---|
| 49 | ConsoleWrite("StringLen(name) : " & StringLen($sName1) & @CRLF)
|
|---|
| 50 | ConsoleWrite(@CRLF)
|
|---|
| 51 |
|
|---|
| 52 | If $iExt1 > 100000 Then
|
|---|
| 53 | ConsoleWrite("BUG CONFIRMED: @extended = " & $iExt1 & " is a MEMORY POINTER, not string length!" & @CRLF)
|
|---|
| 54 | ConsoleWrite(" Expected a small number like " & StringLen($sName1) & ", but got a pointer address." & @CRLF)
|
|---|
| 55 | ElseIf $iExt1 = StringLen($sName1) Then
|
|---|
| 56 | ConsoleWrite("No bug detected (unexpected)" & @CRLF)
|
|---|
| 57 | Else
|
|---|
| 58 | ConsoleWrite("@extended = " & $iExt1 & " doesn't match StringLen = " & StringLen($sName1) & @CRLF)
|
|---|
| 59 | EndIf
|
|---|
| 60 |
|
|---|
| 61 | ConsoleWrite(@CRLF)
|
|---|
| 62 |
|
|---|
| 63 | ; Test the FIXED version
|
|---|
| 64 | ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
|
|---|
| 65 | ConsoleWrite("TEST 2: Corrected _WinAPI_RegEnumKey_Fixed (FIXED)" & @CRLF)
|
|---|
| 66 | ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
|
|---|
| 67 |
|
|---|
| 68 | Local $sName2 = _WinAPI_RegEnumKey_Fixed($hKey, 0)
|
|---|
| 69 | Local $iExt2 = @extended
|
|---|
| 70 | Local $iErr2 = @error
|
|---|
| 71 |
|
|---|
| 72 | ConsoleWrite("Subkey name returned: '" & $sName2 & "'" & @CRLF)
|
|---|
| 73 | ConsoleWrite("@error : " & $iErr2 & @CRLF)
|
|---|
| 74 | ConsoleWrite("@extended : " & $iExt2 & @CRLF)
|
|---|
| 75 | ConsoleWrite("StringLen(name) : " & StringLen($sName2) & @CRLF)
|
|---|
| 76 | ConsoleWrite(@CRLF)
|
|---|
| 77 |
|
|---|
| 78 | If $iExt2 = StringLen($sName2) Then
|
|---|
| 79 | ConsoleWrite("✓ FIX CONFIRMED: @extended = " & $iExt2 & " correctly equals StringLen!" & @CRLF)
|
|---|
| 80 | Else
|
|---|
| 81 | ConsoleWrite("❌ Fix failed: @extended = " & $iExt2 & " doesn't match StringLen = " & StringLen($sName2) & @CRLF)
|
|---|
| 82 | EndIf
|
|---|
| 83 |
|
|---|
| 84 | ConsoleWrite(@CRLF)
|
|---|
| 85 |
|
|---|
| 86 | ; Cleanup
|
|---|
| 87 | _WinAPI_RegCloseKey($hKey)
|
|---|