Ticket #4106: Test_WinAPI_RegEnumKey.au3

File Test_WinAPI_RegEnumKey.au3, 4.2 KB (added by TukangUsil7, on Jul 12, 2026 at 1:11:33 PM)

Test_WinAPI_RegEnumKey

Line 
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; =====================================================================================================================
10Func _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])
19EndFunc
20
21; =====================================================================================================================
22; TEST
23; =====================================================================================================================
24ConsoleWrite("=============================================================================" & @CRLF)
25ConsoleWrite("Testing _WinAPI_RegEnumKey bug and fix" & @CRLF)
26ConsoleWrite("=============================================================================" & @CRLF & @CRLF)
27
28; Open HKCU\Software which should have subkeys
29Local $hKey = _WinAPI_RegOpenKey($HKEY_CURRENT_USER, "Software", BitOR($KEY_ENUMERATE_SUB_KEYS, $KEY_QUERY_VALUE))
30If @error Then
31 ConsoleWrite("ERROR: Failed to open registry key. Error code: " & @error & @CRLF)
32 Exit 1
33EndIf
34
35ConsoleWrite("Successfully opened HKCU\Software" & @CRLF & @CRLF)
36
37; Test the BUGGY version
38ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
39ConsoleWrite("TEST 1: Original _WinAPI_RegEnumKey (BUGGY)" & @CRLF)
40ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
41
42Local $sName1 = _WinAPI_RegEnumKey($hKey, 0)
43Local $iExt1 = @extended
44Local $iErr1 = @error
45
46ConsoleWrite("Subkey name returned: '" & $sName1 & "'" & @CRLF)
47ConsoleWrite("@error : " & $iErr1 & @CRLF)
48ConsoleWrite("@extended : " & $iExt1 & @CRLF)
49ConsoleWrite("StringLen(name) : " & StringLen($sName1) & @CRLF)
50ConsoleWrite(@CRLF)
51
52If $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)
55ElseIf $iExt1 = StringLen($sName1) Then
56 ConsoleWrite("No bug detected (unexpected)" & @CRLF)
57Else
58 ConsoleWrite("@extended = " & $iExt1 & " doesn't match StringLen = " & StringLen($sName1) & @CRLF)
59EndIf
60
61ConsoleWrite(@CRLF)
62
63; Test the FIXED version
64ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
65ConsoleWrite("TEST 2: Corrected _WinAPI_RegEnumKey_Fixed (FIXED)" & @CRLF)
66ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF)
67
68Local $sName2 = _WinAPI_RegEnumKey_Fixed($hKey, 0)
69Local $iExt2 = @extended
70Local $iErr2 = @error
71
72ConsoleWrite("Subkey name returned: '" & $sName2 & "'" & @CRLF)
73ConsoleWrite("@error : " & $iErr2 & @CRLF)
74ConsoleWrite("@extended : " & $iExt2 & @CRLF)
75ConsoleWrite("StringLen(name) : " & StringLen($sName2) & @CRLF)
76ConsoleWrite(@CRLF)
77
78If $iExt2 = StringLen($sName2) Then
79 ConsoleWrite("✓ FIX CONFIRMED: @extended = " & $iExt2 & " correctly equals StringLen!" & @CRLF)
80Else
81 ConsoleWrite("❌ Fix failed: @extended = " & $iExt2 & " doesn't match StringLen = " & StringLen($sName2) & @CRLF)
82EndIf
83
84ConsoleWrite(@CRLF)
85
86; Cleanup
87_WinAPI_RegCloseKey($hKey)