Fix some whitespaces
[reactos.git] / reactos / dll / win32 / setupapi / devinst.c
index 4f6cdb9..a0c94b5 100644 (file)
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include "setupapi_private.h"
@@ -86,6 +86,15 @@ struct CoInstallerElement
     PVOID PrivateData;
 };
 
+struct GetSectionCallbackInfo
+{
+    PSP_ALTPLATFORM_INFO PlatformInfo;
+    BYTE ProductType;
+    WORD SuiteMask;
+    WCHAR BestSection[LINE_LEN + 1];
+    DWORD BestScore1, BestScore2, BestScore3, BestScore4, BestScore5;
+};
+
 static BOOL
 PropertyChangeHandler(
     IN HDEVINFO DeviceInfoSet,
@@ -93,7 +102,7 @@ PropertyChangeHandler(
     IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
     IN DWORD ClassInstallParamsSize);
 
-static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
+static const UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
     NULL, /* DIF_SELECTDEVICE */
     NULL, /* DIF_INSTALLDEVICE */
     NULL, /* DIF_ASSIGNRESOURCES */
@@ -141,11 +150,12 @@ static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
 /***********************************************************************
  *              SetupDiBuildClassInfoList  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoList(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiBuildClassInfoList(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize)
 {
     TRACE("\n");
     return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
@@ -156,13 +166,14 @@ BOOL WINAPI SetupDiBuildClassInfoList(
 /***********************************************************************
  *              SetupDiBuildClassInfoListExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoListExA(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiBuildClassInfoListExA(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR MachineNameW = NULL;
     BOOL bResult;
@@ -188,27 +199,29 @@ BOOL WINAPI SetupDiBuildClassInfoListExA(
 /***********************************************************************
  *             SetupDiBuildClassInfoListExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoListExW(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiBuildClassInfoListExW(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
-    HKEY hClassesKey;
-    HKEY hClassKey;
+    HKEY hClassesKey = INVALID_HANDLE_VALUE;
+    HKEY hClassKey = NULL;
     DWORD dwLength;
     DWORD dwIndex;
     LONG lError;
     DWORD dwGuidListIndex = 0;
+    BOOL ret = FALSE;
 
     TRACE("0x%lx %p %lu %p %s %p\n", Flags, ClassGuidList,
         ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
 
     if (RequiredSize != NULL)
-       *RequiredSize = 0;
+        *RequiredSize = 0;
 
     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
                                             KEY_ENUMERATE_SUB_KEYS,
@@ -216,118 +229,113 @@ BOOL WINAPI SetupDiBuildClassInfoListExW(
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     for (dwIndex = 0; ; dwIndex++)
     {
-       dwLength = MAX_GUID_STRING_LEN + 1;
-       lError = RegEnumKeyExW(hClassesKey,
-                              dwIndex,
-                              szKeyName,
-                              &dwLength,
-                              NULL,
-                              NULL,
-                              NULL,
-                              NULL);
-       TRACE("RegEnumKeyExW() returns %ld\n", lError);
-       if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
-       {
-           TRACE("Key name: %s\n", debugstr_w(szKeyName));
-
-           if (RegOpenKeyExW(hClassesKey,
-                             szKeyName,
-                             0,
-                             KEY_QUERY_VALUE,
-                             &hClassKey))
-           {
-               RegCloseKey(hClassesKey);
-               return FALSE;
-           }
-
-           if (!RegQueryValueExW(hClassKey,
-                                 REGSTR_VAL_NOUSECLASS,
-                                 NULL,
-                                 NULL,
-                                 NULL,
-                                 NULL))
-           {
-               TRACE("'NoUseClass' value found!\n");
-               RegCloseKey(hClassKey);
-               continue;
-           }
-
-           if ((Flags & DIBCI_NOINSTALLCLASS) &&
-               (!RegQueryValueExW(hClassKey,
-                                  REGSTR_VAL_NOINSTALLCLASS,
-                                  NULL,
-                                  NULL,
-                                  NULL,
-                                  NULL)))
-           {
-               TRACE("'NoInstallClass' value found!\n");
-               RegCloseKey(hClassKey);
-               continue;
-           }
-
-           if ((Flags & DIBCI_NODISPLAYCLASS) &&
-               (!RegQueryValueExW(hClassKey,
-                                  REGSTR_VAL_NODISPLAYCLASS,
-                                  NULL,
-                                  NULL,
-                                  NULL,
-                                  NULL)))
-           {
-               TRACE("'NoDisplayClass' value found!\n");
-               RegCloseKey(hClassKey);
-               continue;
-           }
-
-           RegCloseKey(hClassKey);
-
-           TRACE("Guid: %s\n", debugstr_w(szKeyName));
-           if (dwGuidListIndex < ClassGuidListSize)
-           {
-               if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
-               {
-                   szKeyName[37] = 0;
-               }
-               TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
-
-               UuidFromStringW(&szKeyName[1],
-                               &ClassGuidList[dwGuidListIndex]);
-           }
-
-           dwGuidListIndex++;
-       }
-
-       if (lError != ERROR_SUCCESS)
-           break;
-    }
-
-    RegCloseKey(hClassesKey);
+        dwLength = MAX_GUID_STRING_LEN + 1;
+        lError = RegEnumKeyExW(hClassesKey,
+                               dwIndex,
+                               szKeyName,
+                               &dwLength,
+                               NULL,
+                               NULL,
+                               NULL,
+                               NULL);
+        TRACE("RegEnumKeyExW() returns %ld\n", lError);
+        if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
+        {
+            TRACE("Key name: %s\n", debugstr_w(szKeyName));
+
+            if (hClassKey != NULL)
+                RegCloseKey(hClassKey);
+            if (RegOpenKeyExW(hClassesKey,
+                              szKeyName,
+                              0,
+                              KEY_QUERY_VALUE,
+                              &hClassKey) != ERROR_SUCCESS)
+            {
+                goto cleanup;
+            }
+
+            if (RegQueryValueExW(hClassKey,
+                                 REGSTR_VAL_NOUSECLASS,
+                                 NULL,
+                                 NULL,
+                                 NULL,
+                                 NULL) == ERROR_SUCCESS)
+            {
+                TRACE("'NoUseClass' value found!\n");
+                continue;
+            }
+
+            if ((Flags & DIBCI_NOINSTALLCLASS) &&
+                (!RegQueryValueExW(hClassKey,
+                                   REGSTR_VAL_NOINSTALLCLASS,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   NULL)))
+            {
+                TRACE("'NoInstallClass' value found!\n");
+                continue;
+            }
+
+            if ((Flags & DIBCI_NODISPLAYCLASS) &&
+                (!RegQueryValueExW(hClassKey,
+                                   REGSTR_VAL_NODISPLAYCLASS,
+                                   NULL,
+                                   NULL,
+                                   NULL,
+                                   NULL)))
+            {
+                TRACE("'NoDisplayClass' value found!\n");
+                continue;
+            }
+
+            TRACE("Guid: %s\n", debugstr_w(szKeyName));
+            if (dwGuidListIndex < ClassGuidListSize)
+            {
+                if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
+                    szKeyName[37] = 0;
+                TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
+
+                UuidFromStringW(&szKeyName[1],
+                    &ClassGuidList[dwGuidListIndex]);
+            }
+
+            dwGuidListIndex++;
+        }
+
+        if (lError != ERROR_SUCCESS)
+        break;
+    }
 
     if (RequiredSize != NULL)
-       *RequiredSize = dwGuidListIndex;
+        *RequiredSize = dwGuidListIndex;
 
     if (ClassGuidListSize < dwGuidListIndex)
-    {
-       SetLastError(ERROR_INSUFFICIENT_BUFFER);
-       return FALSE;
-    }
+        SetLastError(ERROR_INSUFFICIENT_BUFFER);
+    else
+        ret = TRUE;
 
-    return TRUE;
+cleanup:
+    if (hClassesKey != INVALID_HANDLE_VALUE)
+        RegCloseKey(hClassesKey);
+    if (hClassKey != NULL)
+        RegCloseKey(hClassKey);
+    return ret;
 }
 
 /***********************************************************************
  *             SetupDiClassGuidsFromNameA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassGuidsFromNameA(
-        LPCSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiClassGuidsFromNameA(
+    IN PCSTR ClassName,
+    OUT LPGUID ClassGuidList,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize)
 {
   return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
                                       ClassGuidListSize, RequiredSize,
@@ -338,10 +346,10 @@ BOOL WINAPI SetupDiClassGuidsFromNameA(
  *             SetupDiClassGuidsFromNameW  (SETUPAPI.@)
  */
 BOOL WINAPI SetupDiClassGuidsFromNameW(
-        LPCWSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize)
+        IN PCWSTR ClassName,
+        OUT LPGUID ClassGuidList,
+        IN DWORD ClassGuidListSize,
+        OUT PDWORD RequiredSize)
 {
   return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
                                       ClassGuidListSize, RequiredSize,
@@ -351,13 +359,14 @@ BOOL WINAPI SetupDiClassGuidsFromNameW(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassGuidsFromNameExA(
-        LPCSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassGuidsFromNameExA(
+    IN PCSTR ClassName,
+    OUT LPGUID ClassGuidList,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR ClassNameW = NULL;
     LPWSTR MachineNameW = NULL;
@@ -394,28 +403,30 @@ BOOL WINAPI SetupDiClassGuidsFromNameExA(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassGuidsFromNameExW(
-        LPCWSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassGuidsFromNameExW(
+    IN PCWSTR ClassName,
+    OUT LPGUID ClassGuidList,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
     WCHAR szClassName[256];
-    HKEY hClassesKey;
-    HKEY hClassKey;
+    HKEY hClassesKey = INVALID_HANDLE_VALUE;
+    HKEY hClassKey = NULL;
     DWORD dwLength;
     DWORD dwIndex;
     LONG lError;
     DWORD dwGuidListIndex = 0;
+    BOOL ret = FALSE;
 
     TRACE("%s %p %lu %p %s %p\n", debugstr_w(ClassName), ClassGuidList,
         ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
 
     if (RequiredSize != NULL)
-       *RequiredSize = 0;
+        *RequiredSize = 0;
 
     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
                                             KEY_ENUMERATE_SUB_KEYS,
@@ -423,96 +434,94 @@ BOOL WINAPI SetupDiClassGuidsFromNameExW(
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     for (dwIndex = 0; ; dwIndex++)
     {
-       dwLength = MAX_GUID_STRING_LEN + 1;
-       lError = RegEnumKeyExW(hClassesKey,
-                              dwIndex,
-                              szKeyName,
-                              &dwLength,
-                              NULL,
-                              NULL,
-                              NULL,
-                              NULL);
-       TRACE("RegEnumKeyExW() returns %ld\n", lError);
-       if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
-       {
-           TRACE("Key name: %s\n", debugstr_w(szKeyName));
-
-           if (RegOpenKeyExW(hClassesKey,
-                             szKeyName,
-                             0,
-                             KEY_QUERY_VALUE,
-                             &hClassKey))
-           {
-               RegCloseKey(hClassesKey);
-               return FALSE;
-           }
-
-           dwLength = 256 * sizeof(WCHAR);
-           if (!RegQueryValueExW(hClassKey,
-                                 Class,
-                                 NULL,
-                                 NULL,
-                                 (LPBYTE)szClassName,
-                                 &dwLength))
-           {
-               TRACE("Class name: %s\n", debugstr_w(szClassName));
-
-               if (strcmpiW(szClassName, ClassName) == 0)
-               {
-                   TRACE("Found matching class name\n");
-
-                   TRACE("Guid: %s\n", debugstr_w(szKeyName));
-                   if (dwGuidListIndex < ClassGuidListSize)
-                   {
-                       if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
-                       {
-                           szKeyName[37] = 0;
-                       }
-                       TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
-
-                       UuidFromStringW(&szKeyName[1],
-                                       &ClassGuidList[dwGuidListIndex]);
-                   }
-
-                   dwGuidListIndex++;
-               }
-           }
-
-           RegCloseKey(hClassKey);
-       }
-
-       if (lError != ERROR_SUCCESS)
-           break;
-    }
-
-    RegCloseKey(hClassesKey);
+        dwLength = MAX_GUID_STRING_LEN + 1;
+        lError = RegEnumKeyExW(hClassesKey,
+                               dwIndex,
+                               szKeyName,
+                               &dwLength,
+                               NULL,
+                               NULL,
+                               NULL,
+                               NULL);
+        TRACE("RegEnumKeyExW() returns %ld\n", lError);
+        if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
+        {
+            TRACE("Key name: %s\n", debugstr_w(szKeyName));
+
+            if (hClassKey != NULL)
+                RegCloseKey(hClassKey);
+            if (RegOpenKeyExW(hClassesKey,
+                              szKeyName,
+                              0,
+                              KEY_QUERY_VALUE,
+                              &hClassKey) != ERROR_SUCCESS)
+            {
+                goto cleanup;
+            }
+
+            dwLength = 256 * sizeof(WCHAR);
+            if (RegQueryValueExW(hClassKey,
+                                 Class,
+                                 NULL,
+                                 NULL,
+                                 (LPBYTE)szClassName,
+                                 &dwLength) == ERROR_SUCCESS)
+            {
+                TRACE("Class name: %s\n", debugstr_w(szClassName));
+
+                if (strcmpiW(szClassName, ClassName) == 0)
+                {
+                    TRACE("Found matching class name\n");
+
+                    TRACE("Guid: %s\n", debugstr_w(szKeyName));
+                    if (dwGuidListIndex < ClassGuidListSize)
+                    {
+                        if (szKeyName[0] == L'{' && szKeyName[37] == L'}')
+                            szKeyName[37] = 0;
+                        TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
+
+                        UuidFromStringW(&szKeyName[1],
+                            &ClassGuidList[dwGuidListIndex]);
+                    }
+
+                    dwGuidListIndex++;
+                }
+            }
+        }
+
+        if (lError != ERROR_SUCCESS)
+            break;
+    }
 
     if (RequiredSize != NULL)
-       *RequiredSize = dwGuidListIndex;
+        *RequiredSize = dwGuidListIndex;
 
     if (ClassGuidListSize < dwGuidListIndex)
-    {
-       SetLastError(ERROR_INSUFFICIENT_BUFFER);
-       return FALSE;
-    }
+        SetLastError(ERROR_INSUFFICIENT_BUFFER);
+    else
+        ret = TRUE;
 
-    return TRUE;
+cleanup:
+    if (hClassesKey != INVALID_HANDLE_VALUE)
+        RegCloseKey(hClassesKey);
+    if (hClassKey != NULL)
+        RegCloseKey(hClassKey);
+    return ret;
 }
 
 /***********************************************************************
  *              SetupDiClassNameFromGuidA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidA(
-        const GUID* ClassGuid,
-        PSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiClassNameFromGuidA(
+    IN CONST GUID* ClassGuid,
+    OUT PSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
                                      ClassNameSize, RequiredSize,
@@ -522,11 +531,12 @@ BOOL WINAPI SetupDiClassNameFromGuidA(
 /***********************************************************************
  *              SetupDiClassNameFromGuidW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidW(
-        const GUID* ClassGuid,
-        PWSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiClassNameFromGuidW(
+    IN CONST GUID* ClassGuid,
+    OUT PWSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
                                      ClassNameSize, RequiredSize,
@@ -536,13 +546,14 @@ BOOL WINAPI SetupDiClassNameFromGuidW(
 /***********************************************************************
  *              SetupDiClassNameFromGuidExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidExA(
-        const GUID* ClassGuid,
-        PSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize,
-        PCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassNameFromGuidExA(
+    IN CONST GUID* ClassGuid,
+    OUT PSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
     LPWSTR MachineNameW = NULL;
@@ -551,7 +562,7 @@ BOOL WINAPI SetupDiClassNameFromGuidExA(
     if (MachineName)
         MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
     ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
-     NULL, MachineNameW, Reserved);
+        NULL, MachineNameW, Reserved);
     if (ret)
     {
         int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
@@ -567,17 +578,19 @@ BOOL WINAPI SetupDiClassNameFromGuidExA(
 /***********************************************************************
  *             SetupDiClassNameFromGuidExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidExW(
-        const GUID* ClassGuid,
-        PWSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize,
-        PCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassNameFromGuidExW(
+    IN CONST GUID* ClassGuid,
+    OUT PWSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HKEY hKey;
     DWORD dwLength;
     LONG rc;
+    BOOL ret = FALSE;
 
     TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassName,
         ClassNameSize, RequiredSize, debugstr_w(MachineName), Reserved);
@@ -588,54 +601,54 @@ BOOL WINAPI SetupDiClassNameFromGuidExW(
                                      MachineName,
                                      Reserved);
     if (hKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     if (RequiredSize != NULL)
     {
-       dwLength = 0;
-       rc = RegQueryValueExW(hKey,
-                            Class,
-                            NULL,
-                            NULL,
-                            NULL,
-                            &dwLength);
-       if (rc != ERROR_SUCCESS)
-       {
-           SetLastError(rc);
-           RegCloseKey(hKey);
-           return FALSE;
-       }
+        dwLength = 0;
+        rc = RegQueryValueExW(hKey,
+                              Class,
+                              NULL,
+                              NULL,
+                              NULL,
+                              &dwLength);
+        if (rc != ERROR_SUCCESS)
+        {
+            SetLastError(rc);
+            goto cleanup;
+        }
 
-       *RequiredSize = dwLength / sizeof(WCHAR);
+        *RequiredSize = dwLength / sizeof(WCHAR);
     }
 
     dwLength = ClassNameSize * sizeof(WCHAR);
     rc = RegQueryValueExW(hKey,
-                        Class,
-                        NULL,
-                        NULL,
-                        (LPBYTE)ClassName,
-                        &dwLength);
+                          Class,
+                          NULL,
+                          NULL,
+                          (LPBYTE)ClassName,
+                          &dwLength);
     if (rc != ERROR_SUCCESS)
     {
-       SetLastError(rc);
-       RegCloseKey(hKey);
-       return FALSE;
+        SetLastError(rc);
+        goto cleanup;
     }
 
-    RegCloseKey(hKey);
+    ret = TRUE;
 
-    return TRUE;
+cleanup:
+    if (hKey != INVALID_HANDLE_VALUE)
+        RegCloseKey(hKey);
+    return ret;
 }
 
 /***********************************************************************
  *             SetupDiCreateDeviceInfoList (SETUPAPI.@)
  */
 HDEVINFO WINAPI
-SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
-                           HWND hwndParent)
+SetupDiCreateDeviceInfoList(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN HWND hwndParent OPTIONAL)
 {
   return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
 }
@@ -644,10 +657,11 @@ SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
  *             SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
  */
 HDEVINFO WINAPI
-SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
-                              HWND hwndParent,
-                              PCSTR MachineName,
-                              PVOID Reserved)
+SetupDiCreateDeviceInfoListExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR MachineNameW = NULL;
     HDEVINFO hDevInfo;
@@ -703,10 +717,11 @@ GetErrorCodeFromCrCode(const IN CONFIGRET cr)
  *             SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
  */
 HDEVINFO WINAPI
-SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
-                              HWND hwndParent,
-                              PCWSTR MachineName,
-                              PVOID Reserved)
+SetupDiCreateDeviceInfoListExW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
   struct DeviceInfoSet *list;
   LPWSTR UNCServerName = NULL;
@@ -788,21 +803,26 @@ SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
 cleanup:
   if (ret == INVALID_HANDLE_VALUE)
   {
-    if (list && list->HKLM != 0 && list->HKLM != HKEY_LOCAL_MACHINE)
-      RegCloseKey(list->HKLM);
+    if (list)
+    {
+      if (list->HKLM != NULL && list->HKLM != HKEY_LOCAL_MACHINE)
+        RegCloseKey(list->HKLM);
+    }
     HeapFree(GetProcessHeap(), 0, list);
   }
-  HeapFree(GetProcessHeap(), 0, UNCServerName);
+  if (UNCServerName)
+    HeapFree(GetProcessHeap(), 0, UNCServerName);
   return ret;
 }
 
 /***********************************************************************
  *             SetupDiEnumDeviceInfo (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiEnumDeviceInfo(
-        HDEVINFO DeviceInfoSet,
-        DWORD MemberIndex,
-        PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiEnumDeviceInfo(
+    IN HDEVINFO DeviceInfoSet,
+    IN DWORD MemberIndex,
+    OUT PSP_DEVINFO_DATA DeviceInfoData)
 {
     BOOL ret = FALSE;
 
@@ -826,7 +846,7 @@ BOOL WINAPI SetupDiEnumDeviceInfo(
                 SetLastError(ERROR_NO_MORE_ITEMS);
             else
             {
-                struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
+                struct DeviceInfoElement *DevInfo = CONTAINING_RECORD(ItemList, struct DeviceInfoElement, ListEntry);
                 memcpy(&DeviceInfoData->ClassGuid,
                     &DevInfo->ClassGuid,
                     sizeof(GUID));
@@ -846,12 +866,12 @@ BOOL WINAPI SetupDiEnumDeviceInfo(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallA(
-        IN HINF InfHandle,
-        IN PCSTR InfSectionName,
-        OUT PSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PSTR *Extension OPTIONAL)
+    IN HINF InfHandle,
+    IN PCSTR InfSectionName,
+    OUT PSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSTR *Extension OPTIONAL)
 {
     return SetupDiGetActualSectionToInstallExA(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -863,12 +883,12 @@ SetupDiGetActualSectionToInstallA(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallW(
-        IN HINF InfHandle,
-        IN PCWSTR InfSectionName,
-        OUT PWSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PWSTR *Extension OPTIONAL)
+    IN HINF InfHandle,
+    IN PCWSTR InfSectionName,
+    OUT PWSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PWSTR *Extension OPTIONAL)
 {
     return SetupDiGetActualSectionToInstallExW(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -880,14 +900,14 @@ SetupDiGetActualSectionToInstallW(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallExA(
-        IN HINF InfHandle,
-        IN PCSTR InfSectionName,
-        IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
-        OUT PSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PSTR* Extension OPTIONAL,
-        IN PVOID Reserved)
+    IN HINF InfHandle,
+    IN PCSTR InfSectionName,
+    IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
+    OUT PSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSTR* Extension OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR InfSectionNameW = NULL;
     LPWSTR InfSectionWithExtW = NULL;
@@ -937,19 +957,304 @@ cleanup:
     return bResult;
 }
 
+/* Lower scores are best ones */
+static BOOL
+CheckSectionValid(
+    IN LPCWSTR SectionName,
+    IN PSP_ALTPLATFORM_INFO PlatformInfo,
+    IN BYTE ProductType,
+    IN WORD SuiteMask,
+    OUT PDWORD ScorePlatform,
+    OUT PDWORD ScoreMajorVersion,
+    OUT PDWORD ScoreMinorVersion,
+    OUT PDWORD ScoreProductType,
+    OUT PDWORD ScoreSuiteMask)
+{
+    LPWSTR Section = NULL;
+    LPCWSTR pExtensionPlatform, pExtensionArchitecture;
+    LPWSTR Fields[6];
+    DWORD i;
+    BOOL ret = FALSE;
+
+    TRACE("%s %p 0x%x 0x%x\n",
+        debugstr_w(SectionName), PlatformInfo, ProductType, SuiteMask);
+
+    static const WCHAR ExtensionPlatformNone[]  = {'.',0};
+    static const WCHAR ExtensionPlatformNT[]  = {'.','N','T',0};
+    static const WCHAR ExtensionPlatformWindows[]  = {'.','W','i','n',0};
+
+    static const WCHAR ExtensionArchitectureNone[]  = {0};
+    static const WCHAR ExtensionArchitecturealpha[]  = {'a','l','p','h','a',0};
+    static const WCHAR ExtensionArchitectureamd64[]  = {'a','m','d','6','4',0};
+    static const WCHAR ExtensionArchitectureia64[]  = {'i','a','6','4',0};
+    static const WCHAR ExtensionArchitecturemips[]  = {'m','i','p','s',0};
+    static const WCHAR ExtensionArchitectureppc[]  = {'p','p','c',0};
+    static const WCHAR ExtensionArchitecturex86[]  = {'x','8','6',0};
+
+    *ScorePlatform = *ScoreMajorVersion = *ScoreMinorVersion = *ScoreProductType = *ScoreSuiteMask = 0;
+
+    Section = DuplicateString(SectionName);
+    if (!Section)
+    {
+        TRACE("DuplicateString() failed\n");
+        goto cleanup;
+    }
+
+    /* Set various extensions values */
+    switch (PlatformInfo->Platform)
+    {
+        case VER_PLATFORM_WIN32_WINDOWS:
+            pExtensionPlatform = ExtensionPlatformWindows;
+            break;
+        case VER_PLATFORM_WIN32_NT:
+            pExtensionPlatform = ExtensionPlatformNT;
+            break;
+        default:
+            ERR("Unkown platform 0x%lx\n", PlatformInfo->Platform);
+            pExtensionPlatform = ExtensionPlatformNone;
+            break;
+    }
+    switch (PlatformInfo->ProcessorArchitecture)
+    {
+        case PROCESSOR_ARCHITECTURE_ALPHA:
+            pExtensionArchitecture = ExtensionArchitecturealpha;
+            break;
+        case PROCESSOR_ARCHITECTURE_AMD64:
+            pExtensionArchitecture = ExtensionArchitectureamd64;
+            break;
+        case PROCESSOR_ARCHITECTURE_IA64:
+            pExtensionArchitecture = ExtensionArchitectureia64;
+            break;
+        case PROCESSOR_ARCHITECTURE_INTEL:
+            pExtensionArchitecture = ExtensionArchitecturex86;
+            break;
+        case PROCESSOR_ARCHITECTURE_MIPS:
+            pExtensionArchitecture = ExtensionArchitecturemips;
+            break;
+        case PROCESSOR_ARCHITECTURE_PPC:
+            pExtensionArchitecture = ExtensionArchitectureppc;
+            break;
+        default:
+            ERR("Unknown processor architecture 0x%x\n", PlatformInfo->ProcessorArchitecture);
+        case PROCESSOR_ARCHITECTURE_UNKNOWN:
+            pExtensionArchitecture = ExtensionArchitectureNone;
+            break;
+    }
+
+    /*
+     * Field[0] Platform
+     * Field[1] Architecture
+     * Field[2] Major version
+     * Field[3] Minor version
+     * Field[4] Product type
+     * Field[5] Suite mask
+     * Remark: lastests fields may be NULL if the information is not provided
+     */
+    Fields[0] = strchrW(Section, '.');
+    if (Fields[0] == NULL)
+    {
+        TRACE("No extension found\n");
+        *ScorePlatform = *ScoreMajorVersion = *ScoreMinorVersion = *ScoreProductType = *ScoreSuiteMask = ULONG_MAX;
+        ret = TRUE;
+        goto cleanup;
+    }
+    Fields[1] = Fields[0] + 1;
+    Fields[2] = Fields[3] = Fields[4] = Fields[5] = NULL;
+    for (i = 2; Fields[i - 1] != NULL && i < 6; i++)
+    {
+        Fields[i] = wcschr(Fields[i - 1], '.');
+        if (Fields[i])
+        {
+            Fields[i]++;
+            *(Fields[i] - 1) = L'\0';
+        }
+    }
+    /* Take care of first 2 fields */
+    if (strncmpiW(Fields[0], ExtensionPlatformWindows, strlenW(ExtensionPlatformWindows)) == 0)
+    {
+        if (PlatformInfo->Platform != VER_PLATFORM_WIN32_WINDOWS)
+        {
+            TRACE("Mismatch on platform field\n");
+            goto cleanup;
+        }
+        Fields[1] += wcslen(ExtensionPlatformWindows) - 1;
+    }
+    else if (strncmpiW(Fields[0], ExtensionPlatformNT, strlenW(ExtensionPlatformNT)) == 0)
+    {
+        if (PlatformInfo->Platform != VER_PLATFORM_WIN32_NT)
+        {
+            TRACE("Mismatch on platform field\n");
+            goto cleanup;
+        }
+        Fields[1] += wcslen(ExtensionPlatformNT) - 1;
+    }
+    else
+    {
+        /* No platform specified */
+        *ScorePlatform |= 0x02;
+    }
+    if (strcmpiW(Fields[1], ExtensionArchitectureNone) == 0)
+    {
+        /* No architecture specified */
+        *ScorePlatform |= 0x01;
+    }
+    else if (strcmpiW(Fields[1], pExtensionArchitecture) != 0)
+    {
+        TRACE("Mismatch on architecture field ('%s' and '%s')\n",
+            debugstr_w(Fields[1]), debugstr_w(pExtensionArchitecture));
+        goto cleanup;
+    }
+
+    /* Check if informations are matching */
+    if (Fields[2] && *Fields[2])
+    {
+        DWORD MajorVersion, MinorVersion = 0;
+        MajorVersion = strtoulW(Fields[2], NULL, 0);
+        if ((MajorVersion == 0 || MajorVersion == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong MajorVersion ('%s')\n", debugstr_w(Fields[2]));
+            goto cleanup;
+        }
+        if (Fields[3] && *Fields[3])
+        {
+            MinorVersion = strtoulW(Fields[3], NULL, 0);
+            if ((MinorVersion == 0 || MinorVersion == ULONG_MAX) &&
+                (errno == ERANGE || errno == EINVAL))
+            {
+                TRACE("Wrong MinorVersion ('%s')\n", debugstr_w(Fields[3]));
+                goto cleanup;
+            }
+        }
+        if (PlatformInfo->MajorVersion < MajorVersion ||
+            (PlatformInfo->MajorVersion == MajorVersion && PlatformInfo->MinorVersion < MinorVersion))
+        {
+            TRACE("Mismatch on version field (%lu.%lu and %lu.%lu)\n",
+                MajorVersion, MinorVersion, PlatformInfo->MajorVersion, PlatformInfo->MinorVersion);
+            goto cleanup;
+        }
+        *ScoreMajorVersion = MajorVersion - PlatformInfo->MajorVersion;
+        if (MajorVersion == PlatformInfo->MajorVersion)
+            *ScoreMinorVersion = MinorVersion - PlatformInfo->MinorVersion;
+        else
+            *ScoreMinorVersion = MinorVersion;
+    }
+    else if (Fields[3] && *Fields[3])
+    {
+        TRACE("Minor version found without major version\n");
+        goto cleanup;
+    }
+    else
+    {
+        *ScoreMajorVersion = PlatformInfo->MajorVersion;
+        *ScoreMinorVersion = PlatformInfo->MinorVersion;
+    }
+
+    if (Fields[4] && *Fields[4])
+    {
+        DWORD CurrentProductType;
+        CurrentProductType = strtoulW(Fields[4], NULL, 0);
+        if ((CurrentProductType == 0 || CurrentProductType == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong Product type ('%s')\n", debugstr_w(Fields[4]));
+            goto cleanup;
+        }
+        if (CurrentProductType != ProductType)
+        {
+            TRACE("Mismatch on product type (0x%08lx and 0x%08x)\n",
+                CurrentProductType, ProductType);
+            goto cleanup;
+        }
+    }
+    else
+        *ScoreProductType = 1;
+
+    if (Fields[5] && *Fields[5])
+    {
+        DWORD CurrentSuiteMask;
+        CurrentSuiteMask = strtoulW(Fields[5], NULL, 0);
+        if ((CurrentSuiteMask == 0 || CurrentSuiteMask == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong Suite mask ('%s')\n", debugstr_w(Fields[5]));
+            goto cleanup;
+        }
+        if ((CurrentSuiteMask & ~SuiteMask) != 0)
+        {
+            TRACE("Mismatch on suite mask (0x%08lx and 0x%08x)\n",
+                CurrentSuiteMask, SuiteMask);
+            goto cleanup;
+        }
+        *ScoreSuiteMask = SuiteMask & ~CurrentSuiteMask;
+    }
+    else
+        *ScoreSuiteMask = SuiteMask;
+
+    ret = TRUE;
+
+cleanup:
+    MyFree(Section);
+    return ret;
+}
+
+static BOOL
+GetSectionCallback(
+    IN LPCWSTR SectionName,
+    IN PVOID Context)
+{
+    struct GetSectionCallbackInfo *info = Context;
+    DWORD Score1, Score2, Score3, Score4, Score5;
+    BOOL ret;
+
+    ret = CheckSectionValid(
+        SectionName,
+        info->PlatformInfo,
+        info->ProductType,
+        info->SuiteMask,
+        &Score1, &Score2, &Score3, &Score4, &Score5);
+    if (!ret)
+    {
+        TRACE("Section %s not compatible\n", debugstr_w(SectionName));
+        return TRUE;
+    }
+    if (Score1 > info->BestScore1) goto done;
+    if (Score1 < info->BestScore1) goto bettersection;
+    if (Score2 > info->BestScore2) goto done;
+    if (Score2 < info->BestScore2) goto bettersection;
+    if (Score3 > info->BestScore3) goto done;
+    if (Score3 < info->BestScore3) goto bettersection;
+    if (Score4 > info->BestScore4) goto done;
+    if (Score4 < info->BestScore4) goto bettersection;
+    if (Score5 > info->BestScore5) goto done;
+    if (Score5 < info->BestScore5) goto bettersection;
+    goto done;
+
+bettersection:
+    strcpyW(info->BestSection, SectionName);
+    info->BestScore1 = Score1;
+    info->BestScore2 = Score2;
+    info->BestScore3 = Score3;
+    info->BestScore4 = Score4;
+    info->BestScore5 = Score5;
+
+done:
+    return TRUE;
+}
+
 /***********************************************************************
  *             SetupDiGetActualSectionToInstallExW (SETUPAPI.@)
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallExW(
-        IN HINF InfHandle,
-        IN PCWSTR InfSectionName,
-        IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
-        OUT PWSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PWSTR* Extension OPTIONAL,
-        IN PVOID Reserved)
+    IN HINF InfHandle,
+    IN PCWSTR InfSectionName,
+    IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
+    OUT PWSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PWSTR* Extension OPTIONAL,
+    IN PVOID Reserved)
 {
     BOOL ret = FALSE;
 
@@ -968,162 +1273,84 @@ SetupDiGetActualSectionToInstallExW(
     else
     {
         static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
+        static BYTE CurrentProductType = 0;
+        static WORD CurrentSuiteMask = 0;
         PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
-        LPCWSTR pExtensionPlatform, pExtensionArchitecture;
-        WCHAR SectionName[LINE_LEN + 1];
-        LONG lLineCount = -1;
+        struct GetSectionCallbackInfo CallbackInfo;
         DWORD dwFullLength;
+        BYTE ProductType;
+        WORD SuiteMask;
 
         /* Fill platform info if needed */
         if (AlternatePlatformInfo)
-            pPlatformInfo = AlternatePlatformInfo;
-        else if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
         {
-            /* That's the first time we go here. We need to fill in the structure */
-            OSVERSIONINFO VersionInfo;
-            SYSTEM_INFO SystemInfo;
-            VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-            ret = GetVersionEx(&VersionInfo);
-            if (!ret)
-                goto done;
-            GetSystemInfo(&SystemInfo);
-            CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
-            CurrentPlatform.Platform = VersionInfo.dwPlatformId;
-            CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
-            CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
-            CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
-            CurrentPlatform.Reserved = 0;
-        }
-
-static const WCHAR ExtensionPlatformNone[]  = {'.',0};
-static const WCHAR ExtensionPlatformNT[]  = {'.','N','T',0};
-static const WCHAR ExtensionPlatformWindows[]  = {'.','W','i','n',0};
-
-static const WCHAR ExtensionArchitectureNone[]  = {0};
-static const WCHAR ExtensionArchitecturealpha[]  = {'a','l','p','h','a',0};
-static const WCHAR ExtensionArchitectureamd64[]  = {'a','m','d','6','4',0};
-static const WCHAR ExtensionArchitectureia64[]  = {'i','a','6','4',0};
-static const WCHAR ExtensionArchitecturemips[]  = {'m','i','p','s',0};
-static const WCHAR ExtensionArchitectureppc[]  = {'p','p','c',0};
-static const WCHAR ExtensionArchitecturex86[]  = {'x','8','6',0};
-
-        /* Set various extensions values */
-        switch (pPlatformInfo->Platform)
-        {
-            case VER_PLATFORM_WIN32_WINDOWS:
-                pExtensionPlatform = ExtensionPlatformWindows;
-                break;
-            case VER_PLATFORM_WIN32_NT:
-                pExtensionPlatform = ExtensionPlatformNT;
-                break;
-            default:
-                ERR("Unkown platform 0x%lx\n", pPlatformInfo->Platform);
-                pExtensionPlatform = ExtensionPlatformNone;
-                break;
+            pPlatformInfo = AlternatePlatformInfo;
+            ProductType = 0;
+            SuiteMask = 0;
         }
-        switch (pPlatformInfo->ProcessorArchitecture)
+        else
         {
-            case PROCESSOR_ARCHITECTURE_ALPHA:
-                pExtensionArchitecture = ExtensionArchitecturealpha;
-                break;
-            case PROCESSOR_ARCHITECTURE_AMD64:
-                pExtensionArchitecture = ExtensionArchitectureamd64;
-                break;
-            case PROCESSOR_ARCHITECTURE_IA64:
-                pExtensionArchitecture = ExtensionArchitectureia64;
-                break;
-            case PROCESSOR_ARCHITECTURE_INTEL:
-                pExtensionArchitecture = ExtensionArchitecturex86;
-                break;
-            case PROCESSOR_ARCHITECTURE_MIPS:
-                pExtensionArchitecture = ExtensionArchitecturemips;
-                break;
-            case PROCESSOR_ARCHITECTURE_PPC:
-                pExtensionArchitecture = ExtensionArchitectureppc;
-                break;
-            default:
-                ERR("Unknown processor architecture 0x%x\n", pPlatformInfo->ProcessorArchitecture);
-            case PROCESSOR_ARCHITECTURE_UNKNOWN:
-                pExtensionArchitecture = ExtensionArchitectureNone;
-                break;
+            if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
+            {
+                /* That's the first time we go here. We need to fill in the structure */
+                OSVERSIONINFOEX VersionInfo;
+                SYSTEM_INFO SystemInfo;
+                VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+                ret = GetVersionEx((POSVERSIONINFO)&VersionInfo);
+                if (!ret)
+                    goto done;
+                GetSystemInfo(&SystemInfo);
+                CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
+                CurrentPlatform.Platform = VersionInfo.dwPlatformId;
+                CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
+                CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
+                CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
+                CurrentPlatform.Reserved = 0;
+                CurrentProductType = VersionInfo.wProductType;
+                CurrentSuiteMask = VersionInfo.wSuiteMask;
+            }
+            ProductType = CurrentProductType;
+            SuiteMask = CurrentSuiteMask;
+        }
+
+        CallbackInfo.PlatformInfo = pPlatformInfo;
+        CallbackInfo.ProductType = ProductType;
+        CallbackInfo.SuiteMask = SuiteMask;
+        CallbackInfo.BestScore1 = ULONG_MAX;
+        CallbackInfo.BestScore2 = ULONG_MAX;
+        CallbackInfo.BestScore3 = ULONG_MAX;
+        CallbackInfo.BestScore4 = ULONG_MAX;
+        CallbackInfo.BestScore5 = ULONG_MAX;
+        strcpyW(CallbackInfo.BestSection, InfSectionName);
+        if (!EnumerateSectionsStartingWith(
+            InfHandle,
+            InfSectionName,
+            GetSectionCallback,
+            &CallbackInfo))
+        {
+            SetLastError(ERROR_GEN_FAILURE);
+            goto done;
         }
 
-static const WCHAR FormatPlatformArchitectureMajorMinor[]  = {'%','s','%','s','%','s','.','%','l','u','.','%','l','u',0};
-static const WCHAR FormatPlatformMajorMinor[]  = {'%','s','%','s','.','%','l','u','.','%','l','u',0};
-static const WCHAR FormatPlatformArchitectureMajor[]  = {'%','s','%','s','%','s','.','%','l','u',0};
-static const WCHAR FormatPlatformMajor[]  = {'%','s','%','s','.','%','l','u',0};
-static const WCHAR FormatPlatformArchitecture[]  = {'%','s','%','s','%','s',0};
-static const WCHAR FormatPlatform[]  = {'%','s','%','s',0};
-static const WCHAR FormatNone[]  = {'%','s',0};
-
-        SectionName[LINE_LEN] = UNICODE_NULL;
-
-        /* Test with platform.architecture.major.minor extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajorMinor, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.major.minor extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformMajorMinor, InfSectionName,
-            pExtensionPlatform, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.architecture.major extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajor, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.major extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformMajor, InfSectionName,
-            pExtensionPlatform, pPlatformInfo->MajorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.architecture extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitecture, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatform, InfSectionName,
-            pExtensionPlatform);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test without extension */
-        snprintfW(SectionName, LINE_LEN, FormatNone, InfSectionName);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* No appropriate section found */
-        SetLastError(ERROR_INVALID_PARAMETER);
-        goto done;
+        dwFullLength = lstrlenW(CallbackInfo.BestSection);
+        if (RequiredSize != NULL)
+            *RequiredSize = dwFullLength + 1;
 
-sectionfound:
-        dwFullLength = lstrlenW(SectionName);
-        if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
+        if (InfSectionWithExtSize > 0)
         {
-            if (InfSectionWithExtSize < (dwFullLength + 1))
+            if (InfSectionWithExtSize < dwFullLength + 1)
             {
                 SetLastError(ERROR_INSUFFICIENT_BUFFER);
                 goto done;
             }
-
-            lstrcpyW(InfSectionWithExt, SectionName);
-            if (Extension != NULL)
+            strcpyW(InfSectionWithExt, CallbackInfo.BestSection);
+            if (Extension)
             {
-                DWORD dwLength = lstrlenW(SectionName);
+                DWORD dwLength = lstrlenW(InfSectionName);
                 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
             }
         }
 
-        if (RequiredSize != NULL)
-            *RequiredSize = dwFullLength + 1;
-
         ret = TRUE;
     }
 
@@ -1136,11 +1363,12 @@ done:
 /***********************************************************************
  *             SetupDiGetClassDescriptionA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionA(
-        const GUID* ClassGuid,
-        PSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiGetClassDescriptionA(
+    IN CONST GUID *ClassGuid,
+    OUT PSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1150,11 +1378,12 @@ BOOL WINAPI SetupDiGetClassDescriptionA(
 /***********************************************************************
  *             SetupDiGetClassDescriptionW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionW(
-        const GUID* ClassGuid,
-        PWSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiGetClassDescriptionW(
+    IN CONST GUID *ClassGuid,
+    OUT PWSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1164,13 +1393,14 @@ BOOL WINAPI SetupDiGetClassDescriptionW(
 /***********************************************************************
  *             SetupDiGetClassDescriptionExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionExA(
-        const GUID* ClassGuid,
-        PSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize,
-        PCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassDescriptionExA(
+    IN CONST GUID *ClassGuid,
+    OUT PSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWCHAR ClassDescriptionW;
     LPWSTR MachineNameW = NULL;
@@ -1221,16 +1451,18 @@ end:
 /***********************************************************************
  *             SetupDiGetClassDescriptionExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionExW(
-        const GUID* ClassGuid,
-        PWSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize,
-        PCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassDescriptionExW(
+    IN CONST GUID *ClassGuid,
+    OUT PWSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
-    HKEY hKey;
+    HKEY hKey = INVALID_HANDLE_VALUE;
     DWORD dwLength;
+    BOOL ret = FALSE;
 
     TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
         ClassDescriptionSize, RequiredSize, debugstr_w(MachineName), Reserved);
@@ -1242,109 +1474,114 @@ BOOL WINAPI SetupDiGetClassDescriptionExW(
                                      Reserved);
     if (hKey == INVALID_HANDLE_VALUE)
     {
-       WARN("SetupDiOpenClassRegKeyExW() failed (Error %lu)\n", GetLastError());
-       return FALSE;
+        WARN("SetupDiOpenClassRegKeyExW() failed (Error %lu)\n", GetLastError());
+        goto cleanup;
     }
 
     if (RequiredSize != NULL)
     {
-       dwLength = 0;
-       if (RegQueryValueExW(hKey,
-                            NULL,
-                            NULL,
-                            NULL,
-                            NULL,
-                            &dwLength))
-       {
-           RegCloseKey(hKey);
-           return FALSE;
-       }
+        dwLength = 0;
+        if (RegQueryValueExW(hKey,
+                             NULL,
+                             NULL,
+                             NULL,
+                             NULL,
+                             &dwLength) != ERROR_SUCCESS)
+        {
+            goto cleanup;
+        }
 
-       *RequiredSize = dwLength / sizeof(WCHAR);
+        *RequiredSize = dwLength / sizeof(WCHAR);
     }
 
     dwLength = ClassDescriptionSize * sizeof(WCHAR);
     if (RegQueryValueExW(hKey,
-                        NULL,
-                        NULL,
-                        NULL,
-                        (LPBYTE)ClassDescription,
-                        &dwLength))
+                         NULL,
+                         NULL,
+                         NULL,
+                         (LPBYTE)ClassDescription,
+                         &dwLength) != ERROR_SUCCESS)
     {
-       RegCloseKey(hKey);
-       return FALSE;
+        goto cleanup;
     }
 
-    RegCloseKey(hKey);
+    ret = TRUE;
 
-    return TRUE;
+cleanup:
+    if (hKey != INVALID_HANDLE_VALUE)
+        RegCloseKey(hKey);
+
+    return ret;
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsA (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsA(
-       CONST GUID *class,
-       LPCSTR enumstr,
-       HWND parent,
-       DWORD flags)
+HDEVINFO WINAPI
+SetupDiGetClassDevsA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags)
 {
-    return SetupDiGetClassDevsExA(class, enumstr, parent,
-                                  flags, NULL, NULL, NULL);
+    return SetupDiGetClassDevsExA(ClassGuid, Enumerator, hwndParent,
+                                  Flags, NULL, NULL, NULL);
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsW (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsW(
-       CONST GUID *class,
-       LPCWSTR enumstr,
-       HWND parent,
-       DWORD flags)
+HDEVINFO WINAPI
+SetupDiGetClassDevsW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCWSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags)
 {
-    return SetupDiGetClassDevsExW(class, enumstr, parent,
-                                  flags, NULL, NULL, NULL);
+    return SetupDiGetClassDevsExW(ClassGuid, Enumerator, hwndParent,
+                                  Flags, NULL, NULL, NULL);
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsExA (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsExA(
-       CONST GUID *class,
-       LPCSTR enumstr,
-       HWND parent,
-       DWORD flags,
-       HDEVINFO deviceset,
-       LPCSTR machine,
-       PVOID reserved)
+HDEVINFO WINAPI
+SetupDiGetClassDevsExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags,
+    IN HDEVINFO DeviceInfoSet OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HDEVINFO ret;
     LPWSTR enumstrW = NULL;
     LPWSTR machineW = NULL;
 
-    if (enumstr)
+    if (Enumerator)
     {
-        int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
+        int len = MultiByteToWideChar(CP_ACP, 0, Enumerator, -1, NULL, 0);
         enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
         if (!enumstrW)
         {
             ret = (HDEVINFO)INVALID_HANDLE_VALUE;
             goto end;
         }
-        MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
+        MultiByteToWideChar(CP_ACP, 0, Enumerator, -1, enumstrW, len);
     }
-    if (machine)
+    if (MachineName)
     {
-        int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
+        int len = MultiByteToWideChar(CP_ACP, 0, MachineName, -1, NULL, 0);
         machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
         if (!machineW)
         {
             ret = (HDEVINFO)INVALID_HANDLE_VALUE;
             goto end;
         }
-        MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
+        MultiByteToWideChar(CP_ACP, 0, MachineName, -1, machineW, len);
     }
-    ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset, machineW, reserved);
+    ret = SetupDiGetClassDevsExW(ClassGuid, enumstrW, hwndParent, Flags, DeviceInfoSet, machineW, Reserved);
 
 end:
     HeapFree(GetProcessHeap(), 0, enumstrW);
@@ -1422,13 +1659,14 @@ CreateDeviceInterface(
     return TRUE;
 }
 
-static LONG SETUP_CreateDevListFromEnumerator(
-       struct DeviceInfoSet *list,
-       LPCGUID pClassGuid OPTIONAL,
-       LPCWSTR Enumerator,
-       HKEY hEnumeratorKey) /* handle to Enumerator registry key */
+static LONG
+SETUP_CreateDevListFromEnumerator(
+    struct DeviceInfoSet *list,
+    CONST GUID *pClassGuid OPTIONAL,
+    LPCWSTR Enumerator,
+    HKEY hEnumeratorKey) /* handle to Enumerator registry key */
 {
-    HKEY hDeviceIdKey, hInstanceIdKey;
+    HKEY hDeviceIdKey = NULL, hInstanceIdKey;
     WCHAR KeyBuffer[MAX_PATH];
     WCHAR InstancePath[MAX_PATH];
     LPWSTR pEndOfInstancePath; /* Pointer into InstancePath buffer */
@@ -1445,13 +1683,15 @@ static LONG SETUP_CreateDevListFromEnumerator(
         if (rc == ERROR_NO_MORE_ITEMS)
             break;
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
         i++;
 
         /* Open device id sub key */
+        if (hDeviceIdKey != NULL)
+            RegCloseKey(hDeviceIdKey);
         rc = RegOpenKeyExW(hEnumeratorKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hDeviceIdKey);
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
         strcpyW(InstancePath, Enumerator);
         strcatW(InstancePath, L"\\");
         strcatW(InstancePath, KeyBuffer);
@@ -1469,19 +1709,13 @@ static LONG SETUP_CreateDevListFromEnumerator(
             if (rc == ERROR_NO_MORE_ITEMS)
                 break;
             if (rc != ERROR_SUCCESS)
-            {
-                RegCloseKey(hDeviceIdKey);
-                return rc;
-            }
+                goto cleanup;
             j++;
 
             /* Open instance id sub key */
             rc = RegOpenKeyExW(hDeviceIdKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hInstanceIdKey);
             if (rc != ERROR_SUCCESS)
-            {
-                RegCloseKey(hDeviceIdKey);
-                return rc;
-            }
+                goto cleanup;
             *pEndOfInstancePath = '\0';
             strcatW(InstancePath, KeyBuffer);
 
@@ -1499,13 +1733,12 @@ static LONG SETUP_CreateDevListFromEnumerator(
             }
             else if (rc != ERROR_SUCCESS)
             {
-                RegCloseKey(hDeviceIdKey);
-                return rc;
+                goto cleanup;
             }
             else if (dwRegType != REG_SZ)
             {
-                RegCloseKey(hDeviceIdKey);
-                return ERROR_GEN_FAILURE;
+                rc = ERROR_GEN_FAILURE;
+                goto cleanup;
             }
             else
             {
@@ -1524,25 +1757,32 @@ static LONG SETUP_CreateDevListFromEnumerator(
             /* Add the entry to the list */
             if (!CreateDeviceInfoElement(list, InstancePath, &KeyGuid, &deviceInfo))
             {
-                RegCloseKey(hDeviceIdKey);
-                return GetLastError();
+                rc = GetLastError();
+                goto cleanup;
             }
             TRACE("Adding '%s' to device info set %p\n", debugstr_w(InstancePath), list);
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
         }
-        RegCloseKey(hDeviceIdKey);
     }
 
-    return ERROR_SUCCESS;
+    rc = ERROR_SUCCESS;
+
+cleanup:
+    if (hDeviceIdKey != NULL)
+        RegCloseKey(hDeviceIdKey);
+    return rc;
 }
 
-static LONG SETUP_CreateDevList(
-       struct DeviceInfoSet *list,
-       PCWSTR MachineName OPTIONAL,
-       LPGUID class OPTIONAL,
-       PCWSTR Enumerator OPTIONAL)
+static LONG
+SETUP_CreateDevList(
+    struct DeviceInfoSet *list,
+    PCWSTR MachineName OPTIONAL,
+    CONST GUID *class OPTIONAL,
+    PCWSTR Enumerator OPTIONAL)
 {
-    HKEY HKLM, hEnumKey, hEnumeratorKey;
+    HKEY HKLM = HKEY_LOCAL_MACHINE;
+    HKEY hEnumKey = NULL;
+    HKEY hEnumeratorKey = NULL;
     WCHAR KeyBuffer[MAX_PATH];
     DWORD i;
     DWORD dwLength;
@@ -1551,24 +1791,22 @@ static LONG SETUP_CreateDevList(
     if (class && IsEqualIID(class, &GUID_NULL))
         class = NULL;
 
-    /* Open Enum key */
+    /* Open Enum key (if applicable) */
     if (MachineName != NULL)
     {
         rc = RegConnectRegistryW(MachineName, HKEY_LOCAL_MACHINE, &HKLM);
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
     }
-    else
-        HKLM = HKEY_LOCAL_MACHINE;
 
-    rc = RegOpenKeyExW(HKLM,
+    rc = RegOpenKeyExW(
+        HKLM,
         REGSTR_PATH_SYSTEMENUM,
         0,
         KEY_ENUMERATE_SUB_KEYS,
         &hEnumKey);
-    if (MachineName != NULL) RegCloseKey(HKLM);
     if (rc != ERROR_SUCCESS)
-        return rc;
+        goto cleanup;
 
     /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
      * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
@@ -1582,12 +1820,9 @@ static LONG SETUP_CreateDevList(
             0,
             KEY_ENUMERATE_SUB_KEYS,
             &hEnumeratorKey);
-        RegCloseKey(hEnumKey);
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
         rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
-        RegCloseKey(hEnumeratorKey);
-        return rc;
     }
     else
     {
@@ -1599,48 +1834,49 @@ static LONG SETUP_CreateDevList(
             rc = RegEnumKeyExW(hEnumKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
             if (rc == ERROR_NO_MORE_ITEMS)
                 break;
-            if (rc != ERROR_SUCCESS)
-            {
-                RegCloseKey(hEnumKey);
-                return rc;
-            }
+            else if (rc != ERROR_SUCCESS)
+                goto cleanup;
             i++;
 
             /* Open sub key */
+            if (hEnumeratorKey != NULL)
+                RegCloseKey(hEnumeratorKey);
             rc = RegOpenKeyExW(hEnumKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hEnumeratorKey);
             if (rc != ERROR_SUCCESS)
-            {
-                RegCloseKey(hEnumKey);
-                return rc;
-            }
+                goto cleanup;
 
             /* Call SETUP_CreateDevListFromEnumerator */
             rc = SETUP_CreateDevListFromEnumerator(list, class, KeyBuffer, hEnumeratorKey);
-            RegCloseKey(hEnumeratorKey);
             if (rc != ERROR_SUCCESS)
-            {
-                RegCloseKey(hEnumKey);
-                return rc;
-            }
+                goto cleanup;
         }
-        RegCloseKey(hEnumKey);
-        return ERROR_SUCCESS;
+        rc = ERROR_SUCCESS;
     }
+
+cleanup:
+    if (HKLM != HKEY_LOCAL_MACHINE)
+        RegCloseKey(HKLM);
+    if (hEnumKey != NULL)
+        RegCloseKey(hEnumKey);
+    if (hEnumeratorKey != NULL)
+        RegCloseKey(hEnumeratorKey);
+    return rc;
 }
 
-static BOOL DestroyDeviceInterface(struct DeviceInterface* deviceInterface)
+static BOOL
+DestroyDeviceInterface(
+    struct DeviceInterface* deviceInterface)
 {
-    HeapFree(GetProcessHeap(), 0, deviceInterface);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, deviceInterface);
 }
 
-static LONG SETUP_CreateInterfaceList(
-       struct DeviceInfoSet *list,
-       PCWSTR MachineName,
-       LPGUID InterfaceGuid,
-       PCWSTR DeviceInstanceW /* OPTIONAL */,
-       BOOL OnlyPresentInterfaces
-       )
+static LONG
+SETUP_CreateInterfaceList(
+    struct DeviceInfoSet *list,
+    PCWSTR MachineName,
+    CONST GUID *InterfaceGuid,
+    PCWSTR DeviceInstanceW /* OPTIONAL */,
+    BOOL OnlyPresentInterfaces)
 {
     HKEY hInterfaceKey;      /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID} */
     HKEY hDeviceInstanceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath} */
@@ -1660,7 +1896,8 @@ static LONG SETUP_CreateInterfaceList(
     struct DeviceInfoElement *deviceInfo;
 
     hInterfaceKey = INVALID_HANDLE_VALUE;
-    hDeviceInstanceKey = INVALID_HANDLE_VALUE;
+    hDeviceInstanceKey = NULL;
+    hReferenceKey = NULL;
 
     /* Open registry key related to this interface */
     hInterfaceKey = SetupDiOpenClassRegKeyExW(InterfaceGuid, KEY_ENUMERATE_SUB_KEYS, DIOCR_INTERFACE, MachineName, NULL);
@@ -1683,7 +1920,7 @@ static LONG SETUP_CreateInterfaceList(
         i++;
 
         /* Open sub key */
-        if (hDeviceInstanceKey != INVALID_HANDLE_VALUE)
+        if (hDeviceInstanceKey != NULL)
             RegCloseKey(hDeviceInstanceKey);
         rc = RegOpenKeyExW(hInterfaceKey, KeyBuffer, 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &hDeviceInstanceKey);
         if (rc != ERROR_SUCCESS)
@@ -1698,8 +1935,7 @@ static LONG SETUP_CreateInterfaceList(
             rc = ERROR_GEN_FAILURE;
             goto cleanup;
         }
-        if (InstancePath != NULL)
-            HeapFree(GetProcessHeap(), 0, InstancePath);
+        HeapFree(GetProcessHeap(), 0, InstancePath);
         InstancePath = HeapAlloc(GetProcessHeap(), 0, dwInstancePathLength + sizeof(WCHAR));
         if (!InstancePath)
         {
@@ -1773,7 +2009,7 @@ static LONG SETUP_CreateInterfaceList(
                 continue;
 
             /* Open sub key */
-            if (hReferenceKey != INVALID_HANDLE_VALUE)
+            if (hReferenceKey != NULL)
                 RegCloseKey(hReferenceKey);
             rc = RegOpenKeyExW(hDeviceInstanceKey, KeyBuffer, 0, KEY_QUERY_VALUE, &hReferenceKey);
             if (rc != ERROR_SUCCESS)
@@ -1800,8 +2036,7 @@ static LONG SETUP_CreateInterfaceList(
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
 
             /* Step 2. Create an interface list for this element */
-            if (pSymbolicLink != NULL)
-                HeapFree(GetProcessHeap(), 0, pSymbolicLink);
+            HeapFree(GetProcessHeap(), 0, pSymbolicLink);
             pSymbolicLink = HeapAlloc(GetProcessHeap(), 0, (dwLength + 1) * sizeof(WCHAR));
             if (!pSymbolicLink)
             {
@@ -1810,7 +2045,6 @@ static LONG SETUP_CreateInterfaceList(
             }
             rc = RegQueryValueExW(hReferenceKey, SymbolicLink, NULL, NULL, (LPBYTE)pSymbolicLink, &dwLength);
             pSymbolicLink[dwLength / sizeof(WCHAR)] = '\0';
-            RegCloseKey(hReferenceKey);
             if (rc != ERROR_SUCCESS)
                 goto cleanup;
             if (!CreateDeviceInterface(deviceInfo, pSymbolicLink, InterfaceGuid, &interfaceInfo))
@@ -1849,57 +2083,57 @@ static LONG SETUP_CreateInterfaceList(
     rc = ERROR_SUCCESS;
 
 cleanup:
-    if (hReferenceKey != INVALID_HANDLE_VALUE)
+    if (hReferenceKey != NULL)
         RegCloseKey(hReferenceKey);
-    if (hDeviceInstanceKey != INVALID_HANDLE_VALUE)
+    if (hDeviceInstanceKey != NULL)
         RegCloseKey(hDeviceInstanceKey);
     if (hInterfaceKey != INVALID_HANDLE_VALUE)
         RegCloseKey(hInterfaceKey);
-    if (InstancePath != NULL)
-        HeapFree(GetProcessHeap(), 0, InstancePath);
-    if (pSymbolicLink != NULL)
-        HeapFree(GetProcessHeap(), 0, pSymbolicLink);
+    HeapFree(GetProcessHeap(), 0, InstancePath);
+    HeapFree(GetProcessHeap(), 0, pSymbolicLink);
     return rc;
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsExW (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsExW(
-       CONST GUID *class,
-       LPCWSTR enumstr,
-       HWND parent,
-       DWORD flags,
-       HDEVINFO deviceset,
-       LPCWSTR machine,
-       PVOID reserved)
+HDEVINFO WINAPI
+SetupDiGetClassDevsExW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCWSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags,
+    IN HDEVINFO DeviceInfoSet OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
     struct DeviceInfoSet *list;
-    LPGUID pClassGuid;
+    CONST GUID *pClassGuid;
     LONG rc;
+    HDEVINFO ret = INVALID_HANDLE_VALUE;
 
-    TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(class), debugstr_w(enumstr),
-     parent, flags, deviceset, debugstr_w(machine), reserved);
+    TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(ClassGuid), debugstr_w(Enumerator),
+        hwndParent, Flags, DeviceInfoSet, debugstr_w(MachineName), Reserved);
 
     /* Create the deviceset if not set */
-    if (deviceset)
+    if (DeviceInfoSet)
     {
-        list = (struct DeviceInfoSet *)deviceset;
+        list = (struct DeviceInfoSet *)DeviceInfoSet;
         if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
         {
             SetLastError(ERROR_INVALID_HANDLE);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
-        hDeviceInfo = deviceset;
+        hDeviceInfo = DeviceInfoSet;
     }
     else
     {
          hDeviceInfo = SetupDiCreateDeviceInfoListExW(
-             flags & DIGCF_DEVICEINTERFACE ? NULL : class,
-             NULL, machine, NULL);
+             Flags & DIGCF_DEVICEINTERFACE ? NULL : ClassGuid,
+             NULL, MachineName, NULL);
          if (hDeviceInfo == INVALID_HANDLE_VALUE)
-             return INVALID_HANDLE_VALUE;
+             goto cleanup;
          list = (struct DeviceInfoSet *)hDeviceInfo;
     }
 
@@ -1908,62 +2142,60 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
     else
         pClassGuid = &list->ClassGuid;
 
-    if (flags & DIGCF_PROFILE)
+    if (Flags & DIGCF_PROFILE)
         FIXME(": flag DIGCF_PROFILE ignored\n");
 
-    if (flags & DIGCF_ALLCLASSES)
+    if (Flags & DIGCF_ALLCLASSES)
     {
-        rc = SETUP_CreateDevList(list, machine, pClassGuid, enumstr);
+        rc = SETUP_CreateDevList(list, MachineName, pClassGuid, Enumerator);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
-            if (!deviceset)
-                SetupDiDestroyDeviceInfoList(hDeviceInfo);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
-        return hDeviceInfo;
+        ret = hDeviceInfo;
     }
-    else if (flags & DIGCF_DEVICEINTERFACE)
+    else if (Flags & DIGCF_DEVICEINTERFACE)
     {
-        if (class == NULL)
+        if (ClassGuid == NULL)
         {
             SetLastError(ERROR_INVALID_PARAMETER);
-            if (!deviceset)
-                SetupDiDestroyDeviceInfoList(hDeviceInfo);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
 
-        rc = SETUP_CreateInterfaceList(list, machine, (LPGUID)class, enumstr, flags & DIGCF_PRESENT);
+        rc = SETUP_CreateInterfaceList(list, MachineName, ClassGuid, Enumerator, Flags & DIGCF_PRESENT);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
-            if (!deviceset)
-                SetupDiDestroyDeviceInfoList(hDeviceInfo);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
-        return hDeviceInfo;
+        ret = hDeviceInfo;
     }
     else
     {
-        rc = SETUP_CreateDevList(list, machine, (LPGUID)class, enumstr);
+        rc = SETUP_CreateDevList(list, MachineName, ClassGuid, Enumerator);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
-            if (!deviceset)
-                SetupDiDestroyDeviceInfoList(hDeviceInfo);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
-        return hDeviceInfo;
+        ret = hDeviceInfo;
     }
+
+cleanup:
+    if (!DeviceInfoSet && hDeviceInfo != INVALID_HANDLE_VALUE && hDeviceInfo != ret)
+        SetupDiDestroyDeviceInfoList(hDeviceInfo);
+    return ret;
 }
 
 /***********************************************************************
  *             SetupDiGetClassImageIndex (SETUPAPI.@)
  */
 
-static BOOL GetIconIndex(
-       IN HKEY hClassKey,
-       OUT PINT ImageIndex)
+static BOOL
+GetIconIndex(
+    IN HKEY hClassKey,
+    OUT PINT ImageIndex)
 {
     LPWSTR Buffer = NULL;
     DWORD dwRegType, dwLength;
@@ -2005,10 +2237,11 @@ cleanup:
     return ret;
 }
 
-BOOL WINAPI SetupDiGetClassImageIndex(
-       IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN CONST GUID *ClassGuid,
-       OUT PINT ImageIndex)
+BOOL WINAPI
+SetupDiGetClassImageIndex(
+    IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN CONST GUID *ClassGuid,
+    OUT PINT ImageIndex)
 {
     struct ClassImageList *list;
     BOOL ret = FALSE;
@@ -2058,8 +2291,9 @@ cleanup:
 /***********************************************************************
  *             SetupDiGetClassImageList(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageList(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
+BOOL WINAPI
+SetupDiGetClassImageList(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
 {
     return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
 }
@@ -2067,10 +2301,11 @@ BOOL WINAPI SetupDiGetClassImageList(
 /***********************************************************************
  *             SetupDiGetClassImageListExA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageListExA(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN PCSTR MachineName OPTIONAL,
-       IN PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassImageListExA(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWSTR MachineNameW = NULL;
     BOOL ret;
@@ -2093,10 +2328,11 @@ BOOL WINAPI SetupDiGetClassImageListExA(
 /***********************************************************************
  *             SetupDiGetClassImageListExW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageListExW(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN PCWSTR MachineName OPTIONAL,
-       IN PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassImageListExW(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     BOOL ret = FALSE;
 
@@ -2134,7 +2370,7 @@ BOOL WINAPI SetupDiGetClassImageListExW(
             list->MachineName = NULL;
         }
 
-        ClassImageListData->Reserved = (DWORD)list; /* FIXME: 64 bit portability issue */
+        ClassImageListData->Reserved = (ULONG_PTR)list;
         ret = TRUE;
 
 cleanup:
@@ -2149,10 +2385,11 @@ cleanup:
 /***********************************************************************
  *             SetupDiLoadClassIcon(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiLoadClassIcon(
-       IN CONST GUID *ClassGuid,
-       OUT HICON *LargeIcon OPTIONAL,
-       OUT PINT MiniIconIndex OPTIONAL)
+BOOL WINAPI
+SetupDiLoadClassIcon(
+    IN CONST GUID *ClassGuid,
+    OUT HICON *LargeIcon OPTIONAL,
+    OUT PINT MiniIconIndex OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -2263,12 +2500,13 @@ cleanup:
 /***********************************************************************
  *             SetupDiEnumDeviceInterfaces (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiEnumDeviceInterfaces(
-       HDEVINFO DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       CONST GUID * InterfaceClassGuid,
-       DWORD MemberIndex,
-       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
+BOOL WINAPI
+SetupDiEnumDeviceInterfaces(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN CONST GUID *InterfaceClassGuid,
+    IN DWORD MemberIndex,
+    OUT PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
 {
     BOOL ret = FALSE;
 
@@ -2290,7 +2528,7 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(
             while (ItemList != &list->ListHead && !Found)
             {
                 PLIST_ENTRY InterfaceListEntry;
-                struct DeviceInfoElement *DevInfo = (struct DeviceInfoElement *)ItemList;
+                struct DeviceInfoElement *DevInfo = CONTAINING_RECORD(ItemList, struct DeviceInfoElement, ListEntry);
                 if (DeviceInfoData && (struct DeviceInfoElement *)DeviceInfoData->Reserved != DevInfo)
                 {
                     /* We are not searching for this element */
@@ -2300,7 +2538,7 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(
                 InterfaceListEntry = DevInfo->InterfaceListHead.Flink;
                 while (InterfaceListEntry != &DevInfo->InterfaceListHead && !Found)
                 {
-                    struct DeviceInterface *DevItf = (struct DeviceInterface *)InterfaceListEntry;
+                    struct DeviceInterface *DevItf = CONTAINING_RECORD(InterfaceListEntry, struct DeviceInterface, ListEntry);
                     if (!IsEqualIID(&DevItf->InterfaceClassGuid, InterfaceClassGuid))
                     {
                         InterfaceListEntry = InterfaceListEntry->Flink;
@@ -2333,12 +2571,14 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(
     return ret;
 }
 
-static VOID ReferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+ReferenceInfFile(struct InfFileDetails* infFile)
 {
     InterlockedIncrement(&infFile->References);
 }
 
-static VOID DereferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+DereferenceInfFile(struct InfFileDetails* infFile)
 {
     if (InterlockedDecrement(&infFile->References) == 0)
     {
@@ -2347,7 +2587,8 @@ static VOID DereferenceInfFile(struct InfFileDetails* infFile)
     }
 }
 
-static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
+static BOOL
+DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
 {
     DereferenceInfFile(driverInfo->InfFileDetails);
     HeapFree(GetProcessHeap(), 0, driverInfo->MatchingId);
@@ -2355,13 +2596,14 @@ static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
     return TRUE;
 }
 
-static BOOL DestroyClassInstallParams(struct ClassInstallParams* installParams)
+static BOOL
+DestroyClassInstallParams(struct ClassInstallParams* installParams)
 {
-    HeapFree(GetProcessHeap(), 0, installParams->PropChange);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, installParams->PropChange);
 }
 
-static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
+static BOOL
+DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
 {
     PLIST_ENTRY ListEntry;
     struct DriverInfoElement *driverInfo;
@@ -2382,11 +2624,11 @@ static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
             return FALSE;
     }
     DestroyClassInstallParams(&deviceInfo->ClassInstallParams);
-    HeapFree(GetProcessHeap(), 0, deviceInfo);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, deviceInfo);
 }
 
-static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
+static BOOL
+DestroyDeviceInfoSet(struct DeviceInfoSet* list)
 {
     PLIST_ENTRY ListEntry;
     struct DeviceInfoElement *deviceInfo;
@@ -2394,7 +2636,7 @@ static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
     while (!IsListEmpty(&list->ListHead))
     {
         ListEntry = RemoveHeadList(&list->ListHead);
-        deviceInfo = (struct DeviceInfoElement *)ListEntry;
+        deviceInfo = CONTAINING_RECORD(ListEntry, struct DeviceInfoElement, ListEntry);
         if (!DestroyDeviceInfoElement(deviceInfo))
             return FALSE;
     }
@@ -2402,21 +2644,22 @@ static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
         RegCloseKey(list->HKLM);
     CM_Disconnect_Machine(list->hMachine);
     DestroyClassInstallParams(&list->ClassInstallParams);
-    HeapFree(GetProcessHeap(), 0, list);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, list);
 }
 
 /***********************************************************************
  *             SetupDiDestroyDeviceInfoList (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
+BOOL WINAPI
+SetupDiDestroyDeviceInfoList(
+    IN HDEVINFO DeviceInfoSet)
 {
     BOOL ret = FALSE;
 
-    TRACE("%p\n", devinfo);
-    if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
+    TRACE("%p\n", DeviceInfoSet);
+    if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
     {
-        struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
+        struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
 
         if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
             ret = DestroyDeviceInfoSet(list);
@@ -2433,13 +2676,14 @@ BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
-      HDEVINFO DeviceInfoSet,
-      PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
-      PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
-      DWORD DeviceInterfaceDetailDataSize,
-      PDWORD RequiredSize,
-      PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiGetDeviceInterfaceDetailA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
+    OUT PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData OPTIONAL,
+    IN DWORD DeviceInterfaceDetailDataSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailDataW = NULL;
     DWORD sizeW = 0, sizeA;
@@ -2503,13 +2747,14 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
-      HDEVINFO DeviceInfoSet,
-      PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
-      PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
-      DWORD DeviceInterfaceDetailDataSize,
-      PDWORD RequiredSize,
-      PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiGetDeviceInterfaceDetailW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
+    OUT PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData OPTIONAL,
+    IN DWORD DeviceInterfaceDetailDataSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -2569,31 +2814,40 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
-        HDEVINFO  devinfo,
-        PSP_DEVINFO_DATA  DeviceInfoData,
-        DWORD   Property,
-        PDWORD  PropertyRegDataType,
-        PBYTE   PropertyBuffer,
-        DWORD   PropertyBufferSize,
-        PDWORD  RequiredSize)
+BOOL WINAPI
+SetupDiGetDeviceRegistryPropertyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    OUT PDWORD PropertyRegDataType OPTIONAL,
+    OUT PBYTE PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize,
+    OUT PDWORD  RequiredSize OPTIONAL)
 {
     BOOL bResult;
     BOOL bIsStringProperty;
     DWORD RegType;
     DWORD RequiredSizeA, RequiredSizeW;
-    DWORD PropertyBufferSizeW;
-    PBYTE PropertyBufferW;
+    DWORD PropertyBufferSizeW = 0;
+    PBYTE PropertyBufferW = NULL;
 
-    TRACE("%p %p %ld %p %p %ld %p\n", devinfo, DeviceInfoData,
+    TRACE("%p %p %ld %p %p %ld %p\n", DeviceInfoSet, DeviceInfoData,
         Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
         RequiredSize);
 
-    PropertyBufferSizeW = PropertyBufferSize * 2;
-    PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
+    if (PropertyBufferSize != 0)
+    {
+        PropertyBufferSizeW = PropertyBufferSize * 2;
+        PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
+        if (!PropertyBufferW)
+        {
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
+    }
 
     bResult = SetupDiGetDeviceRegistryPropertyW(
-        devinfo,
+        DeviceInfoSet,
         DeviceInfoData,
         Property,
         &RegType,
@@ -2647,14 +2901,15 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
-        HDEVINFO  DeviceInfoSet,
-        PSP_DEVINFO_DATA  DeviceInfoData,
-        DWORD   Property,
-        PDWORD  PropertyRegDataType,
-        PBYTE   PropertyBuffer,
-        DWORD   PropertyBufferSize,
-        PDWORD  RequiredSize)
+BOOL WINAPI
+SetupDiGetDeviceRegistryPropertyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    OUT PDWORD PropertyRegDataType OPTIONAL,
+    OUT PBYTE PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize,
+    OUT PDWORD  RequiredSize OPTIONAL)
 {
     HKEY hEnumKey, hKey;
     DWORD rc;
@@ -2845,12 +3100,13 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
-        IN HDEVINFO DeviceInfoSet,
-        IN OUT PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Property,
-        IN CONST BYTE *PropertyBuffer,
-        IN DWORD PropertyBufferSize)
+BOOL WINAPI
+SetupDiSetDeviceRegistryPropertyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    IN CONST BYTE *PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize)
 {
     FIXME("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
         Property, PropertyBuffer, PropertyBufferSize);
@@ -2861,12 +3117,13 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
-        IN HDEVINFO DeviceInfoSet,
-        IN OUT PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Property,
-        IN const BYTE *PropertyBuffer,
-        IN DWORD PropertyBufferSize)
+BOOL WINAPI
+SetupDiSetDeviceRegistryPropertyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    IN CONST BYTE *PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -2995,11 +3252,12 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiInstallClassA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassA(
-        IN HWND hwndParent OPTIONAL,
-        IN PCSTR InfFileName,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL)
+BOOL WINAPI
+SetupDiInstallClassA(
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR InfFileName,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL)
 {
     return SetupDiInstallClassExA(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3008,11 +3266,12 @@ BOOL WINAPI SetupDiInstallClassA(
 /***********************************************************************
  *             SetupDiInstallClassW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassW(
-        IN HWND hwndParent OPTIONAL,
-        IN PCWSTR InfFileName,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL)
+BOOL WINAPI
+SetupDiInstallClassW(
+    IN HWND hwndParent OPTIONAL,
+    IN PCWSTR InfFileName,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL)
 {
     return SetupDiInstallClassExW(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3021,14 +3280,15 @@ BOOL WINAPI SetupDiInstallClassW(
 /***********************************************************************
  *             SetupDiInstallClassExA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassExA(
-        IN HWND hwndParent OPTIONAL,
-        IN PCSTR InfFileName OPTIONAL,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL,
-        IN const GUID* InterfaceClassGuid OPTIONAL,
-        IN PVOID Reserved1,
-        IN PVOID Reserved2)
+BOOL WINAPI
+SetupDiInstallClassExA(
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR InfFileName OPTIONAL,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL,
+    IN CONST GUID *InterfaceClassGuid OPTIONAL,
+    IN PVOID Reserved1,
+    IN PVOID Reserved2)
 {
     PWSTR InfFileNameW = NULL;
     BOOL Result;
@@ -3052,82 +3312,89 @@ BOOL WINAPI SetupDiInstallClassExA(
 }
 
 
-static HKEY CreateClassKey(HINF hInf)
+static HKEY
+CreateClassKey(HINF hInf)
 {
     WCHAR FullBuffer[MAX_PATH];
     WCHAR Buffer[MAX_PATH];
     DWORD RequiredSize;
-    HKEY hClassKey;
+    HKEY hClassKey = NULL;
+    HKEY ret = INVALID_HANDLE_VALUE;
 
+    FullBuffer[0] = '\0';
     Buffer[0] = '\\';
     if (!SetupGetLineTextW(NULL,
-                          hInf,
-                          Version,
-                          ClassGUID,
-                          &Buffer[1],
-                          MAX_PATH - 1,
-                          &RequiredSize))
+                           hInf,
+                           Version,
+                           ClassGUID,
+                           &Buffer[1],
+                           MAX_PATH - 1,
+                           &RequiredSize))
     {
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
     lstrcpyW(FullBuffer, REGSTR_PATH_CLASS_NT);
     lstrcatW(FullBuffer, Buffer);
 
-
     if (!SetupGetLineTextW(NULL,
-                              hInf,
-                              Version,
-                              Class,
-                              Buffer,
-                              MAX_PATH,
-                              &RequiredSize))
+                           hInf,
+                           Version,
+                           Class,
+                           Buffer,
+                           MAX_PATH,
+                           &RequiredSize))
     {
         RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
     if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_LOCAL_MACHINE,
-                           FullBuffer,
-                           0,
-                           NULL,
-                           REG_OPTION_NON_VOLATILE,
-                           KEY_SET_VALUE,
-                           NULL,
-                           &hClassKey,
-             NULL))
+                                         FullBuffer,
+                                         0,
+                                         NULL,
+                                         REG_OPTION_NON_VOLATILE,
+                                         KEY_SET_VALUE,
+                                         NULL,
+                                         &hClassKey,
+                                         NULL))
     {
-        RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
     if (ERROR_SUCCESS != RegSetValueExW(hClassKey,
-                      Class,
-                      0,
-                      REG_SZ,
-                      (LPBYTE)Buffer,
-             RequiredSize * sizeof(WCHAR)))
+                                        Class,
+                                        0,
+                                        REG_SZ,
+                                        (LPBYTE)Buffer,
+                                        RequiredSize * sizeof(WCHAR)))
     {
-        RegCloseKey(hClassKey);
-        RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
-    return hClassKey;
+    ret = hClassKey;
+
+cleanup:
+    if (hClassKey != NULL && hClassKey != ret)
+        RegCloseKey(hClassKey);
+    if (ret == INVALID_HANDLE_VALUE && FullBuffer[0] != '\0')
+        RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
+    return ret;
 }
 
 
 /***********************************************************************
  *             SetupDiInstallClassExW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassExW(
-        IN HWND hwndParent OPTIONAL,
-        IN PCWSTR InfFileName OPTIONAL,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL,
-        IN const GUID* InterfaceClassGuid OPTIONAL,
-        IN PVOID Reserved1,
-        IN PVOID Reserved2)
+BOOL WINAPI
+SetupDiInstallClassExW(
+    IN HWND hwndParent OPTIONAL,
+    IN PCWSTR InfFileName OPTIONAL,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL,
+    IN CONST GUID *InterfaceClassGuid OPTIONAL,
+    IN PVOID Reserved1,
+    IN PVOID Reserved2)
 {
     BOOL ret = FALSE;
 
@@ -3236,7 +3503,7 @@ BOOL WINAPI SetupDiInstallClassExW(
                 SectionName,
                 SPINST_REGISTRY | SPINST_FILES | SPINST_BITREG | SPINST_INIFILES | SPINST_INI2REG,
                 hRootKey,
-                NULL, /* SourceRootPath */
+                NULL, /* FIXME: SourceRootPath */
                 !(Flags & DI_NOVCP) && (Flags & DI_FORCECOPY) ? SP_COPY_FORCE_IN_USE : 0, /* CopyFlags */
                 SetupDefaultQueueCallbackW,
                 callback_context,
@@ -3279,9 +3546,10 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenClassRegKey  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKey(
-        const GUID* ClassGuid,
-        REGSAM samDesired)
+HKEY WINAPI
+SetupDiOpenClassRegKey(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN REGSAM samDesired)
 {
     return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
                                      DIOCR_INSTALLER, NULL, NULL);
@@ -3291,12 +3559,13 @@ HKEY WINAPI SetupDiOpenClassRegKey(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExA  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKeyExA(
-        const GUID* ClassGuid OPTIONAL,
-        REGSAM samDesired,
-        DWORD Flags,
-        PCSTR MachineName OPTIONAL,
-        PVOID Reserved)
+HKEY WINAPI
+SetupDiOpenClassRegKeyExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN REGSAM samDesired,
+    IN DWORD Flags,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWSTR MachineNameW = NULL;
     HKEY hKey;
@@ -3323,19 +3592,21 @@ HKEY WINAPI SetupDiOpenClassRegKeyExA(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExW  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKeyExW(
-        const GUID* ClassGuid OPTIONAL,
-        REGSAM samDesired,
-        DWORD Flags,
-        PCWSTR MachineName OPTIONAL,
-        PVOID Reserved)
+HKEY WINAPI
+SetupDiOpenClassRegKeyExW(
+    IN CONST GUID* ClassGuid OPTIONAL,
+    IN REGSAM samDesired,
+    IN DWORD Flags,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
-    LPWSTR lpGuidString;
-    LPWSTR lpFullGuidString;
+    LPWSTR lpGuidString = NULL;
+    LPWSTR lpFullGuidString = NULL;
     DWORD dwLength;
     HKEY HKLM;
-    HKEY hClassesKey;
-    HKEY hClassKey;
+    HKEY hClassesKey = NULL;
+    HKEY hClassKey = NULL;
+    HKEY ret = INVALID_HANDLE_VALUE;
     DWORD rc;
     LPCWSTR lpKeyName;
 
@@ -3343,18 +3614,14 @@ HKEY WINAPI SetupDiOpenClassRegKeyExW(
         Flags, debugstr_w(MachineName), Reserved);
 
     if (Flags == DIOCR_INSTALLER)
-    {
         lpKeyName = REGSTR_PATH_CLASS_NT;
-    }
     else if (Flags == DIOCR_INTERFACE)
-    {
         lpKeyName = REGSTR_PATH_DEVICE_CLASSES;
-    }
     else
     {
         ERR("Invalid Flags parameter!\n");
         SetLastError(ERROR_INVALID_FLAGS);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
     if (MachineName != NULL)
@@ -3363,32 +3630,35 @@ HKEY WINAPI SetupDiOpenClassRegKeyExW(
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
     }
     else
         HKLM = HKEY_LOCAL_MACHINE;
 
     rc = RegOpenKeyExW(HKLM,
-                     lpKeyName,
-                     0,
-                     ClassGuid ? 0 : samDesired,
-                     &hClassesKey);
+                      lpKeyName,
+                      0,
+                      ClassGuid ? 0 : samDesired,
+                      &hClassesKey);
     if (MachineName != NULL) RegCloseKey(HKLM);
     if (rc != ERROR_SUCCESS)
     {
-       SetLastError(rc);
-       return INVALID_HANDLE_VALUE;
+        SetLastError(rc);
+        goto cleanup;
     }
 
     if (ClassGuid == NULL)
-        return hClassesKey;
+    {
+        /* Stop here. We don't need to open a subkey */
+        ret = hClassesKey;
+        goto cleanup;
+    }
 
     if (UuidToStringW((UUID*)ClassGuid, &lpGuidString) != RPC_S_OK)
     {
-       SetLastError(ERROR_GEN_FAILURE);
-       RegCloseKey(hClassesKey);
-       return INVALID_HANDLE_VALUE;
+        SetLastError(ERROR_GEN_FAILURE);
+        goto cleanup;
     }
 
     dwLength = lstrlenW(lpGuidString);
@@ -3396,42 +3666,46 @@ HKEY WINAPI SetupDiOpenClassRegKeyExW(
     if (!lpFullGuidString)
     {
         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
-        RpcStringFreeW(&lpGuidString);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
     lpFullGuidString[0] = '{';
     memcpy(&lpFullGuidString[1], lpGuidString, dwLength * sizeof(WCHAR));
     lpFullGuidString[dwLength + 1] = '}';
     lpFullGuidString[dwLength + 2] = '\0';
-    RpcStringFreeW(&lpGuidString);
 
     rc = RegOpenKeyExW(hClassesKey,
-                     lpFullGuidString,
-                     0,
-                     samDesired,
-                     &hClassKey);
+                       lpFullGuidString,
+                       0,
+                       samDesired,
+                       &hClassKey);
     if (rc != ERROR_SUCCESS)
     {
-       SetLastError(rc);
-       HeapFree(GetProcessHeap(), 0, lpFullGuidString);
-       RegCloseKey(hClassesKey);
-       return INVALID_HANDLE_VALUE;
+        SetLastError(rc);
+        goto cleanup;
     }
+    ret = hClassKey;
 
+cleanup:
+    if (hClassKey != NULL && hClassKey != ret)
+        RegCloseKey(hClassKey);
+    if (hClassesKey != NULL && hClassesKey != ret)
+        RegCloseKey(hClassesKey);
+    if (lpGuidString)
+        RpcStringFreeW(&lpGuidString);
     HeapFree(GetProcessHeap(), 0, lpFullGuidString);
-    RegCloseKey(hClassesKey);
 
-    return hClassKey;
+    return ret;
 }
 
 /***********************************************************************
  *             SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiOpenDeviceInterfaceW(
-       HDEVINFO DeviceInfoSet,
-       PCWSTR DevicePath,
-       DWORD OpenFlags,
-       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
+BOOL WINAPI
+SetupDiOpenDeviceInterfaceW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCWSTR DevicePath,
+    IN DWORD OpenFlags,
+    OUT PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData OPTIONAL)
 {
     FIXME("%p %s %08lx %p\n",
         DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
@@ -3441,11 +3715,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceW(
 /***********************************************************************
  *             SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiOpenDeviceInterfaceA(
-       HDEVINFO DeviceInfoSet,
-       PCSTR DevicePath,
-       DWORD OpenFlags,
-       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
+BOOL WINAPI
+SetupDiOpenDeviceInterfaceA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCSTR DevicePath,
+    IN DWORD OpenFlags,
+    OUT PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData OPTIONAL)
 {
     LPWSTR DevicePathW = NULL;
     BOOL bResult;
@@ -3467,11 +3742,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetClassInstallParamsA(
-       HDEVINFO  DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       PSP_CLASSINSTALL_HEADER ClassInstallParams,
-       DWORD ClassInstallParamsSize)
+BOOL WINAPI
+SetupDiSetClassInstallParamsA(
+    IN HDEVINFO  DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     FIXME("%p %p %x %lu\n",DeviceInfoSet, DeviceInfoData,
           ClassInstallParams->InstallFunction, ClassInstallParamsSize);
@@ -3481,11 +3757,12 @@ BOOL WINAPI SetupDiSetClassInstallParamsA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetClassInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
-       IN DWORD ClassInstallParamsSize)
+BOOL WINAPI
+SetupDiSetClassInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -3550,11 +3827,12 @@ done:
     return ret;
 }
 
-static BOOL PropertyChangeHandler(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData,
-       IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
-       IN DWORD ClassInstallParamsSize)
+static BOOL
+PropertyChangeHandler(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     PSP_PROPCHANGE_PARAMS PropChangeParams = (PSP_PROPCHANGE_PARAMS)ClassInstallParams;
     BOOL ret = FALSE;
@@ -3611,9 +3889,9 @@ done:
 
 static DWORD
 GetFunctionPointer(
-        IN PWSTR InstallerName,
-        OUT HMODULE* ModulePointer,
-        OUT PVOID* FunctionPointer)
+    IN PWSTR InstallerName,
+    OUT HMODULE* ModulePointer,
+    OUT PVOID* FunctionPointer)
 {
     HMODULE hModule = NULL;
     LPSTR FunctionNameA = NULL;
@@ -3672,8 +3950,8 @@ cleanup:
 
 static DWORD
 FreeFunctionPointer(
-        IN HMODULE ModulePointer,
-        IN PVOID FunctionPointer)
+    IN HMODULE ModulePointer,
+    IN PVOID FunctionPointer)
 {
     if (ModulePointer == NULL)
         return ERROR_SUCCESS;
@@ -3694,10 +3972,11 @@ IntSetupDiRegisterDeviceInfo(
 /***********************************************************************
  *             SetupDiCallClassInstaller (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCallClassInstaller(
-       IN DI_FUNCTION InstallFunction,
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+BOOL WINAPI
+SetupDiCallClassInstaller(
+    IN DI_FUNCTION InstallFunction,
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -3942,7 +4221,8 @@ BOOL WINAPI SetupDiCallClassInstaller(
             ListEntry = ClassCoInstallersListHead.Flink;
             while (rc == NO_ERROR && ListEntry != &ClassCoInstallersListHead)
             {
-                struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
+                struct CoInstallerElement *coinstaller;
+                coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry);
                 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
                 coinstaller->PrivateData = Context.PrivateData;
                 if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
@@ -3957,7 +4237,8 @@ BOOL WINAPI SetupDiCallClassInstaller(
             ListEntry = DeviceCoInstallersListHead.Flink;
             while (rc == NO_ERROR && ListEntry != &DeviceCoInstallersListHead)
             {
-                struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
+                struct CoInstallerElement *coinstaller;
+                coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry);
                 rc = (*coinstaller->Function)(InstallFunction, DeviceInfoSet, DeviceInfoData, &Context);
                 coinstaller->PrivateData = Context.PrivateData;
                 if (rc == ERROR_DI_POSTPROCESSING_REQUIRED)
@@ -3996,7 +4277,8 @@ BOOL WINAPI SetupDiCallClassInstaller(
             ListEntry = ClassCoInstallersListHead.Flink;
             while (ListEntry != &ClassCoInstallersListHead)
             {
-                struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
+                struct CoInstallerElement *coinstaller;
+                coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry);
                 if (coinstaller->DoPostProcessing)
                 {
                     Context.InstallResult = rc;
@@ -4011,7 +4293,8 @@ BOOL WINAPI SetupDiCallClassInstaller(
             ListEntry = DeviceCoInstallersListHead.Flink;
             while (ListEntry != &DeviceCoInstallersListHead)
             {
-                struct CoInstallerElement *coinstaller = (struct CoInstallerElement *)ListEntry;
+                struct CoInstallerElement *coinstaller;
+                coinstaller = CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry);
                 if (coinstaller->DoPostProcessing)
                 {
                     Context.InstallResult = rc;
@@ -4026,12 +4309,12 @@ BOOL WINAPI SetupDiCallClassInstaller(
             while (!IsListEmpty(&ClassCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&ClassCoInstallersListHead);
-                HeapFree(GetProcessHeap(), 0, ListEntry);
+                HeapFree(GetProcessHeap(), 0, CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry));
             }
             while (!IsListEmpty(&DeviceCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&DeviceCoInstallersListHead);
-                HeapFree(GetProcessHeap(), 0, ListEntry);
+                HeapFree(GetProcessHeap(), 0, CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry));
             }
 
             ret = (rc == NO_ERROR);
@@ -4045,9 +4328,10 @@ BOOL WINAPI SetupDiCallClassInstaller(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListClass  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInfoListClass(
-        IN HDEVINFO DeviceInfoSet,
-        OUT LPGUID ClassGuid)
+BOOL WINAPI
+SetupDiGetDeviceInfoListClass(
+    IN HDEVINFO DeviceInfoSet,
+    OUT LPGUID ClassGuid)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4074,9 +4358,10 @@ BOOL WINAPI SetupDiGetDeviceInfoListClass(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListDetailW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
-        IN HDEVINFO DeviceInfoSet,
-        OUT PSP_DEVINFO_LIST_DETAIL_DATA_W DeviceInfoListDetailData)
+BOOL WINAPI
+SetupDiGetDeviceInfoListDetailW(
+    IN HDEVINFO DeviceInfoSet,
+    OUT PSP_DEVINFO_LIST_DETAIL_DATA_W DeviceInfoListDetailData)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4113,10 +4398,11 @@ BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstallParamsA(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       OUT PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
+BOOL WINAPI
+SetupDiGetDeviceInstallParamsA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    OUT PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
 {
     SP_DEVINSTALL_PARAMS_W deviceInstallParamsW;
     BOOL ret = FALSE;
@@ -4155,10 +4441,11 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsA(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       OUT PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+BOOL WINAPI
+SetupDiGetDeviceInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    OUT PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4193,7 +4480,7 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsW(
 
 static BOOL
 CheckDeviceInstallParameters(
-       IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+    IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     DWORD SupportedFlags =
         DI_NOVCP |                            /* 0x00000008 */
@@ -4248,10 +4535,11 @@ CheckDeviceInstallParameters(
 /***********************************************************************
  *             SetupDiSetDeviceInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData,
-       IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+BOOL WINAPI
+SetupDiSetDeviceInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4287,12 +4575,13 @@ BOOL WINAPI SetupDiSetDeviceInstallParamsW(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstanceIdA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        OUT PSTR DeviceInstanceId OPTIONAL,
-        IN DWORD DeviceInstanceIdSize,
-        OUT PDWORD RequiredSize OPTIONAL)
+BOOL WINAPI
+SetupDiGetDeviceInstanceIdA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    OUT PSTR DeviceInstanceId OPTIONAL,
+    IN DWORD DeviceInstanceIdSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
     PWSTR DeviceInstanceIdW = NULL;
     BOOL ret = FALSE;
@@ -4333,12 +4622,13 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdA(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstanceIdW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        OUT PWSTR DeviceInstanceId OPTIONAL,
-        IN DWORD DeviceInstanceIdSize,
-        OUT PDWORD RequiredSize OPTIONAL)
+BOOL WINAPI
+SetupDiGetDeviceInstanceIdW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    OUT PWSTR DeviceInstanceId OPTIONAL,
+    IN DWORD DeviceInstanceIdSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -4382,13 +4672,14 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdW(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDevPropertySheetsA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-        IN LPPROPSHEETHEADERA PropertySheetHeader,
-        IN DWORD PropertySheetHeaderPageListSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        IN DWORD PropertySheetType)
+BOOL WINAPI
+SetupDiGetClassDevPropertySheetsA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN LPPROPSHEETHEADERA PropertySheetHeader,
+    IN DWORD PropertySheetHeaderPageListSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN DWORD PropertySheetType)
 {
     PROPSHEETHEADERW psh;
     BOOL ret = FALSE;
@@ -4420,9 +4711,10 @@ struct ClassDevPropertySheetsData
     DWORD NumberOfPages;
 };
 
-static BOOL WINAPI GetClassDevPropertySheetsCallback(
-        IN HPROPSHEETPAGE hPropSheetPage,
-        IN OUT LPARAM lParam)
+static BOOL WINAPI
+GetClassDevPropertySheetsCallback(
+    IN HPROPSHEETPAGE hPropSheetPage,
+    IN OUT LPARAM lParam)
 {
     struct ClassDevPropertySheetsData *PropPageData;
 
@@ -4441,13 +4733,14 @@ static BOOL WINAPI GetClassDevPropertySheetsCallback(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDevPropertySheetsW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-        IN OUT LPPROPSHEETHEADERW PropertySheetHeader,
-        IN DWORD PropertySheetHeaderPageListSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        IN DWORD PropertySheetType)
+BOOL WINAPI
+SetupDiGetClassDevPropertySheetsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT LPPROPSHEETHEADERW PropertySheetHeader,
+    IN DWORD PropertySheetHeaderPageListSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN DWORD PropertySheetType)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4572,14 +4865,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyA (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiCreateDevRegKeyA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Scope,
-        IN DWORD HwProfile,
-        IN DWORD KeyType,
-        IN HINF InfHandle OPTIONAL,
-        IN PCSTR InfSectionName OPTIONAL)
+HKEY WINAPI
+SetupDiCreateDevRegKeyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN HINF InfHandle OPTIONAL,
+    IN PCSTR InfSectionName OPTIONAL)
 {
     PCWSTR InfSectionNameW = NULL;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4606,12 +4900,12 @@ HKEY WINAPI SetupDiCreateDevRegKeyA(
 
 static HKEY
 OpenHardwareProfileKey(
-        IN HKEY HKLM,
-        IN DWORD HwProfile,
-        IN DWORD samDesired)
+    IN HKEY HKLM,
+    IN DWORD HwProfile,
+    IN DWORD samDesired)
 {
-    HKEY hHWProfilesKey = INVALID_HANDLE_VALUE;
-    HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
+    HKEY hHWProfilesKey = NULL;
+    HKEY hHWProfileKey = NULL;
     HKEY ret = INVALID_HANDLE_VALUE;
     LONG rc;
 
@@ -4654,9 +4948,9 @@ OpenHardwareProfileKey(
     ret = hHWProfileKey;
 
 cleanup:
-    if (hHWProfilesKey != INVALID_HANDLE_VALUE)
+    if (hHWProfilesKey != NULL)
         RegCloseKey(hHWProfilesKey);
-    if (hHWProfileKey != INVALID_HANDLE_VALUE && hHWProfileKey != ret)
+    if (hHWProfileKey != NULL && hHWProfileKey != ret)
         RegCloseKey(hHWProfileKey);
     return ret;
 }
@@ -4664,14 +4958,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyW (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiCreateDevRegKeyW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Scope,
-        IN DWORD HwProfile,
-        IN DWORD KeyType,
-        IN HINF InfHandle OPTIONAL,
-        IN PCWSTR InfSectionName OPTIONAL)
+HKEY WINAPI
+SetupDiCreateDevRegKeyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN HINF InfHandle OPTIONAL,
+    IN PCWSTR InfSectionName OPTIONAL)
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4703,10 +4998,10 @@ HKEY WINAPI SetupDiCreateDevRegKeyW(
         DWORD Index; /* Index used in the DriverKey name */
         DWORD rc;
         HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
-        HKEY hEnumKey = INVALID_HANDLE_VALUE;
-        HKEY hClassKey = INVALID_HANDLE_VALUE;
+        HKEY hEnumKey = NULL;
+        HKEY hClassKey = NULL;
         HKEY hDeviceKey = INVALID_HANDLE_VALUE;
-        HKEY hKey = INVALID_HANDLE_VALUE;
+        HKEY hKey = NULL;
         HKEY RootKey;
 
         if (Scope == DICS_FLAG_GLOBAL)
@@ -4811,7 +5106,7 @@ HKEY WINAPI SetupDiCreateDevRegKeyW(
                 if (Disposition == REG_CREATED_NEW_KEY)
                     break;
                 RegCloseKey(hKey);
-                hKey = INVALID_HANDLE_VALUE;
+                hKey = NULL;
                 Index++;
             }
             if (Index > 9999)
@@ -4847,13 +5142,13 @@ cleanup:
         HeapFree(GetProcessHeap(), 0, DriverKey);
         if (hHWProfileKey != INVALID_HANDLE_VALUE)
             RegCloseKey(hHWProfileKey);
-        if (hEnumKey != INVALID_HANDLE_VALUE)
+        if (hEnumKey != NULL)
             RegCloseKey(hEnumKey);
-        if (hClassKey != INVALID_HANDLE_VALUE)
+        if (hClassKey != NULL)
             RegCloseKey(hClassKey);
         if (hDeviceKey != INVALID_HANDLE_VALUE)
             RegCloseKey(hDeviceKey);
-        if (hKey != INVALID_HANDLE_VALUE && hKey != ret)
+        if (hKey != NULL && hKey != ret)
             RegCloseKey(hKey);
     }
 
@@ -4864,13 +5159,14 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenDevRegKey (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenDevRegKey(
-       HDEVINFO DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       DWORD Scope,
-       DWORD HwProfile,
-       DWORD KeyType,
-       REGSAM samDesired)
+HKEY WINAPI
+SetupDiOpenDevRegKey(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN REGSAM samDesired)
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4898,8 +5194,8 @@ HKEY WINAPI SetupDiOpenDevRegKey(
         DWORD dwRegType;
         DWORD rc;
         HKEY hHWProfileKey = INVALID_HANDLE_VALUE;
-        HKEY hEnumKey = INVALID_HANDLE_VALUE;
-        HKEY hKey = INVALID_HANDLE_VALUE;
+        HKEY hEnumKey = NULL;
+        HKEY hKey = NULL;
         HKEY RootKey;
 
         if (Scope == DICS_FLAG_GLOBAL)
@@ -4930,7 +5226,7 @@ HKEY WINAPI SetupDiOpenDevRegKey(
             KeyType == DIREG_DEV ? samDesired : KEY_QUERY_VALUE,
             &hKey);
         RegCloseKey(hEnumKey);
-        hEnumKey = INVALID_HANDLE_VALUE;
+        hEnumKey = NULL;
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
@@ -4967,7 +5263,7 @@ HKEY WINAPI SetupDiOpenDevRegKey(
             goto cleanup;
         }
         RegCloseKey(hKey);
-        hKey = INVALID_HANDLE_VALUE;
+        hKey = NULL;
         /* Need to open the driver key */
         rc = RegOpenKeyExW(
             RootKey,
@@ -4996,9 +5292,9 @@ HKEY WINAPI SetupDiOpenDevRegKey(
 cleanup:
         if (hHWProfileKey != INVALID_HANDLE_VALUE)
             RegCloseKey(hHWProfileKey);
-        if (hEnumKey != INVALID_HANDLE_VALUE)
+        if (hEnumKey != NULL)
             RegCloseKey(hEnumKey);
-        if (hKey != INVALID_HANDLE_VALUE && hKey != ret)
+        if (hKey != NULL && hKey != ret)
             RegCloseKey(hKey);
     }
 
@@ -5009,14 +5305,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDeviceInfoA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCreateDeviceInfoA(
-       HDEVINFO DeviceInfoSet,
-       PCSTR DeviceName,
-       CONST GUID *ClassGuid,
-       PCSTR DeviceDescription,
-       HWND hwndParent,
-       DWORD CreationFlags,
-       PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiCreateDeviceInfoA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCSTR DeviceName,
+    IN CONST GUID *ClassGuid,
+    IN PCSTR DeviceDescription OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD CreationFlags,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     LPWSTR DeviceNameW = NULL;
     LPWSTR DeviceDescriptionW = NULL;
@@ -5053,14 +5350,15 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(
 /***********************************************************************
  *             SetupDiCreateDeviceInfoW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCreateDeviceInfoW(
-       HDEVINFO DeviceInfoSet,
-       PCWSTR DeviceName,
-       CONST GUID *ClassGuid,
-       PCWSTR DeviceDescription,
-       HWND hwndParent,
-       DWORD CreationFlags,
-       PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiCreateDeviceInfoW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCWSTR DeviceName,
+    IN CONST GUID *ClassGuid,
+    IN PCWSTR DeviceDescription OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD CreationFlags,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -5557,6 +5855,47 @@ done:
     return Result;
 }
 
+static struct InfFileDetails *
+CreateInfFileDetails(
+    IN LPCWSTR InfFileName)
+{
+    struct InfFileDetails *details;
+    PWCHAR last;
+    DWORD Needed;
+
+    last = strrchrW(InfFileName, '\\');
+    Needed = FIELD_OFFSET(struct InfFileDetails, szData)
+        + strlenW(InfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
+    if (last != NULL)
+    Needed += (last - InfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
+
+    details = HeapAlloc(GetProcessHeap(), 0, Needed);
+    if (!details)
+    {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return NULL;
+    }
+
+    memset(details, 0, Needed);
+    if (last)
+    {
+        details->DirectoryName = details->szData;
+        details->FullInfFileName = &details->szData[last - InfFileName + 1];
+        strncpyW(details->DirectoryName, InfFileName, last - InfFileName);
+    }
+    else
+        details->FullInfFileName = details->szData;
+    strcpyW(details->FullInfFileName, InfFileName);
+    ReferenceInfFile(details);
+    details->hInf = SetupOpenInfFileW(InfFileName, NULL, INF_STYLE_WIN4, NULL);
+    if (details->hInf == INVALID_HANDLE_VALUE)
+    {
+        HeapFree(GetProcessHeap(), 0, details);
+        return NULL;
+    }
+    return details;
+}
+
 /***********************************************************************
  *             SetupDiBuildDriverInfoList (SETUPAPI.@)
  */
@@ -5721,23 +6060,9 @@ SetupDiBuildDriverInfoList(
                 strcpyW(pFullFilename, filename);
                 TRACE("Opening file %s\n", debugstr_w(FullInfFileName));
 
-                currentInfFileDetails = HeapAlloc(
-                    GetProcessHeap(),
-                    0,
-                    FIELD_OFFSET(struct InfFileDetails, FullInfFileName) + strlenW(FullInfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL));
+                currentInfFileDetails = CreateInfFileDetails(FullInfFileName);
                 if (!currentInfFileDetails)
                     continue;
-                memset(currentInfFileDetails, 0, sizeof(struct InfFileDetails));
-                strcpyW(currentInfFileDetails->FullInfFileName, FullInfFileName);
-
-                currentInfFileDetails->hInf = SetupOpenInfFileW(FullInfFileName, NULL, INF_STYLE_WIN4, NULL);
-                ReferenceInfFile(currentInfFileDetails);
-                if (currentInfFileDetails->hInf == INVALID_HANDLE_VALUE)
-                {
-                    HeapFree(GetProcessHeap(), 0, currentInfFileDetails);
-                    currentInfFileDetails = NULL;
-                    continue;
-                }
 
                 if (!GetVersionInformationFromInfFile(
                     currentInfFileDetails->hInf,
@@ -5746,8 +6071,7 @@ SetupDiBuildDriverInfoList(
                     &DriverDate,
                     &DriverVersion))
                 {
-                    SetupCloseInfFile(currentInfFileDetails->hInf);
-                    HeapFree(GetProcessHeap(), 0, currentInfFileDetails->hInf);
+                    DereferenceInfFile(currentInfFileDetails);
                     currentInfFileDetails = NULL;
                     continue;
                 }
@@ -5871,7 +6195,7 @@ SetupDiBuildDriverInfoList(
                                 DriverAlreadyAdded = FALSE;
                                 for (DriverRank = 0, currentId = (LPCWSTR)HardwareIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                 {
-                                    if (wcsicmp(DeviceId, currentId) == 0)
+                                    if (strcmpiW(DeviceId, currentId) == 0)
                                     {
                                         AddDriverToList(
                                             pDriverListHead,
@@ -5892,7 +6216,7 @@ SetupDiBuildDriverInfoList(
                                 {
                                     for (DriverRank = 0, currentId = (LPCWSTR)CompatibleIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                     {
-                                        if (wcsicmp(DeviceId, currentId) == 0)
+                                        if (strcmpiW(DeviceId, currentId) == 0)
                                         {
                                             AddDriverToList(
                                                 pDriverListHead,
@@ -5986,7 +6310,7 @@ SetupDiDeleteDeviceInfo(
 BOOL WINAPI
 SetupDiDestroyDriverInfoList(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN DWORD DriverType)
 {
     struct DeviceInfoSet *list;
@@ -6023,7 +6347,7 @@ SetupDiDestroyDriverInfoList(
             while (!IsListEmpty(&list->DriverListHead))
             {
                  ListEntry = RemoveHeadList(&list->DriverListHead);
-                 driverInfo = (struct DriverInfoElement *)ListEntry;
+                 driverInfo = CONTAINING_RECORD(ListEntry, struct DriverInfoElement, ListEntry);
                  DestroyDriverInfoElement(driverInfo);
             }
             InstallParams.Reserved = 0;
@@ -6043,7 +6367,7 @@ SetupDiDestroyDriverInfoList(
             while (!IsListEmpty(&deviceInfo->DriverListHead))
             {
                  ListEntry = RemoveHeadList(&deviceInfo->DriverListHead);
-                 driverInfo = (struct DriverInfoElement *)ListEntry;
+                 driverInfo = CONTAINING_RECORD(ListEntry, struct DriverInfoElement, ListEntry);
                  if ((PVOID)InstallParamsSet.Reserved == driverInfo)
                  {
                      InstallParamsSet.Reserved = 0;
@@ -6105,7 +6429,7 @@ SetupDiOpenDeviceInfoW(
     OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     struct DeviceInfoSet *list;
-    HKEY hEnumKey, hKey;
+    HKEY hEnumKey, hKey = NULL;
     DWORD rc;
     BOOL ret = FALSE;
 
@@ -6163,7 +6487,7 @@ SetupDiOpenDeviceInfoW(
             if (rc != ERROR_SUCCESS)
             {
                 SetLastError(rc);
-                return FALSE;
+                goto cleanup;
             }
             rc = RegOpenKeyExW(
                 hEnumKey,
@@ -6177,20 +6501,16 @@ SetupDiOpenDeviceInfoW(
                 if (rc == ERROR_FILE_NOT_FOUND)
                     rc = ERROR_NO_SUCH_DEVINST;
                 SetLastError(rc);
-                return FALSE;
+                goto cleanup;
             }
 
             /* FIXME: try to get ClassGUID from registry, instead of
              * sending GUID_NULL to CreateDeviceInfoElement
              */
             if (!CreateDeviceInfoElement(list, DeviceInstanceId, &GUID_NULL, &deviceInfo))
-            {
-                RegCloseKey(hKey);
-                return FALSE;
-            }
+                goto cleanup;
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
 
-            RegCloseKey(hKey);
             ret = TRUE;
         }
 
@@ -6202,6 +6522,9 @@ SetupDiOpenDeviceInfoW(
         }
     }
 
+cleanup:
+    if (hKey != NULL)
+        RegCloseKey(hKey);
     return ret;
 }
 
@@ -6321,7 +6644,7 @@ SetupDiEnumDriverInfoW(
             SetLastError(ERROR_NO_MORE_ITEMS);
         else
         {
-            struct DriverInfoElement *DrvInfo = (struct DriverInfoElement *)ItemList;
+            struct DriverInfoElement *DrvInfo = CONTAINING_RECORD(ItemList, struct DriverInfoElement, ListEntry);
 
             memcpy(
                 &DriverInfoData->DriverType,
@@ -6524,7 +6847,7 @@ SetupDiSetSelectedDevice(
 BOOL WINAPI
 SetupDiSetSelectedDriverA(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN OUT PSP_DRVINFO_DATA_A DriverInfoData OPTIONAL)
 {
     SP_DRVINFO_DATA_V1_W DriverInfoDataW;
@@ -6587,7 +6910,7 @@ SetupDiSetSelectedDriverA(
 BOOL WINAPI
 SetupDiSetSelectedDriverW(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN OUT PSP_DRVINFO_DATA_W DriverInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
@@ -6653,11 +6976,11 @@ SetupDiSetSelectedDriverW(
                 SetLastError(ERROR_INVALID_PARAMETER);
             else
             {
-                *pDriverInfo = (struct DriverInfoElement *)ItemList;
+                *pDriverInfo = CONTAINING_RECORD(ItemList, struct DriverInfoElement, ListEntry);
                 DriverInfoData->Reserved = (ULONG_PTR)ItemList;
                 ret = TRUE;
                 TRACE("Choosing driver whose rank is 0x%lx\n",
-                    ((struct DriverInfoElement *)ItemList)->DriverRank);
+                    (*pDriverInfo)->DriverRank);
                 if (DeviceInfoData)
                     memcpy(&DeviceInfoData->ClassGuid, &(*pDriverInfo)->ClassGuid, sizeof(GUID));
             }
@@ -6676,7 +6999,7 @@ SetupDiGetDriverInfoDetailA(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_A DriverInfoData,
-    OUT PSP_DRVINFO_DETAIL_DATA_A DriverInfoDetailData OPTIONAL,
+    IN OUT PSP_DRVINFO_DETAIL_DATA_A DriverInfoDetailData OPTIONAL,
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6877,7 +7200,7 @@ SetupDiGetDriverInfoDetailW(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_W DriverInfoData,
-    OUT PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailData OPTIONAL,
+    IN OUT PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailData OPTIONAL,
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6951,7 +7274,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = HardwareIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
-            TRACE("Adding %S to list\n", DeviceID);
+            TRACE("Adding %s to list\n", debugstr_w(DeviceID));
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -6968,7 +7291,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = CompatibleIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
-            TRACE("Adding %S to list\n", DeviceID);
+            TRACE("Adding %s to list\n", debugstr_w(DeviceID));
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -7001,7 +7324,7 @@ static DWORD
 GetCurrentHwProfile(
     IN HDEVINFO DeviceInfoSet)
 {
-    HKEY hKey = INVALID_HANDLE_VALUE;
+    HKEY hKey = NULL;
     DWORD dwRegType, dwLength;
     DWORD hwProfile;
     LONG rc;
@@ -7040,7 +7363,7 @@ GetCurrentHwProfile(
     ret = hwProfile;
 
 cleanup:
-    if (hKey != INVALID_HANDLE_VALUE)
+    if (hKey != NULL)
         RegCloseKey(hKey);
 
     return hwProfile;
@@ -7087,7 +7410,7 @@ static BOOL StopDevice(
 BOOL WINAPI
 SetupDiChangeState(
     IN HDEVINFO DeviceInfoSet,
-    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData)
 {
     PSP_PROPCHANGE_PARAMS PropChange;
     HKEY hKey = INVALID_HANDLE_VALUE;
@@ -7199,7 +7522,7 @@ cleanup:
 BOOL WINAPI
 SetupDiSelectBestCompatDrv(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     SP_DRVINFO_DATA_W drvInfoData;
     BOOL ret;
@@ -7235,7 +7558,7 @@ SetupDiSelectBestCompatDrv(
 BOOL WINAPI
 SetupDiInstallDriverFiles(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+    IN PSP_DEVINFO_DATA DeviceInfoData)
 {
     BOOL ret = FALSE;
 
@@ -7297,7 +7620,7 @@ SetupDiInstallDriverFiles(
         }
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
+            SPINST_FILES, NULL, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7307,7 +7630,7 @@ SetupDiInstallDriverFiles(
         lstrcatW(SectionName, DotCoInstallers);
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
+            SPINST_FILES, NULL, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7404,7 +7727,7 @@ SetupDiRegisterCoDeviceInstallers(
         }
         Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            DoAction, hKey, NULL, SP_COPY_NEWER,
+            DoAction, hKey, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             SetupDefaultQueueCallback, Context,
             DeviceInfoSet, DeviceInfoData);
         if (!Result)
@@ -7567,7 +7890,7 @@ cleanup:
     return ret;
 }
 
-BOOL
+static BOOL
 InfIsFromOEMLocation(
     IN PCWSTR FullName,
     OUT LPBOOL IsOEMLocation)
@@ -7612,7 +7935,7 @@ InfIsFromOEMLocation(
 BOOL WINAPI
 SetupDiInstallDevice(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData)
 {
     SP_DEVINSTALL_PARAMS_W InstallParams;
     struct DriverInfoElement *SelectedDriver;
@@ -7629,7 +7952,6 @@ SetupDiInstallDevice(
     LPWSTR lpGuidString = NULL, lpFullGuidString = NULL;
     BOOL RebootRequired = FALSE;
     HKEY hKey = INVALID_HANDLE_VALUE;
-    HKEY hClassKey = INVALID_HANDLE_VALUE;
     BOOL NeedtoCopyFile;
     LARGE_INTEGER fullVersion;
     LONG rc;
@@ -7712,7 +8034,7 @@ SetupDiInstallDevice(
     pSectionName = &SectionName[strlenW(SectionName)];
 
     /* Get information from [Version] section */
-    if (!SetupDiGetINFClassW(SelectedDriver->Details.InfFileName, &ClassGuid, ClassName, MAX_CLASS_NAME_LEN, &RequiredSize))
+    if (!SetupDiGetINFClassW(SelectedDriver->InfFileDetails->FullInfFileName, &ClassGuid, ClassName, MAX_CLASS_NAME_LEN, &RequiredSize))
         goto cleanup;
     /* Format ClassGuid to a string */
     if (UuidToStringW((UUID*)&ClassGuid, &lpGuidString) != RPC_S_OK)
@@ -7754,7 +8076,7 @@ SetupDiInstallDevice(
     *pSectionName = '\0';
     Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
         SelectedDriver->InfFileDetails->hInf, SectionName,
-        DoAction, hKey, NULL, SP_COPY_NEWER,
+        DoAction, hKey, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
         SetupDefaultQueueCallback, Context,
         DeviceInfoSet, DeviceInfoData);
     if (!Result)
@@ -7774,7 +8096,7 @@ SetupDiInstallDevice(
     TRACE("InfSectionExt   : '%s'\n", debugstr_w(&SectionName[strlenW(SelectedDriver->Details.SectionName)]));
     TRACE("MatchingDeviceId: '%s'\n", debugstr_w(SelectedDriver->MatchingId));
     TRACE("ProviderName    : '%s'\n", debugstr_w(SelectedDriver->Info.ProviderName));
-    swprintf(Buffer, L"%u-%u-%u", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
+    sprintfW(Buffer, L"%u-%u-%u", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
     rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
     if (rc == ERROR_SUCCESS)
         rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE_DATA, 0, REG_BINARY, (const BYTE *)&SelectedDriver->Info.DriverDate, sizeof(FILETIME));
@@ -7782,7 +8104,7 @@ SetupDiInstallDevice(
         rc = RegSetValueEx(hKey, REGSTR_VAL_DRVDESC, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.Description, (strlenW(SelectedDriver->Info.Description) + 1) * sizeof(WCHAR));
     if (rc == ERROR_SUCCESS)
     {
-        swprintf(Buffer, L"%u.%u.%u.%u", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
+        sprintfW(Buffer, L"%u.%u.%u.%u", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
         rc = RegSetValueEx(hKey, REGSTR_DRIVER_VERSION, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
     }
     if (rc == ERROR_SUCCESS)
@@ -7885,8 +8207,6 @@ SetupDiInstallDevice(
 
 cleanup:
     /* End of installation */
-    if (hClassKey != INVALID_HANDLE_VALUE)
-        RegCloseKey(hClassKey);
     if (hKey != INVALID_HANDLE_VALUE)
         RegCloseKey(hKey);
     if (lpGuidString)