; ===================================================================================================================== ; Test script to prove the bug in _WinAPI_RegEnumKey and verify the fix in _WinAPI_RegEnumKey_Fixed ; ===================================================================================================================== #include ; ===================================================================================================================== ; CORRECTED VERSION ; ===================================================================================================================== Func _WinAPI_RegEnumKey_Fixed($hKey, $iIndex) Local $tLastWriteTime = DllStructCreate($tagFILETIME) Local $aCall = DllCall('advapi32.dll', 'long', 'RegEnumKeyExW', 'ulong_ptr', $hKey, 'dword', $iIndex, 'wstr', '', _ 'dword*', 256, 'dword', 0, 'ptr', 0, 'ptr', 0, 'ptr', DllStructGetPtr($tLastWriteTime)) If @error Then Return SetError(@error, @extended, '') If $aCall[0] Then Return SetError(10, $aCall[0], '') ; FIXED: $aCall[4] is the actual character count (lpcchName output parameter) Return SetExtended($aCall[4], $aCall[3]) EndFunc ; ===================================================================================================================== ; TEST ; ===================================================================================================================== ConsoleWrite("=============================================================================" & @CRLF) ConsoleWrite("Testing _WinAPI_RegEnumKey bug and fix" & @CRLF) ConsoleWrite("=============================================================================" & @CRLF & @CRLF) ; Open HKCU\Software which should have subkeys Local $hKey = _WinAPI_RegOpenKey($HKEY_CURRENT_USER, "Software", BitOR($KEY_ENUMERATE_SUB_KEYS, $KEY_QUERY_VALUE)) If @error Then ConsoleWrite("ERROR: Failed to open registry key. Error code: " & @error & @CRLF) Exit 1 EndIf ConsoleWrite("Successfully opened HKCU\Software" & @CRLF & @CRLF) ; Test the BUGGY version ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF) ConsoleWrite("TEST 1: Original _WinAPI_RegEnumKey (BUGGY)" & @CRLF) ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF) Local $sName1 = _WinAPI_RegEnumKey($hKey, 0) Local $iExt1 = @extended Local $iErr1 = @error ConsoleWrite("Subkey name returned: '" & $sName1 & "'" & @CRLF) ConsoleWrite("@error : " & $iErr1 & @CRLF) ConsoleWrite("@extended : " & $iExt1 & @CRLF) ConsoleWrite("StringLen(name) : " & StringLen($sName1) & @CRLF) ConsoleWrite(@CRLF) If $iExt1 > 100000 Then ConsoleWrite("BUG CONFIRMED: @extended = " & $iExt1 & " is a MEMORY POINTER, not string length!" & @CRLF) ConsoleWrite(" Expected a small number like " & StringLen($sName1) & ", but got a pointer address." & @CRLF) ElseIf $iExt1 = StringLen($sName1) Then ConsoleWrite("No bug detected (unexpected)" & @CRLF) Else ConsoleWrite("@extended = " & $iExt1 & " doesn't match StringLen = " & StringLen($sName1) & @CRLF) EndIf ConsoleWrite(@CRLF) ; Test the FIXED version ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF) ConsoleWrite("TEST 2: Corrected _WinAPI_RegEnumKey_Fixed (FIXED)" & @CRLF) ConsoleWrite("-----------------------------------------------------------------------------" & @CRLF) Local $sName2 = _WinAPI_RegEnumKey_Fixed($hKey, 0) Local $iExt2 = @extended Local $iErr2 = @error ConsoleWrite("Subkey name returned: '" & $sName2 & "'" & @CRLF) ConsoleWrite("@error : " & $iErr2 & @CRLF) ConsoleWrite("@extended : " & $iExt2 & @CRLF) ConsoleWrite("StringLen(name) : " & StringLen($sName2) & @CRLF) ConsoleWrite(@CRLF) If $iExt2 = StringLen($sName2) Then ConsoleWrite("✓ FIX CONFIRMED: @extended = " & $iExt2 & " correctly equals StringLen!" & @CRLF) Else ConsoleWrite("❌ Fix failed: @extended = " & $iExt2 & " doesn't match StringLen = " & StringLen($sName2) & @CRLF) EndIf ConsoleWrite(@CRLF) ; Cleanup _WinAPI_RegCloseKey($hKey)