From cfcc8d85b2b3065d895925eb32837a83019eed98 Mon Sep 17 00:00:00 2001 From: Ratin Gao Date: Sun, 23 Mar 2025 09:25:12 +0800 Subject: [PATCH] [NEWDEV] Fix `HardwareId` match logic in `UpdateDriverForPlugAndPlayDevicesW` (#7808) Match Hardware ID correctly. This fix is required to install the latest (7.1.6) Virtual Box Guest Additions drivers. - Add Compatible IDs match, call SetupDiGetDeviceRegistryPropertyW twice, once for SPDRP_HARDWAREID and once for SPDRP_COMPATIBLEIDS. - Use case-insensitive comparison (wcscmp -> _wcsicmp) - Convert code file to UTF-8 encoding --- dll/win32/newdev/newdev.c | 90 +++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/dll/win32/newdev/newdev.c b/dll/win32/newdev/newdev.c index 8eade90eb32..bd62f858de5 100644 --- a/dll/win32/newdev/newdev.c +++ b/dll/win32/newdev/newdev.c @@ -1,7 +1,7 @@ /* * New device installer (newdev.dll) * - * Copyright 2005-2006 Hervé Poussineau (hpoussin@reactos.org) + * Copyright 2005-2006 HervĂ© Poussineau (hpoussin@reactos.org) * 2005 Christoph von Wittich (Christoph@ActiveVB.de) * 2009 Colin Finck (colin@reactos.org) * @@ -50,6 +50,7 @@ UpdateDriverForPlugAndPlayDevicesW( LPWSTR Buffer = NULL; DWORD BufferSize; LPCWSTR CurrentHardwareId; /* Pointer into Buffer */ + DWORD Property; BOOL FoundHardwareId, FoundAtLeastOneDevice = FALSE; BOOL ret = FALSE; @@ -86,51 +87,58 @@ UpdateDriverForPlugAndPlayDevicesW( break; } - /* Get Hardware ID */ - HeapFree(GetProcessHeap(), 0, Buffer); - Buffer = NULL; - BufferSize = 0; - while (!SetupDiGetDeviceRegistryPropertyW( - DevInstData.hDevInfo, - &DevInstData.devInfoData, - SPDRP_HARDWAREID, - NULL, - (PBYTE)Buffer, - BufferSize, - &BufferSize)) - { - if (GetLastError() == ERROR_FILE_NOT_FOUND) - { - Buffer = NULL; - break; - } - else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - { - TRACE("SetupDiGetDeviceRegistryPropertyW() failed with error 0x%x\n", GetLastError()); - goto cleanup; - } - /* This error was expected */ - HeapFree(GetProcessHeap(), 0, Buffer); - Buffer = HeapAlloc(GetProcessHeap(), 0, BufferSize); - if (!Buffer) - { - TRACE("HeapAlloc() failed\n", GetLastError()); - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - goto cleanup; - } - } - if (Buffer == NULL) - continue; - - /* Check if we match the given hardware ID */ + /* Match Hardware ID */ FoundHardwareId = FALSE; - for (CurrentHardwareId = Buffer; *CurrentHardwareId != UNICODE_NULL; CurrentHardwareId += wcslen(CurrentHardwareId) + 1) + Property = SPDRP_HARDWAREID; + while (TRUE) { - if (wcscmp(CurrentHardwareId, HardwareId) == 0) + /* Get IDs data */ + Buffer = NULL; + BufferSize = 0; + while (!SetupDiGetDeviceRegistryPropertyW(DevInstData.hDevInfo, + &DevInstData.devInfoData, + Property, + NULL, + (PBYTE)Buffer, + BufferSize, + &BufferSize)) + { + if (GetLastError() == ERROR_FILE_NOT_FOUND) + { + break; + } + else if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + TRACE("SetupDiGetDeviceRegistryPropertyW() failed with error 0x%x\n", GetLastError()); + goto cleanup; + } + /* This error was expected */ + HeapFree(GetProcessHeap(), 0, Buffer); + Buffer = HeapAlloc(GetProcessHeap(), 0, BufferSize); + if (!Buffer) + { + TRACE("HeapAlloc() failed\n", GetLastError()); + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + goto cleanup; + } + } + if (Buffer) + { + /* Check if we match the given hardware ID */ + for (CurrentHardwareId = Buffer; *CurrentHardwareId != UNICODE_NULL; CurrentHardwareId += wcslen(CurrentHardwareId) + 1) + { + if (_wcsicmp(CurrentHardwareId, HardwareId) == 0) + { + FoundHardwareId = TRUE; + break; + } + } + } + if (FoundHardwareId || Property == SPDRP_COMPATIBLEIDS) { - FoundHardwareId = TRUE; break; } + Property = SPDRP_COMPATIBLEIDS; } if (!FoundHardwareId) continue;