Fix some whitespaces
[reactos.git] / reactos / dll / win32 / setupapi / devinst.c
index f6ea449..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
  *
  * 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"
  */
 
 #include "setupapi_private.h"
@@ -86,6 +86,15 @@ struct CoInstallerElement
     PVOID PrivateData;
 };
 
     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,
 static BOOL
 PropertyChangeHandler(
     IN HDEVINFO DeviceInfoSet,
@@ -93,7 +102,7 @@ PropertyChangeHandler(
     IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
     IN DWORD ClassInstallParamsSize);
 
     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 */
     NULL, /* DIF_SELECTDEVICE */
     NULL, /* DIF_INSTALLDEVICE */
     NULL, /* DIF_ASSIGNRESOURCES */
@@ -141,11 +150,12 @@ static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
 /***********************************************************************
  *              SetupDiBuildClassInfoList  (SETUPAPI.@)
  */
 /***********************************************************************
  *              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,
 {
     TRACE("\n");
     return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
@@ -156,13 +166,14 @@ BOOL WINAPI SetupDiBuildClassInfoList(
 /***********************************************************************
  *              SetupDiBuildClassInfoListExA  (SETUPAPI.@)
  */
 /***********************************************************************
  *              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;
 {
     LPWSTR MachineNameW = NULL;
     BOOL bResult;
@@ -188,27 +199,29 @@ BOOL WINAPI SetupDiBuildClassInfoListExA(
 /***********************************************************************
  *             SetupDiBuildClassInfoListExW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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];
 {
     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;
     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)
 
     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,
 
     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
                                             KEY_ENUMERATE_SUB_KEYS,
@@ -216,118 +229,113 @@ BOOL WINAPI SetupDiBuildClassInfoListExW(
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     for (dwIndex = 0; ; dwIndex++)
     {
 
     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)
 
     if (RequiredSize != NULL)
-       *RequiredSize = dwGuidListIndex;
+        *RequiredSize = dwGuidListIndex;
 
     if (ClassGuidListSize < 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.@)
  */
 }
 
 /***********************************************************************
  *             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,
 {
   return SetupDiClassGuidsFromNameExA(ClassName, ClassGuidList,
                                       ClassGuidListSize, RequiredSize,
@@ -338,10 +346,10 @@ BOOL WINAPI SetupDiClassGuidsFromNameA(
  *             SetupDiClassGuidsFromNameW  (SETUPAPI.@)
  */
 BOOL WINAPI SetupDiClassGuidsFromNameW(
  *             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,
 {
   return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
                                       ClassGuidListSize, RequiredSize,
@@ -351,13 +359,14 @@ BOOL WINAPI SetupDiClassGuidsFromNameW(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExA  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     LPWSTR ClassNameW = NULL;
     LPWSTR MachineNameW = NULL;
@@ -394,28 +403,30 @@ BOOL WINAPI SetupDiClassGuidsFromNameExA(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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];
 {
     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;
     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)
 
     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,
 
     hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
                                             KEY_ENUMERATE_SUB_KEYS,
@@ -423,96 +434,94 @@ BOOL WINAPI SetupDiClassGuidsFromNameExW(
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
                                             MachineName,
                                             Reserved);
     if (hClassesKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     for (dwIndex = 0; ; dwIndex++)
     {
 
     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)
 
     if (RequiredSize != NULL)
-       *RequiredSize = dwGuidListIndex;
+        *RequiredSize = dwGuidListIndex;
 
     if (ClassGuidListSize < 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.@)
  */
 }
 
 /***********************************************************************
  *              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,
 {
   return SetupDiClassNameFromGuidExA(ClassGuid, ClassName,
                                      ClassNameSize, RequiredSize,
@@ -522,11 +531,12 @@ BOOL WINAPI SetupDiClassNameFromGuidA(
 /***********************************************************************
  *              SetupDiClassNameFromGuidW  (SETUPAPI.@)
  */
 /***********************************************************************
  *              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,
 {
   return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
                                      ClassNameSize, RequiredSize,
@@ -536,13 +546,14 @@ BOOL WINAPI SetupDiClassNameFromGuidW(
 /***********************************************************************
  *              SetupDiClassNameFromGuidExA  (SETUPAPI.@)
  */
 /***********************************************************************
  *              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;
 {
     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,
     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,
     if (ret)
     {
         int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
@@ -567,17 +578,19 @@ BOOL WINAPI SetupDiClassNameFromGuidExA(
 /***********************************************************************
  *             SetupDiClassNameFromGuidExW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     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);
 
     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)
                                      MachineName,
                                      Reserved);
     if (hKey == INVALID_HANDLE_VALUE)
-    {
-       return FALSE;
-    }
+        goto cleanup;
 
     if (RequiredSize != NULL)
     {
 
     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,
     }
 
     dwLength = ClassNameSize * sizeof(WCHAR);
     rc = RegQueryValueExW(hKey,
-                        Class,
-                        NULL,
-                        NULL,
-                        (LPBYTE)ClassName,
-                        &dwLength);
+                          Class,
+                          NULL,
+                          NULL,
+                          (LPBYTE)ClassName,
+                          &dwLength);
     if (rc != ERROR_SUCCESS)
     {
     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 (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);
 }
 {
   return SetupDiCreateDeviceInfoListExW(ClassGuid, hwndParent, NULL, NULL);
 }
@@ -644,10 +657,11 @@ SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
  *             SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
  */
 HDEVINFO WINAPI
  *             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;
 {
     LPWSTR MachineNameW = NULL;
     HDEVINFO hDevInfo;
@@ -703,10 +717,11 @@ GetErrorCodeFromCrCode(const IN CONFIGRET cr)
  *             SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
  */
 HDEVINFO WINAPI
  *             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;
 {
   struct DeviceInfoSet *list;
   LPWSTR UNCServerName = NULL;
@@ -803,10 +818,11 @@ cleanup:
 /***********************************************************************
  *             SetupDiEnumDeviceInfo (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -850,12 +866,12 @@ BOOL WINAPI SetupDiEnumDeviceInfo(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallA(
  */
 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,
 {
     return SetupDiGetActualSectionToInstallExA(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -867,12 +883,12 @@ SetupDiGetActualSectionToInstallA(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallW(
  */
 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,
 {
     return SetupDiGetActualSectionToInstallExW(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -884,14 +900,14 @@ SetupDiGetActualSectionToInstallW(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallExA(
  */
 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;
 {
     LPWSTR InfSectionNameW = NULL;
     LPWSTR InfSectionWithExtW = NULL;
@@ -941,19 +957,304 @@ cleanup:
     return bResult;
 }
 
     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(
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -972,162 +1273,84 @@ SetupDiGetActualSectionToInstallExW(
     else
     {
         static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
     else
     {
         static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
+        static BYTE CurrentProductType = 0;
+        static WORD CurrentSuiteMask = 0;
         PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
         PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
-        LPCWSTR pExtensionPlatform, pExtensionArchitecture;
-        WCHAR SectionName[LINE_LEN + 1];
-        LONG lLineCount = -1;
+        struct GetSectionCallbackInfo CallbackInfo;
         DWORD dwFullLength;
         DWORD dwFullLength;
+        BYTE ProductType;
+        WORD SuiteMask;
 
         /* Fill platform info if needed */
         if (AlternatePlatformInfo)
 
         /* 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;
             }
             {
                 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];
             }
         }
 
                 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
             }
         }
 
-        if (RequiredSize != NULL)
-            *RequiredSize = dwFullLength + 1;
-
         ret = TRUE;
     }
 
         ret = TRUE;
     }
 
@@ -1140,11 +1363,12 @@ done:
 /***********************************************************************
  *             SetupDiGetClassDescriptionA  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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,
 {
   return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1154,11 +1378,12 @@ BOOL WINAPI SetupDiGetClassDescriptionA(
 /***********************************************************************
  *             SetupDiGetClassDescriptionW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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,
 {
   return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1168,13 +1393,14 @@ BOOL WINAPI SetupDiGetClassDescriptionW(
 /***********************************************************************
  *             SetupDiGetClassDescriptionExA  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PWCHAR ClassDescriptionW;
     LPWSTR MachineNameW = NULL;
@@ -1225,16 +1451,18 @@ end:
 /***********************************************************************
  *             SetupDiGetClassDescriptionExW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
     DWORD dwLength;
+    BOOL ret = FALSE;
 
     TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
         ClassDescriptionSize, RequiredSize, debugstr_w(MachineName), Reserved);
 
     TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
         ClassDescriptionSize, RequiredSize, debugstr_w(MachineName), Reserved);
@@ -1246,109 +1474,114 @@ BOOL WINAPI SetupDiGetClassDescriptionExW(
                                      Reserved);
     if (hKey == INVALID_HANDLE_VALUE)
     {
                                      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)
     {
     }
 
     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,
     }
 
     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.@)
  */
 }
 
 /***********************************************************************
  *             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.@)
  */
 }
 
 /***********************************************************************
  *             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.@)
  */
 }
 
 /***********************************************************************
  *             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;
 
 {
     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;
         }
         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;
         }
         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);
 
 end:
     HeapFree(GetProcessHeap(), 0, enumstrW);
@@ -1426,13 +1659,14 @@ CreateDeviceInterface(
     return TRUE;
 }
 
     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 */
     WCHAR KeyBuffer[MAX_PATH];
     WCHAR InstancePath[MAX_PATH];
     LPWSTR pEndOfInstancePath; /* Pointer into InstancePath buffer */
@@ -1449,13 +1683,15 @@ static LONG SETUP_CreateDevListFromEnumerator(
         if (rc == ERROR_NO_MORE_ITEMS)
             break;
         if (rc != ERROR_SUCCESS)
         if (rc == ERROR_NO_MORE_ITEMS)
             break;
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
         i++;
 
         /* Open device id sub key */
         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)
         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);
         strcpyW(InstancePath, Enumerator);
         strcatW(InstancePath, L"\\");
         strcatW(InstancePath, KeyBuffer);
@@ -1473,19 +1709,13 @@ static LONG SETUP_CreateDevListFromEnumerator(
             if (rc == ERROR_NO_MORE_ITEMS)
                 break;
             if (rc != ERROR_SUCCESS)
             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)
             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);
 
             *pEndOfInstancePath = '\0';
             strcatW(InstancePath, KeyBuffer);
 
@@ -1503,13 +1733,12 @@ static LONG SETUP_CreateDevListFromEnumerator(
             }
             else if (rc != ERROR_SUCCESS)
             {
             }
             else if (rc != ERROR_SUCCESS)
             {
-                RegCloseKey(hDeviceIdKey);
-                return rc;
+                goto cleanup;
             }
             else if (dwRegType != REG_SZ)
             {
             }
             else if (dwRegType != REG_SZ)
             {
-                RegCloseKey(hDeviceIdKey);
-                return ERROR_GEN_FAILURE;
+                rc = ERROR_GEN_FAILURE;
+                goto cleanup;
             }
             else
             {
             }
             else
             {
@@ -1528,25 +1757,32 @@ static LONG SETUP_CreateDevListFromEnumerator(
             /* Add the entry to the list */
             if (!CreateDeviceInfoElement(list, InstancePath, &KeyGuid, &deviceInfo))
             {
             /* 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);
         }
             }
             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;
     WCHAR KeyBuffer[MAX_PATH];
     DWORD i;
     DWORD dwLength;
@@ -1555,24 +1791,22 @@ static LONG SETUP_CreateDevList(
     if (class && IsEqualIID(class, &GUID_NULL))
         class = NULL;
 
     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)
     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);
         REGSTR_PATH_SYSTEMENUM,
         0,
         KEY_ENUMERATE_SUB_KEYS,
         &hEnumKey);
-    if (MachineName != NULL) RegCloseKey(HKLM);
     if (rc != ERROR_SUCCESS)
     if (rc != ERROR_SUCCESS)
-        return rc;
+        goto cleanup;
 
     /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
      * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
 
     /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
      * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
@@ -1586,12 +1820,9 @@ static LONG SETUP_CreateDevList(
             0,
             KEY_ENUMERATE_SUB_KEYS,
             &hEnumeratorKey);
             0,
             KEY_ENUMERATE_SUB_KEYS,
             &hEnumeratorKey);
-        RegCloseKey(hEnumKey);
         if (rc != ERROR_SUCCESS)
         if (rc != ERROR_SUCCESS)
-            return rc;
+            goto cleanup;
         rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
         rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
-        RegCloseKey(hEnumeratorKey);
-        return rc;
     }
     else
     {
     }
     else
     {
@@ -1603,48 +1834,49 @@ static LONG SETUP_CreateDevList(
             rc = RegEnumKeyExW(hEnumKey, i, KeyBuffer, &dwLength, NULL, NULL, NULL, NULL);
             if (rc == ERROR_NO_MORE_ITEMS)
                 break;
             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 */
             i++;
 
             /* Open sub key */
+            if (hEnumeratorKey != NULL)
+                RegCloseKey(hEnumeratorKey);
             rc = RegOpenKeyExW(hEnumKey, KeyBuffer, 0, KEY_ENUMERATE_SUB_KEYS, &hEnumeratorKey);
             if (rc != ERROR_SUCCESS)
             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);
 
             /* Call SETUP_CreateDevListFromEnumerator */
             rc = SETUP_CreateDevListFromEnumerator(list, class, KeyBuffer, hEnumeratorKey);
-            RegCloseKey(hEnumeratorKey);
             if (rc != ERROR_SUCCESS)
             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} */
 {
     HKEY hInterfaceKey;      /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID} */
     HKEY hDeviceInstanceKey; /* HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID}\##?#{InstancePath} */
@@ -1703,8 +1935,7 @@ static LONG SETUP_CreateInterfaceList(
             rc = ERROR_GEN_FAILURE;
             goto cleanup;
         }
             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)
         {
         InstancePath = HeapAlloc(GetProcessHeap(), 0, dwInstancePathLength + sizeof(WCHAR));
         if (!InstancePath)
         {
@@ -1805,8 +2036,7 @@ static LONG SETUP_CreateInterfaceList(
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
 
             /* Step 2. Create an interface list for this element */
             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)
             {
             pSymbolicLink = HeapAlloc(GetProcessHeap(), 0, (dwLength + 1) * sizeof(WCHAR));
             if (!pSymbolicLink)
             {
@@ -1859,51 +2089,51 @@ cleanup:
         RegCloseKey(hDeviceInstanceKey);
     if (hInterfaceKey != INVALID_HANDLE_VALUE)
         RegCloseKey(hInterfaceKey);
         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.@)
  */
     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;
 {
     HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
     struct DeviceInfoSet *list;
-    LPGUID pClassGuid;
+    CONST GUID *pClassGuid;
     LONG rc;
     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 */
 
     /* 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);
         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(
     }
     else
     {
          hDeviceInfo = SetupDiCreateDeviceInfoListExW(
-             flags & DIGCF_DEVICEINTERFACE ? NULL : class,
-             NULL, machine, NULL);
+             Flags & DIGCF_DEVICEINTERFACE ? NULL : ClassGuid,
+             NULL, MachineName, NULL);
          if (hDeviceInfo == INVALID_HANDLE_VALUE)
          if (hDeviceInfo == INVALID_HANDLE_VALUE)
-             return INVALID_HANDLE_VALUE;
+             goto cleanup;
          list = (struct DeviceInfoSet *)hDeviceInfo;
     }
 
          list = (struct DeviceInfoSet *)hDeviceInfo;
     }
 
@@ -1912,62 +2142,60 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
     else
         pClassGuid = &list->ClassGuid;
 
     else
         pClassGuid = &list->ClassGuid;
 
-    if (flags & DIGCF_PROFILE)
+    if (Flags & DIGCF_PROFILE)
         FIXME(": flag DIGCF_PROFILE ignored\n");
 
         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 (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);
         {
             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 (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
-            if (!deviceset)
-                SetupDiDestroyDeviceInfoList(hDeviceInfo);
-            return INVALID_HANDLE_VALUE;
+            goto cleanup;
         }
         }
-        return hDeviceInfo;
+        ret = hDeviceInfo;
     }
     else
     {
     }
     else
     {
-        rc = SETUP_CreateDevList(list, machine, (LPGUID)class, enumstr);
+        rc = SETUP_CreateDevList(list, MachineName, ClassGuid, Enumerator);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
         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.@)
  */
 
 }
 
 /***********************************************************************
  *             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;
 {
     LPWSTR Buffer = NULL;
     DWORD dwRegType, dwLength;
@@ -2009,10 +2237,11 @@ cleanup:
     return ret;
 }
 
     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;
 {
     struct ClassImageList *list;
     BOOL ret = FALSE;
@@ -2062,8 +2291,9 @@ cleanup:
 /***********************************************************************
  *             SetupDiGetClassImageList(SETUPAPI.@)
  */
 /***********************************************************************
  *             SetupDiGetClassImageList(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageList(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
+BOOL WINAPI
+SetupDiGetClassImageList(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
 {
     return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
 }
 {
     return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
 }
@@ -2071,10 +2301,11 @@ BOOL WINAPI SetupDiGetClassImageList(
 /***********************************************************************
  *             SetupDiGetClassImageListExA(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PWSTR MachineNameW = NULL;
     BOOL ret;
@@ -2097,10 +2328,11 @@ BOOL WINAPI SetupDiGetClassImageListExA(
 /***********************************************************************
  *             SetupDiGetClassImageListExW(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -2153,10 +2385,11 @@ cleanup:
 /***********************************************************************
  *             SetupDiLoadClassIcon(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -2267,12 +2500,13 @@ cleanup:
 /***********************************************************************
  *             SetupDiEnumDeviceInterfaces (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -2337,12 +2571,14 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(
     return ret;
 }
 
     return ret;
 }
 
-static VOID ReferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+ReferenceInfFile(struct InfFileDetails* infFile)
 {
     InterlockedIncrement(&infFile->References);
 }
 
 {
     InterlockedIncrement(&infFile->References);
 }
 
-static VOID DereferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+DereferenceInfFile(struct InfFileDetails* infFile)
 {
     if (InterlockedDecrement(&infFile->References) == 0)
     {
 {
     if (InterlockedDecrement(&infFile->References) == 0)
     {
@@ -2351,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);
 {
     DereferenceInfFile(driverInfo->InfFileDetails);
     HeapFree(GetProcessHeap(), 0, driverInfo->MatchingId);
@@ -2359,13 +2596,14 @@ static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
     return TRUE;
 }
 
     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;
 {
     PLIST_ENTRY ListEntry;
     struct DriverInfoElement *driverInfo;
@@ -2386,11 +2624,11 @@ static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
             return FALSE;
     }
     DestroyClassInstallParams(&deviceInfo->ClassInstallParams);
             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;
 {
     PLIST_ENTRY ListEntry;
     struct DeviceInfoElement *deviceInfo;
@@ -2406,21 +2644,22 @@ static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
         RegCloseKey(list->HKLM);
     CM_Disconnect_Machine(list->hMachine);
     DestroyClassInstallParams(&list->ClassInstallParams);
         RegCloseKey(list->HKLM);
     CM_Disconnect_Machine(list->hMachine);
     DestroyClassInstallParams(&list->ClassInstallParams);
-    HeapFree(GetProcessHeap(), 0, list);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, list);
 }
 
 /***********************************************************************
  *             SetupDiDestroyDeviceInfoList (SETUPAPI.@)
  */
 }
 
 /***********************************************************************
  *             SetupDiDestroyDeviceInfoList (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
+BOOL WINAPI
+SetupDiDestroyDeviceInfoList(
+    IN HDEVINFO DeviceInfoSet)
 {
     BOOL ret = FALSE;
 
 {
     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);
 
         if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
             ret = DestroyDeviceInfoSet(list);
@@ -2437,13 +2676,14 @@ BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailDataW = NULL;
     DWORD sizeW = 0, sizeA;
@@ -2507,13 +2747,14 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -2573,31 +2814,40 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     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);
 
         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(
 
     bResult = SetupDiGetDeviceRegistryPropertyW(
-        devinfo,
+        DeviceInfoSet,
         DeviceInfoData,
         Property,
         &RegType,
         DeviceInfoData,
         Property,
         &RegType,
@@ -2651,14 +2901,15 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     HKEY hEnumKey, hKey;
     DWORD rc;
@@ -2849,12 +3100,13 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 {
     FIXME("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
         Property, PropertyBuffer, PropertyBufferSize);
@@ -2865,12 +3117,13 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -2999,11 +3252,12 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiInstallClassA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 }
 {
     return SetupDiInstallClassExA(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3012,11 +3266,12 @@ BOOL WINAPI SetupDiInstallClassA(
 /***********************************************************************
  *             SetupDiInstallClassW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 }
 {
     return SetupDiInstallClassExW(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3025,14 +3280,15 @@ BOOL WINAPI SetupDiInstallClassW(
 /***********************************************************************
  *             SetupDiInstallClassExA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PWSTR InfFileNameW = NULL;
     BOOL Result;
@@ -3056,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;
 {
     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,
     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);
 
     }
 
     lstrcpyW(FullBuffer, REGSTR_PATH_CLASS_NT);
     lstrcatW(FullBuffer, Buffer);
 
-
     if (!SetupGetLineTextW(NULL,
     if (!SetupGetLineTextW(NULL,
-                              hInf,
-                              Version,
-                              Class,
-                              Buffer,
-                              MAX_PATH,
-                              &RequiredSize))
+                           hInf,
+                           Version,
+                           Class,
+                           Buffer,
+                           MAX_PATH,
+                           &RequiredSize))
     {
         RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
     {
         RegDeleteKeyW(HKEY_LOCAL_MACHINE, FullBuffer);
-        return INVALID_HANDLE_VALUE;
+        goto cleanup;
     }
 
     if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_LOCAL_MACHINE,
     }
 
     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,
     }
 
     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.@)
  */
 }
 
 
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -3240,7 +3503,7 @@ BOOL WINAPI SetupDiInstallClassExW(
                 SectionName,
                 SPINST_REGISTRY | SPINST_FILES | SPINST_BITREG | SPINST_INIFILES | SPINST_INI2REG,
                 hRootKey,
                 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,
                 !(Flags & DI_NOVCP) && (Flags & DI_FORCECOPY) ? SP_COPY_FORCE_IN_USE : 0, /* CopyFlags */
                 SetupDefaultQueueCallbackW,
                 callback_context,
@@ -3283,9 +3546,10 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenClassRegKey  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 {
     return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
                                      DIOCR_INSTALLER, NULL, NULL);
@@ -3295,12 +3559,13 @@ HKEY WINAPI SetupDiOpenClassRegKey(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExA  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PWSTR MachineNameW = NULL;
     HKEY hKey;
@@ -3327,12 +3592,13 @@ HKEY WINAPI SetupDiOpenClassRegKeyExA(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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 = NULL;
     LPWSTR lpFullGuidString = NULL;
 {
     LPWSTR lpGuidString = NULL;
     LPWSTR lpFullGuidString = NULL;
@@ -3421,13 +3687,12 @@ HKEY WINAPI SetupDiOpenClassRegKeyExW(
 
 cleanup:
     if (hClassKey != NULL && hClassKey != ret)
 
 cleanup:
     if (hClassKey != NULL && hClassKey != ret)
-        RegCloseKey(hClassesKey);
+        RegCloseKey(hClassKey);
     if (hClassesKey != NULL && hClassesKey != ret)
         RegCloseKey(hClassesKey);
     if (lpGuidString)
         RpcStringFreeW(&lpGuidString);
     if (hClassesKey != NULL && hClassesKey != ret)
         RegCloseKey(hClassesKey);
     if (lpGuidString)
         RpcStringFreeW(&lpGuidString);
-    if (lpFullGuidString)
-        HeapFree(GetProcessHeap(), 0, lpFullGuidString);
+    HeapFree(GetProcessHeap(), 0, lpFullGuidString);
 
     return ret;
 }
 
     return ret;
 }
@@ -3435,11 +3700,12 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenDeviceInterfaceW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 {
     FIXME("%p %s %08lx %p\n",
         DeviceInfoSet, debugstr_w(DevicePath), OpenFlags, DeviceInterfaceData);
@@ -3449,11 +3715,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceW(
 /***********************************************************************
  *             SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     LPWSTR DevicePathW = NULL;
     BOOL bResult;
@@ -3475,11 +3742,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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);
 {
     FIXME("%p %p %x %lu\n",DeviceInfoSet, DeviceInfoData,
           ClassInstallParams->InstallFunction, ClassInstallParamsSize);
@@ -3489,11 +3757,12 @@ BOOL WINAPI SetupDiSetClassInstallParamsA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -3558,11 +3827,12 @@ done:
     return ret;
 }
 
     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;
 {
     PSP_PROPCHANGE_PARAMS PropChangeParams = (PSP_PROPCHANGE_PARAMS)ClassInstallParams;
     BOOL ret = FALSE;
@@ -3619,9 +3889,9 @@ done:
 
 static DWORD
 GetFunctionPointer(
 
 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;
 {
     HMODULE hModule = NULL;
     LPSTR FunctionNameA = NULL;
@@ -3680,8 +3950,8 @@ cleanup:
 
 static DWORD
 FreeFunctionPointer(
 
 static DWORD
 FreeFunctionPointer(
-        IN HMODULE ModulePointer,
-        IN PVOID FunctionPointer)
+    IN HMODULE ModulePointer,
+    IN PVOID FunctionPointer)
 {
     if (ModulePointer == NULL)
         return ERROR_SUCCESS;
 {
     if (ModulePointer == NULL)
         return ERROR_SUCCESS;
@@ -3702,10 +3972,11 @@ IntSetupDiRegisterDeviceInfo(
 /***********************************************************************
  *             SetupDiCallClassInstaller (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -4038,12 +4309,12 @@ BOOL WINAPI SetupDiCallClassInstaller(
             while (!IsListEmpty(&ClassCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&ClassCoInstallersListHead);
             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);
             }
             while (!IsListEmpty(&DeviceCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&DeviceCoInstallersListHead);
-                HeapFree(GetProcessHeap(), 0, ListEntry);
+                HeapFree(GetProcessHeap(), 0, CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry));
             }
 
             ret = (rc == NO_ERROR);
             }
 
             ret = (rc == NO_ERROR);
@@ -4057,9 +4328,10 @@ BOOL WINAPI SetupDiCallClassInstaller(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListClass  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4086,9 +4358,10 @@ BOOL WINAPI SetupDiGetDeviceInfoListClass(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListDetailW  (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4125,10 +4398,11 @@ BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     SP_DEVINSTALL_PARAMS_W deviceInstallParamsW;
     BOOL ret = FALSE;
@@ -4167,10 +4441,11 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsA(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4205,7 +4480,7 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsW(
 
 static BOOL
 CheckDeviceInstallParameters(
 
 static BOOL
 CheckDeviceInstallParameters(
-       IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+    IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     DWORD SupportedFlags =
         DI_NOVCP |                            /* 0x00000008 */
 {
     DWORD SupportedFlags =
         DI_NOVCP |                            /* 0x00000008 */
@@ -4260,10 +4535,11 @@ CheckDeviceInstallParameters(
 /***********************************************************************
  *             SetupDiSetDeviceInstallParamsW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4299,12 +4575,13 @@ BOOL WINAPI SetupDiSetDeviceInstallParamsW(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdA(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PWSTR DeviceInstanceIdW = NULL;
     BOOL ret = FALSE;
@@ -4345,12 +4622,13 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdA(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdW(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 
 {
     BOOL ret = FALSE;
 
@@ -4394,13 +4672,14 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdW(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsA(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PROPSHEETHEADERW psh;
     BOOL ret = FALSE;
@@ -4432,9 +4711,10 @@ struct ClassDevPropertySheetsData
     DWORD NumberOfPages;
 };
 
     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;
 
 {
     struct ClassDevPropertySheetsData *PropPageData;
 
@@ -4453,13 +4733,14 @@ static BOOL WINAPI GetClassDevPropertySheetsCallback(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsW(SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4584,14 +4865,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     PCWSTR InfSectionNameW = NULL;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4618,9 +4900,9 @@ HKEY WINAPI SetupDiCreateDevRegKeyA(
 
 static HKEY
 OpenHardwareProfileKey(
 
 static HKEY
 OpenHardwareProfileKey(
-        IN HKEY HKLM,
-        IN DWORD HwProfile,
-        IN DWORD samDesired)
+    IN HKEY HKLM,
+    IN DWORD HwProfile,
+    IN DWORD samDesired)
 {
     HKEY hHWProfilesKey = NULL;
     HKEY hHWProfileKey = NULL;
 {
     HKEY hHWProfilesKey = NULL;
     HKEY hHWProfileKey = NULL;
@@ -4676,14 +4958,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4823,7 +5106,7 @@ HKEY WINAPI SetupDiCreateDevRegKeyW(
                 if (Disposition == REG_CREATED_NEW_KEY)
                     break;
                 RegCloseKey(hKey);
                 if (Disposition == REG_CREATED_NEW_KEY)
                     break;
                 RegCloseKey(hKey);
-                hKey = INVALID_HANDLE_VALUE;
+                hKey = NULL;
                 Index++;
             }
             if (Index > 9999)
                 Index++;
             }
             if (Index > 9999)
@@ -4876,13 +5159,14 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenDevRegKey (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -5021,14 +5305,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDeviceInfoA (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     LPWSTR DeviceNameW = NULL;
     LPWSTR DeviceDescriptionW = NULL;
@@ -5065,14 +5350,15 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(
 /***********************************************************************
  *             SetupDiCreateDeviceInfoW (SETUPAPI.@)
  */
 /***********************************************************************
  *             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;
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -5569,6 +5855,47 @@ done:
     return Result;
 }
 
     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.@)
  */
 /***********************************************************************
  *             SetupDiBuildDriverInfoList (SETUPAPI.@)
  */
@@ -5733,23 +6060,9 @@ SetupDiBuildDriverInfoList(
                 strcpyW(pFullFilename, filename);
                 TRACE("Opening file %s\n", debugstr_w(FullInfFileName));
 
                 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;
                 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,
 
                 if (!GetVersionInformationFromInfFile(
                     currentInfFileDetails->hInf,
@@ -5758,8 +6071,7 @@ SetupDiBuildDriverInfoList(
                     &DriverDate,
                     &DriverVersion))
                 {
                     &DriverDate,
                     &DriverVersion))
                 {
-                    SetupCloseInfFile(currentInfFileDetails->hInf);
-                    HeapFree(GetProcessHeap(), 0, currentInfFileDetails->hInf);
+                    DereferenceInfFile(currentInfFileDetails);
                     currentInfFileDetails = NULL;
                     continue;
                 }
                     currentInfFileDetails = NULL;
                     continue;
                 }
@@ -5883,7 +6195,7 @@ SetupDiBuildDriverInfoList(
                                 DriverAlreadyAdded = FALSE;
                                 for (DriverRank = 0, currentId = (LPCWSTR)HardwareIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                 {
                                 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,
                                     {
                                         AddDriverToList(
                                             pDriverListHead,
@@ -5904,7 +6216,7 @@ SetupDiBuildDriverInfoList(
                                 {
                                     for (DriverRank = 0, currentId = (LPCWSTR)CompatibleIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                     {
                                 {
                                     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,
                                         {
                                             AddDriverToList(
                                                 pDriverListHead,
@@ -5998,7 +6310,7 @@ SetupDiDeleteDeviceInfo(
 BOOL WINAPI
 SetupDiDestroyDriverInfoList(
     IN HDEVINFO DeviceInfoSet,
 BOOL WINAPI
 SetupDiDestroyDriverInfoList(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN DWORD DriverType)
 {
     struct DeviceInfoSet *list;
     IN DWORD DriverType)
 {
     struct DeviceInfoSet *list;
@@ -6117,7 +6429,7 @@ SetupDiOpenDeviceInfoW(
     OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     struct DeviceInfoSet *list;
     OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     struct DeviceInfoSet *list;
-    HKEY hEnumKey, hKey;
+    HKEY hEnumKey, hKey = NULL;
     DWORD rc;
     BOOL ret = FALSE;
 
     DWORD rc;
     BOOL ret = FALSE;
 
@@ -6175,7 +6487,7 @@ SetupDiOpenDeviceInfoW(
             if (rc != ERROR_SUCCESS)
             {
                 SetLastError(rc);
             if (rc != ERROR_SUCCESS)
             {
                 SetLastError(rc);
-                return FALSE;
+                goto cleanup;
             }
             rc = RegOpenKeyExW(
                 hEnumKey,
             }
             rc = RegOpenKeyExW(
                 hEnumKey,
@@ -6189,20 +6501,16 @@ SetupDiOpenDeviceInfoW(
                 if (rc == ERROR_FILE_NOT_FOUND)
                     rc = ERROR_NO_SUCH_DEVINST;
                 SetLastError(rc);
                 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))
             }
 
             /* 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);
 
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
 
-            RegCloseKey(hKey);
             ret = TRUE;
         }
 
             ret = TRUE;
         }
 
@@ -6214,6 +6522,9 @@ SetupDiOpenDeviceInfoW(
         }
     }
 
         }
     }
 
+cleanup:
+    if (hKey != NULL)
+        RegCloseKey(hKey);
     return ret;
 }
 
     return ret;
 }
 
@@ -6536,7 +6847,7 @@ SetupDiSetSelectedDevice(
 BOOL WINAPI
 SetupDiSetSelectedDriverA(
     IN HDEVINFO DeviceInfoSet,
 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;
     IN OUT PSP_DRVINFO_DATA_A DriverInfoData OPTIONAL)
 {
     SP_DRVINFO_DATA_V1_W DriverInfoDataW;
@@ -6599,7 +6910,7 @@ SetupDiSetSelectedDriverA(
 BOOL WINAPI
 SetupDiSetSelectedDriverW(
     IN HDEVINFO DeviceInfoSet,
 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;
     IN OUT PSP_DRVINFO_DATA_W DriverInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
@@ -6688,7 +6999,7 @@ SetupDiGetDriverInfoDetailA(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_A DriverInfoData,
     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)
 {
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6889,7 +7200,7 @@ SetupDiGetDriverInfoDetailW(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_W DriverInfoData,
     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)
 {
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6963,7 +7274,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = HardwareIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
         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;
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -6980,7 +7291,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = CompatibleIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
         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;
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -7099,7 +7410,7 @@ static BOOL StopDevice(
 BOOL WINAPI
 SetupDiChangeState(
     IN HDEVINFO DeviceInfoSet,
 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;
 {
     PSP_PROPCHANGE_PARAMS PropChange;
     HKEY hKey = INVALID_HANDLE_VALUE;
@@ -7211,7 +7522,7 @@ cleanup:
 BOOL WINAPI
 SetupDiSelectBestCompatDrv(
     IN HDEVINFO DeviceInfoSet,
 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;
 {
     SP_DRVINFO_DATA_W drvInfoData;
     BOOL ret;
@@ -7247,7 +7558,7 @@ SetupDiSelectBestCompatDrv(
 BOOL WINAPI
 SetupDiInstallDriverFiles(
     IN HDEVINFO DeviceInfoSet,
 BOOL WINAPI
 SetupDiInstallDriverFiles(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+    IN PSP_DEVINFO_DATA DeviceInfoData)
 {
     BOOL ret = FALSE;
 
 {
     BOOL ret = FALSE;
 
@@ -7309,7 +7620,7 @@ SetupDiInstallDriverFiles(
         }
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
         }
         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)
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7319,7 +7630,7 @@ SetupDiInstallDriverFiles(
         lstrcatW(SectionName, DotCoInstallers);
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
         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)
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7416,7 +7727,7 @@ SetupDiRegisterCoDeviceInstallers(
         }
         Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
         }
         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)
             SetupDefaultQueueCallback, Context,
             DeviceInfoSet, DeviceInfoData);
         if (!Result)
@@ -7579,7 +7890,7 @@ cleanup:
     return ret;
 }
 
     return ret;
 }
 
-BOOL
+static BOOL
 InfIsFromOEMLocation(
     IN PCWSTR FullName,
     OUT LPBOOL IsOEMLocation)
 InfIsFromOEMLocation(
     IN PCWSTR FullName,
     OUT LPBOOL IsOEMLocation)
@@ -7624,7 +7935,7 @@ InfIsFromOEMLocation(
 BOOL WINAPI
 SetupDiInstallDevice(
     IN HDEVINFO DeviceInfoSet,
 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;
 {
     SP_DEVINSTALL_PARAMS_W InstallParams;
     struct DriverInfoElement *SelectedDriver;
@@ -7723,7 +8034,7 @@ SetupDiInstallDevice(
     pSectionName = &SectionName[strlenW(SectionName)];
 
     /* Get information from [Version] section */
     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)
         goto cleanup;
     /* Format ClassGuid to a string */
     if (UuidToStringW((UUID*)&ClassGuid, &lpGuidString) != RPC_S_OK)
@@ -7765,7 +8076,7 @@ SetupDiInstallDevice(
     *pSectionName = '\0';
     Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
         SelectedDriver->InfFileDetails->hInf, SectionName,
     *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)
         SetupDefaultQueueCallback, Context,
         DeviceInfoSet, DeviceInfoData);
     if (!Result)
@@ -7785,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));
     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));
     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));
@@ -7793,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)
     {
         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)
         rc = RegSetValueEx(hKey, REGSTR_DRIVER_VERSION, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
     }
     if (rc == ERROR_SUCCESS)