Fix some whitespaces
[reactos.git] / reactos / dll / win32 / setupapi / devinst.c
index 46ad208..a0c94b5 100644 (file)
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include "setupapi_private.h"
@@ -86,6 +86,15 @@ struct CoInstallerElement
     PVOID PrivateData;
 };
 
+struct GetSectionCallbackInfo
+{
+    PSP_ALTPLATFORM_INFO PlatformInfo;
+    BYTE ProductType;
+    WORD SuiteMask;
+    WCHAR BestSection[LINE_LEN + 1];
+    DWORD BestScore1, BestScore2, BestScore3, BestScore4, BestScore5;
+};
+
 static BOOL
 PropertyChangeHandler(
     IN HDEVINFO DeviceInfoSet,
@@ -93,7 +102,7 @@ PropertyChangeHandler(
     IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
     IN DWORD ClassInstallParamsSize);
 
-static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
+static const UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
     NULL, /* DIF_SELECTDEVICE */
     NULL, /* DIF_INSTALLDEVICE */
     NULL, /* DIF_ASSIGNRESOURCES */
@@ -141,11 +150,12 @@ static UPDATE_CLASS_PARAM_HANDLER UpdateClassInstallParamHandlers[] = {
 /***********************************************************************
  *              SetupDiBuildClassInfoList  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoList(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiBuildClassInfoList(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize)
 {
     TRACE("\n");
     return SetupDiBuildClassInfoListExW(Flags, ClassGuidList,
@@ -156,13 +166,14 @@ BOOL WINAPI SetupDiBuildClassInfoList(
 /***********************************************************************
  *              SetupDiBuildClassInfoListExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoListExA(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiBuildClassInfoListExA(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR MachineNameW = NULL;
     BOOL bResult;
@@ -188,13 +199,14 @@ BOOL WINAPI SetupDiBuildClassInfoListExA(
 /***********************************************************************
  *             SetupDiBuildClassInfoListExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiBuildClassInfoListExW(
-        DWORD Flags,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiBuildClassInfoListExW(
+    IN DWORD Flags,
+    OUT LPGUID ClassGuidList OPTIONAL,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
     HKEY hClassesKey = INVALID_HANDLE_VALUE;
@@ -318,11 +330,12 @@ cleanup:
 /***********************************************************************
  *             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,
@@ -333,10 +346,10 @@ BOOL WINAPI SetupDiClassGuidsFromNameA(
  *             SetupDiClassGuidsFromNameW  (SETUPAPI.@)
  */
 BOOL WINAPI SetupDiClassGuidsFromNameW(
-        LPCWSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize)
+        IN PCWSTR ClassName,
+        OUT LPGUID ClassGuidList,
+        IN DWORD ClassGuidListSize,
+        OUT PDWORD RequiredSize)
 {
   return SetupDiClassGuidsFromNameExW(ClassName, ClassGuidList,
                                       ClassGuidListSize, RequiredSize,
@@ -346,13 +359,14 @@ BOOL WINAPI SetupDiClassGuidsFromNameW(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassGuidsFromNameExA(
-        LPCSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassGuidsFromNameExA(
+    IN PCSTR ClassName,
+    OUT LPGUID ClassGuidList,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR ClassNameW = NULL;
     LPWSTR MachineNameW = NULL;
@@ -389,13 +403,14 @@ BOOL WINAPI SetupDiClassGuidsFromNameExA(
 /***********************************************************************
  *             SetupDiClassGuidsFromNameExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassGuidsFromNameExW(
-        LPCWSTR ClassName,
-        LPGUID ClassGuidList,
-        DWORD ClassGuidListSize,
-        PDWORD RequiredSize,
-        LPCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassGuidsFromNameExW(
+    IN PCWSTR ClassName,
+    OUT LPGUID ClassGuidList,
+    IN DWORD ClassGuidListSize,
+    OUT PDWORD RequiredSize,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
     WCHAR szClassName[256];
@@ -501,11 +516,12 @@ cleanup:
 /***********************************************************************
  *              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,
@@ -515,11 +531,12 @@ BOOL WINAPI SetupDiClassNameFromGuidA(
 /***********************************************************************
  *              SetupDiClassNameFromGuidW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidW(
-        const GUID* ClassGuid,
-        PWSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiClassNameFromGuidW(
+    IN CONST GUID* ClassGuid,
+    OUT PWSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiClassNameFromGuidExW(ClassGuid, ClassName,
                                      ClassNameSize, RequiredSize,
@@ -529,13 +546,14 @@ BOOL WINAPI SetupDiClassNameFromGuidW(
 /***********************************************************************
  *              SetupDiClassNameFromGuidExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidExA(
-        const GUID* ClassGuid,
-        PSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize,
-        PCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassNameFromGuidExA(
+    IN CONST GUID* ClassGuid,
+    OUT PSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     WCHAR ClassNameW[MAX_CLASS_NAME_LEN];
     LPWSTR MachineNameW = NULL;
@@ -560,13 +578,14 @@ BOOL WINAPI SetupDiClassNameFromGuidExA(
 /***********************************************************************
  *             SetupDiClassNameFromGuidExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiClassNameFromGuidExW(
-        const GUID* ClassGuid,
-        PWSTR ClassName,
-        DWORD ClassNameSize,
-        PDWORD RequiredSize,
-        PCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiClassNameFromGuidExW(
+    IN CONST GUID* ClassGuid,
+    OUT PWSTR ClassName,
+    IN DWORD ClassNameSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HKEY hKey;
     DWORD dwLength;
@@ -627,8 +646,9 @@ cleanup:
  *             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);
 }
@@ -637,10 +657,11 @@ SetupDiCreateDeviceInfoList(const GUID *ClassGuid,
  *             SetupDiCreateDeviceInfoListExA (SETUPAPI.@)
  */
 HDEVINFO WINAPI
-SetupDiCreateDeviceInfoListExA(const GUID *ClassGuid,
-                              HWND hwndParent,
-                              PCSTR MachineName,
-                              PVOID Reserved)
+SetupDiCreateDeviceInfoListExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR MachineNameW = NULL;
     HDEVINFO hDevInfo;
@@ -696,10 +717,11 @@ GetErrorCodeFromCrCode(const IN CONFIGRET cr)
  *             SetupDiCreateDeviceInfoListExW (SETUPAPI.@)
  */
 HDEVINFO WINAPI
-SetupDiCreateDeviceInfoListExW(const GUID *ClassGuid,
-                              HWND hwndParent,
-                              PCWSTR MachineName,
-                              PVOID Reserved)
+SetupDiCreateDeviceInfoListExW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
   struct DeviceInfoSet *list;
   LPWSTR UNCServerName = NULL;
@@ -796,10 +818,11 @@ cleanup:
 /***********************************************************************
  *             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;
 
@@ -843,12 +866,12 @@ BOOL WINAPI SetupDiEnumDeviceInfo(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallA(
-        IN HINF InfHandle,
-        IN PCSTR InfSectionName,
-        OUT PSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PSTR *Extension OPTIONAL)
+    IN HINF InfHandle,
+    IN PCSTR InfSectionName,
+    OUT PSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSTR *Extension OPTIONAL)
 {
     return SetupDiGetActualSectionToInstallExA(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -860,12 +883,12 @@ SetupDiGetActualSectionToInstallA(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallW(
-        IN HINF InfHandle,
-        IN PCWSTR InfSectionName,
-        OUT PWSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PWSTR *Extension OPTIONAL)
+    IN HINF InfHandle,
+    IN PCWSTR InfSectionName,
+    OUT PWSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PWSTR *Extension OPTIONAL)
 {
     return SetupDiGetActualSectionToInstallExW(InfHandle, InfSectionName,
         NULL, InfSectionWithExt, InfSectionWithExtSize, RequiredSize,
@@ -877,14 +900,14 @@ SetupDiGetActualSectionToInstallW(
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallExA(
-        IN HINF InfHandle,
-        IN PCSTR InfSectionName,
-        IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
-        OUT PSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PSTR* Extension OPTIONAL,
-        IN PVOID Reserved)
+    IN HINF InfHandle,
+    IN PCSTR InfSectionName,
+    IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
+    OUT PSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSTR* Extension OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR InfSectionNameW = NULL;
     LPWSTR InfSectionWithExtW = NULL;
@@ -934,19 +957,304 @@ cleanup:
     return bResult;
 }
 
+/* Lower scores are best ones */
+static BOOL
+CheckSectionValid(
+    IN LPCWSTR SectionName,
+    IN PSP_ALTPLATFORM_INFO PlatformInfo,
+    IN BYTE ProductType,
+    IN WORD SuiteMask,
+    OUT PDWORD ScorePlatform,
+    OUT PDWORD ScoreMajorVersion,
+    OUT PDWORD ScoreMinorVersion,
+    OUT PDWORD ScoreProductType,
+    OUT PDWORD ScoreSuiteMask)
+{
+    LPWSTR Section = NULL;
+    LPCWSTR pExtensionPlatform, pExtensionArchitecture;
+    LPWSTR Fields[6];
+    DWORD i;
+    BOOL ret = FALSE;
+
+    TRACE("%s %p 0x%x 0x%x\n",
+        debugstr_w(SectionName), PlatformInfo, ProductType, SuiteMask);
+
+    static const WCHAR ExtensionPlatformNone[]  = {'.',0};
+    static const WCHAR ExtensionPlatformNT[]  = {'.','N','T',0};
+    static const WCHAR ExtensionPlatformWindows[]  = {'.','W','i','n',0};
+
+    static const WCHAR ExtensionArchitectureNone[]  = {0};
+    static const WCHAR ExtensionArchitecturealpha[]  = {'a','l','p','h','a',0};
+    static const WCHAR ExtensionArchitectureamd64[]  = {'a','m','d','6','4',0};
+    static const WCHAR ExtensionArchitectureia64[]  = {'i','a','6','4',0};
+    static const WCHAR ExtensionArchitecturemips[]  = {'m','i','p','s',0};
+    static const WCHAR ExtensionArchitectureppc[]  = {'p','p','c',0};
+    static const WCHAR ExtensionArchitecturex86[]  = {'x','8','6',0};
+
+    *ScorePlatform = *ScoreMajorVersion = *ScoreMinorVersion = *ScoreProductType = *ScoreSuiteMask = 0;
+
+    Section = DuplicateString(SectionName);
+    if (!Section)
+    {
+        TRACE("DuplicateString() failed\n");
+        goto cleanup;
+    }
+
+    /* Set various extensions values */
+    switch (PlatformInfo->Platform)
+    {
+        case VER_PLATFORM_WIN32_WINDOWS:
+            pExtensionPlatform = ExtensionPlatformWindows;
+            break;
+        case VER_PLATFORM_WIN32_NT:
+            pExtensionPlatform = ExtensionPlatformNT;
+            break;
+        default:
+            ERR("Unkown platform 0x%lx\n", PlatformInfo->Platform);
+            pExtensionPlatform = ExtensionPlatformNone;
+            break;
+    }
+    switch (PlatformInfo->ProcessorArchitecture)
+    {
+        case PROCESSOR_ARCHITECTURE_ALPHA:
+            pExtensionArchitecture = ExtensionArchitecturealpha;
+            break;
+        case PROCESSOR_ARCHITECTURE_AMD64:
+            pExtensionArchitecture = ExtensionArchitectureamd64;
+            break;
+        case PROCESSOR_ARCHITECTURE_IA64:
+            pExtensionArchitecture = ExtensionArchitectureia64;
+            break;
+        case PROCESSOR_ARCHITECTURE_INTEL:
+            pExtensionArchitecture = ExtensionArchitecturex86;
+            break;
+        case PROCESSOR_ARCHITECTURE_MIPS:
+            pExtensionArchitecture = ExtensionArchitecturemips;
+            break;
+        case PROCESSOR_ARCHITECTURE_PPC:
+            pExtensionArchitecture = ExtensionArchitectureppc;
+            break;
+        default:
+            ERR("Unknown processor architecture 0x%x\n", PlatformInfo->ProcessorArchitecture);
+        case PROCESSOR_ARCHITECTURE_UNKNOWN:
+            pExtensionArchitecture = ExtensionArchitectureNone;
+            break;
+    }
+
+    /*
+     * Field[0] Platform
+     * Field[1] Architecture
+     * Field[2] Major version
+     * Field[3] Minor version
+     * Field[4] Product type
+     * Field[5] Suite mask
+     * Remark: lastests fields may be NULL if the information is not provided
+     */
+    Fields[0] = strchrW(Section, '.');
+    if (Fields[0] == NULL)
+    {
+        TRACE("No extension found\n");
+        *ScorePlatform = *ScoreMajorVersion = *ScoreMinorVersion = *ScoreProductType = *ScoreSuiteMask = ULONG_MAX;
+        ret = TRUE;
+        goto cleanup;
+    }
+    Fields[1] = Fields[0] + 1;
+    Fields[2] = Fields[3] = Fields[4] = Fields[5] = NULL;
+    for (i = 2; Fields[i - 1] != NULL && i < 6; i++)
+    {
+        Fields[i] = wcschr(Fields[i - 1], '.');
+        if (Fields[i])
+        {
+            Fields[i]++;
+            *(Fields[i] - 1) = L'\0';
+        }
+    }
+    /* Take care of first 2 fields */
+    if (strncmpiW(Fields[0], ExtensionPlatformWindows, strlenW(ExtensionPlatformWindows)) == 0)
+    {
+        if (PlatformInfo->Platform != VER_PLATFORM_WIN32_WINDOWS)
+        {
+            TRACE("Mismatch on platform field\n");
+            goto cleanup;
+        }
+        Fields[1] += wcslen(ExtensionPlatformWindows) - 1;
+    }
+    else if (strncmpiW(Fields[0], ExtensionPlatformNT, strlenW(ExtensionPlatformNT)) == 0)
+    {
+        if (PlatformInfo->Platform != VER_PLATFORM_WIN32_NT)
+        {
+            TRACE("Mismatch on platform field\n");
+            goto cleanup;
+        }
+        Fields[1] += wcslen(ExtensionPlatformNT) - 1;
+    }
+    else
+    {
+        /* No platform specified */
+        *ScorePlatform |= 0x02;
+    }
+    if (strcmpiW(Fields[1], ExtensionArchitectureNone) == 0)
+    {
+        /* No architecture specified */
+        *ScorePlatform |= 0x01;
+    }
+    else if (strcmpiW(Fields[1], pExtensionArchitecture) != 0)
+    {
+        TRACE("Mismatch on architecture field ('%s' and '%s')\n",
+            debugstr_w(Fields[1]), debugstr_w(pExtensionArchitecture));
+        goto cleanup;
+    }
+
+    /* Check if informations are matching */
+    if (Fields[2] && *Fields[2])
+    {
+        DWORD MajorVersion, MinorVersion = 0;
+        MajorVersion = strtoulW(Fields[2], NULL, 0);
+        if ((MajorVersion == 0 || MajorVersion == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong MajorVersion ('%s')\n", debugstr_w(Fields[2]));
+            goto cleanup;
+        }
+        if (Fields[3] && *Fields[3])
+        {
+            MinorVersion = strtoulW(Fields[3], NULL, 0);
+            if ((MinorVersion == 0 || MinorVersion == ULONG_MAX) &&
+                (errno == ERANGE || errno == EINVAL))
+            {
+                TRACE("Wrong MinorVersion ('%s')\n", debugstr_w(Fields[3]));
+                goto cleanup;
+            }
+        }
+        if (PlatformInfo->MajorVersion < MajorVersion ||
+            (PlatformInfo->MajorVersion == MajorVersion && PlatformInfo->MinorVersion < MinorVersion))
+        {
+            TRACE("Mismatch on version field (%lu.%lu and %lu.%lu)\n",
+                MajorVersion, MinorVersion, PlatformInfo->MajorVersion, PlatformInfo->MinorVersion);
+            goto cleanup;
+        }
+        *ScoreMajorVersion = MajorVersion - PlatformInfo->MajorVersion;
+        if (MajorVersion == PlatformInfo->MajorVersion)
+            *ScoreMinorVersion = MinorVersion - PlatformInfo->MinorVersion;
+        else
+            *ScoreMinorVersion = MinorVersion;
+    }
+    else if (Fields[3] && *Fields[3])
+    {
+        TRACE("Minor version found without major version\n");
+        goto cleanup;
+    }
+    else
+    {
+        *ScoreMajorVersion = PlatformInfo->MajorVersion;
+        *ScoreMinorVersion = PlatformInfo->MinorVersion;
+    }
+
+    if (Fields[4] && *Fields[4])
+    {
+        DWORD CurrentProductType;
+        CurrentProductType = strtoulW(Fields[4], NULL, 0);
+        if ((CurrentProductType == 0 || CurrentProductType == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong Product type ('%s')\n", debugstr_w(Fields[4]));
+            goto cleanup;
+        }
+        if (CurrentProductType != ProductType)
+        {
+            TRACE("Mismatch on product type (0x%08lx and 0x%08x)\n",
+                CurrentProductType, ProductType);
+            goto cleanup;
+        }
+    }
+    else
+        *ScoreProductType = 1;
+
+    if (Fields[5] && *Fields[5])
+    {
+        DWORD CurrentSuiteMask;
+        CurrentSuiteMask = strtoulW(Fields[5], NULL, 0);
+        if ((CurrentSuiteMask == 0 || CurrentSuiteMask == ULONG_MAX) &&
+            (errno == ERANGE || errno == EINVAL))
+        {
+            TRACE("Wrong Suite mask ('%s')\n", debugstr_w(Fields[5]));
+            goto cleanup;
+        }
+        if ((CurrentSuiteMask & ~SuiteMask) != 0)
+        {
+            TRACE("Mismatch on suite mask (0x%08lx and 0x%08x)\n",
+                CurrentSuiteMask, SuiteMask);
+            goto cleanup;
+        }
+        *ScoreSuiteMask = SuiteMask & ~CurrentSuiteMask;
+    }
+    else
+        *ScoreSuiteMask = SuiteMask;
+
+    ret = TRUE;
+
+cleanup:
+    MyFree(Section);
+    return ret;
+}
+
+static BOOL
+GetSectionCallback(
+    IN LPCWSTR SectionName,
+    IN PVOID Context)
+{
+    struct GetSectionCallbackInfo *info = Context;
+    DWORD Score1, Score2, Score3, Score4, Score5;
+    BOOL ret;
+
+    ret = CheckSectionValid(
+        SectionName,
+        info->PlatformInfo,
+        info->ProductType,
+        info->SuiteMask,
+        &Score1, &Score2, &Score3, &Score4, &Score5);
+    if (!ret)
+    {
+        TRACE("Section %s not compatible\n", debugstr_w(SectionName));
+        return TRUE;
+    }
+    if (Score1 > info->BestScore1) goto done;
+    if (Score1 < info->BestScore1) goto bettersection;
+    if (Score2 > info->BestScore2) goto done;
+    if (Score2 < info->BestScore2) goto bettersection;
+    if (Score3 > info->BestScore3) goto done;
+    if (Score3 < info->BestScore3) goto bettersection;
+    if (Score4 > info->BestScore4) goto done;
+    if (Score4 < info->BestScore4) goto bettersection;
+    if (Score5 > info->BestScore5) goto done;
+    if (Score5 < info->BestScore5) goto bettersection;
+    goto done;
+
+bettersection:
+    strcpyW(info->BestSection, SectionName);
+    info->BestScore1 = Score1;
+    info->BestScore2 = Score2;
+    info->BestScore3 = Score3;
+    info->BestScore4 = Score4;
+    info->BestScore5 = Score5;
+
+done:
+    return TRUE;
+}
+
 /***********************************************************************
  *             SetupDiGetActualSectionToInstallExW (SETUPAPI.@)
  */
 BOOL WINAPI
 SetupDiGetActualSectionToInstallExW(
-        IN HINF InfHandle,
-        IN PCWSTR InfSectionName,
-        IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
-        OUT PWSTR InfSectionWithExt OPTIONAL,
-        IN DWORD InfSectionWithExtSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        OUT PWSTR* Extension OPTIONAL,
-        IN PVOID Reserved)
+    IN HINF InfHandle,
+    IN PCWSTR InfSectionName,
+    IN PSP_ALTPLATFORM_INFO AlternatePlatformInfo OPTIONAL,
+    OUT PWSTR InfSectionWithExt OPTIONAL,
+    IN DWORD InfSectionWithExtSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PWSTR* Extension OPTIONAL,
+    IN PVOID Reserved)
 {
     BOOL ret = FALSE;
 
@@ -965,162 +1273,84 @@ SetupDiGetActualSectionToInstallExW(
     else
     {
         static SP_ALTPLATFORM_INFO CurrentPlatform = { 0, };
+        static BYTE CurrentProductType = 0;
+        static WORD CurrentSuiteMask = 0;
         PSP_ALTPLATFORM_INFO pPlatformInfo = &CurrentPlatform;
-        LPCWSTR pExtensionPlatform, pExtensionArchitecture;
-        WCHAR SectionName[LINE_LEN + 1];
-        LONG lLineCount = -1;
+        struct GetSectionCallbackInfo CallbackInfo;
         DWORD dwFullLength;
+        BYTE ProductType;
+        WORD SuiteMask;
 
         /* Fill platform info if needed */
         if (AlternatePlatformInfo)
-            pPlatformInfo = AlternatePlatformInfo;
-        else if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
         {
-            /* That's the first time we go here. We need to fill in the structure */
-            OSVERSIONINFO VersionInfo;
-            SYSTEM_INFO SystemInfo;
-            VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-            ret = GetVersionEx(&VersionInfo);
-            if (!ret)
-                goto done;
-            GetSystemInfo(&SystemInfo);
-            CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
-            CurrentPlatform.Platform = VersionInfo.dwPlatformId;
-            CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
-            CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
-            CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
-            CurrentPlatform.Reserved = 0;
-        }
-
-static const WCHAR ExtensionPlatformNone[]  = {'.',0};
-static const WCHAR ExtensionPlatformNT[]  = {'.','N','T',0};
-static const WCHAR ExtensionPlatformWindows[]  = {'.','W','i','n',0};
-
-static const WCHAR ExtensionArchitectureNone[]  = {0};
-static const WCHAR ExtensionArchitecturealpha[]  = {'a','l','p','h','a',0};
-static const WCHAR ExtensionArchitectureamd64[]  = {'a','m','d','6','4',0};
-static const WCHAR ExtensionArchitectureia64[]  = {'i','a','6','4',0};
-static const WCHAR ExtensionArchitecturemips[]  = {'m','i','p','s',0};
-static const WCHAR ExtensionArchitectureppc[]  = {'p','p','c',0};
-static const WCHAR ExtensionArchitecturex86[]  = {'x','8','6',0};
-
-        /* Set various extensions values */
-        switch (pPlatformInfo->Platform)
-        {
-            case VER_PLATFORM_WIN32_WINDOWS:
-                pExtensionPlatform = ExtensionPlatformWindows;
-                break;
-            case VER_PLATFORM_WIN32_NT:
-                pExtensionPlatform = ExtensionPlatformNT;
-                break;
-            default:
-                ERR("Unkown platform 0x%lx\n", pPlatformInfo->Platform);
-                pExtensionPlatform = ExtensionPlatformNone;
-                break;
+            pPlatformInfo = AlternatePlatformInfo;
+            ProductType = 0;
+            SuiteMask = 0;
         }
-        switch (pPlatformInfo->ProcessorArchitecture)
+        else
         {
-            case PROCESSOR_ARCHITECTURE_ALPHA:
-                pExtensionArchitecture = ExtensionArchitecturealpha;
-                break;
-            case PROCESSOR_ARCHITECTURE_AMD64:
-                pExtensionArchitecture = ExtensionArchitectureamd64;
-                break;
-            case PROCESSOR_ARCHITECTURE_IA64:
-                pExtensionArchitecture = ExtensionArchitectureia64;
-                break;
-            case PROCESSOR_ARCHITECTURE_INTEL:
-                pExtensionArchitecture = ExtensionArchitecturex86;
-                break;
-            case PROCESSOR_ARCHITECTURE_MIPS:
-                pExtensionArchitecture = ExtensionArchitecturemips;
-                break;
-            case PROCESSOR_ARCHITECTURE_PPC:
-                pExtensionArchitecture = ExtensionArchitectureppc;
-                break;
-            default:
-                ERR("Unknown processor architecture 0x%x\n", pPlatformInfo->ProcessorArchitecture);
-            case PROCESSOR_ARCHITECTURE_UNKNOWN:
-                pExtensionArchitecture = ExtensionArchitectureNone;
-                break;
+            if (CurrentPlatform.cbSize != sizeof(SP_ALTPLATFORM_INFO))
+            {
+                /* That's the first time we go here. We need to fill in the structure */
+                OSVERSIONINFOEX VersionInfo;
+                SYSTEM_INFO SystemInfo;
+                VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+                ret = GetVersionEx((POSVERSIONINFO)&VersionInfo);
+                if (!ret)
+                    goto done;
+                GetSystemInfo(&SystemInfo);
+                CurrentPlatform.cbSize = sizeof(SP_ALTPLATFORM_INFO);
+                CurrentPlatform.Platform = VersionInfo.dwPlatformId;
+                CurrentPlatform.MajorVersion = VersionInfo.dwMajorVersion;
+                CurrentPlatform.MinorVersion = VersionInfo.dwMinorVersion;
+                CurrentPlatform.ProcessorArchitecture = SystemInfo.wProcessorArchitecture;
+                CurrentPlatform.Reserved = 0;
+                CurrentProductType = VersionInfo.wProductType;
+                CurrentSuiteMask = VersionInfo.wSuiteMask;
+            }
+            ProductType = CurrentProductType;
+            SuiteMask = CurrentSuiteMask;
+        }
+
+        CallbackInfo.PlatformInfo = pPlatformInfo;
+        CallbackInfo.ProductType = ProductType;
+        CallbackInfo.SuiteMask = SuiteMask;
+        CallbackInfo.BestScore1 = ULONG_MAX;
+        CallbackInfo.BestScore2 = ULONG_MAX;
+        CallbackInfo.BestScore3 = ULONG_MAX;
+        CallbackInfo.BestScore4 = ULONG_MAX;
+        CallbackInfo.BestScore5 = ULONG_MAX;
+        strcpyW(CallbackInfo.BestSection, InfSectionName);
+        if (!EnumerateSectionsStartingWith(
+            InfHandle,
+            InfSectionName,
+            GetSectionCallback,
+            &CallbackInfo))
+        {
+            SetLastError(ERROR_GEN_FAILURE);
+            goto done;
         }
 
-static const WCHAR FormatPlatformArchitectureMajorMinor[]  = {'%','s','%','s','%','s','.','%','l','u','.','%','l','u',0};
-static const WCHAR FormatPlatformMajorMinor[]  = {'%','s','%','s','.','%','l','u','.','%','l','u',0};
-static const WCHAR FormatPlatformArchitectureMajor[]  = {'%','s','%','s','%','s','.','%','l','u',0};
-static const WCHAR FormatPlatformMajor[]  = {'%','s','%','s','.','%','l','u',0};
-static const WCHAR FormatPlatformArchitecture[]  = {'%','s','%','s','%','s',0};
-static const WCHAR FormatPlatform[]  = {'%','s','%','s',0};
-static const WCHAR FormatNone[]  = {'%','s',0};
-
-        SectionName[LINE_LEN] = UNICODE_NULL;
-
-        /* Test with platform.architecture.major.minor extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajorMinor, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.major.minor extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformMajorMinor, InfSectionName,
-            pExtensionPlatform, pPlatformInfo->MajorVersion, pPlatformInfo->MinorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.architecture.major extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitectureMajor, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture, pPlatformInfo->MajorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.major extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformMajor, InfSectionName,
-            pExtensionPlatform, pPlatformInfo->MajorVersion);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform.architecture extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatformArchitecture, InfSectionName,
-            pExtensionPlatform, pExtensionArchitecture);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test with platform extension */
-        snprintfW(SectionName, LINE_LEN, FormatPlatform, InfSectionName,
-            pExtensionPlatform);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* Test without extension */
-        snprintfW(SectionName, LINE_LEN, FormatNone, InfSectionName);
-        lLineCount = SetupGetLineCountW(InfHandle, SectionName);
-        if (lLineCount != -1) goto sectionfound;
-
-        /* No appropriate section found */
-        SetLastError(ERROR_INVALID_PARAMETER);
-        goto done;
+        dwFullLength = lstrlenW(CallbackInfo.BestSection);
+        if (RequiredSize != NULL)
+            *RequiredSize = dwFullLength + 1;
 
-sectionfound:
-        dwFullLength = lstrlenW(SectionName);
-        if (InfSectionWithExt != NULL && InfSectionWithExtSize != 0)
+        if (InfSectionWithExtSize > 0)
         {
-            if (InfSectionWithExtSize < (dwFullLength + 1))
+            if (InfSectionWithExtSize < dwFullLength + 1)
             {
                 SetLastError(ERROR_INSUFFICIENT_BUFFER);
                 goto done;
             }
-
-            lstrcpyW(InfSectionWithExt, SectionName);
-            if (Extension != NULL)
+            strcpyW(InfSectionWithExt, CallbackInfo.BestSection);
+            if (Extension)
             {
-                DWORD dwLength = lstrlenW(SectionName);
+                DWORD dwLength = lstrlenW(InfSectionName);
                 *Extension = (dwLength == dwFullLength) ? NULL : &InfSectionWithExt[dwLength];
             }
         }
 
-        if (RequiredSize != NULL)
-            *RequiredSize = dwFullLength + 1;
-
         ret = TRUE;
     }
 
@@ -1133,11 +1363,12 @@ done:
 /***********************************************************************
  *             SetupDiGetClassDescriptionA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionA(
-        const GUID* ClassGuid,
-        PSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiGetClassDescriptionA(
+    IN CONST GUID *ClassGuid,
+    OUT PSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiGetClassDescriptionExA(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1147,11 +1378,12 @@ BOOL WINAPI SetupDiGetClassDescriptionA(
 /***********************************************************************
  *             SetupDiGetClassDescriptionW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionW(
-        const GUID* ClassGuid,
-        PWSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize)
+BOOL WINAPI
+SetupDiGetClassDescriptionW(
+    IN CONST GUID *ClassGuid,
+    OUT PWSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
   return SetupDiGetClassDescriptionExW(ClassGuid, ClassDescription,
                                        ClassDescriptionSize,
@@ -1161,13 +1393,14 @@ BOOL WINAPI SetupDiGetClassDescriptionW(
 /***********************************************************************
  *             SetupDiGetClassDescriptionExA  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionExA(
-        const GUID* ClassGuid,
-        PSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize,
-        PCSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassDescriptionExA(
+    IN CONST GUID *ClassGuid,
+    OUT PSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWCHAR ClassDescriptionW;
     LPWSTR MachineNameW = NULL;
@@ -1218,13 +1451,14 @@ end:
 /***********************************************************************
  *             SetupDiGetClassDescriptionExW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDescriptionExW(
-        const GUID* ClassGuid,
-        PWSTR ClassDescription,
-        DWORD ClassDescriptionSize,
-        PDWORD RequiredSize,
-        PCWSTR MachineName,
-        PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassDescriptionExW(
+    IN CONST GUID *ClassGuid,
+    OUT PWSTR ClassDescription,
+    IN DWORD ClassDescriptionSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HKEY hKey = INVALID_HANDLE_VALUE;
     DWORD dwLength;
@@ -1283,68 +1517,71 @@ cleanup:
 /***********************************************************************
  *             SetupDiGetClassDevsA (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsA(
-       CONST GUID *class,
-       LPCSTR enumstr,
-       HWND parent,
-       DWORD flags)
+HDEVINFO WINAPI
+SetupDiGetClassDevsA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags)
 {
-    return SetupDiGetClassDevsExA(class, enumstr, parent,
-                                  flags, NULL, NULL, NULL);
+    return SetupDiGetClassDevsExA(ClassGuid, Enumerator, hwndParent,
+                                  Flags, NULL, NULL, NULL);
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsW (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsW(
-       CONST GUID *class,
-       LPCWSTR enumstr,
-       HWND parent,
-       DWORD flags)
+HDEVINFO WINAPI
+SetupDiGetClassDevsW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCWSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags)
 {
-    return SetupDiGetClassDevsExW(class, enumstr, parent,
-                                  flags, NULL, NULL, NULL);
+    return SetupDiGetClassDevsExW(ClassGuid, Enumerator, hwndParent,
+                                  Flags, NULL, NULL, NULL);
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsExA (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsExA(
-       CONST GUID *class,
-       LPCSTR enumstr,
-       HWND parent,
-       DWORD flags,
-       HDEVINFO deviceset,
-       LPCSTR machine,
-       PVOID reserved)
+HDEVINFO WINAPI
+SetupDiGetClassDevsExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags,
+    IN HDEVINFO DeviceInfoSet OPTIONAL,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HDEVINFO ret;
     LPWSTR enumstrW = NULL;
     LPWSTR machineW = NULL;
 
-    if (enumstr)
+    if (Enumerator)
     {
-        int len = MultiByteToWideChar(CP_ACP, 0, enumstr, -1, NULL, 0);
+        int len = MultiByteToWideChar(CP_ACP, 0, Enumerator, -1, NULL, 0);
         enumstrW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
         if (!enumstrW)
         {
             ret = (HDEVINFO)INVALID_HANDLE_VALUE;
             goto end;
         }
-        MultiByteToWideChar(CP_ACP, 0, enumstr, -1, enumstrW, len);
+        MultiByteToWideChar(CP_ACP, 0, Enumerator, -1, enumstrW, len);
     }
-    if (machine)
+    if (MachineName)
     {
-        int len = MultiByteToWideChar(CP_ACP, 0, machine, -1, NULL, 0);
+        int len = MultiByteToWideChar(CP_ACP, 0, MachineName, -1, NULL, 0);
         machineW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
         if (!machineW)
         {
             ret = (HDEVINFO)INVALID_HANDLE_VALUE;
             goto end;
         }
-        MultiByteToWideChar(CP_ACP, 0, machine, -1, machineW, len);
+        MultiByteToWideChar(CP_ACP, 0, MachineName, -1, machineW, len);
     }
-    ret = SetupDiGetClassDevsExW(class, enumstrW, parent, flags, deviceset, machineW, reserved);
+    ret = SetupDiGetClassDevsExW(ClassGuid, enumstrW, hwndParent, Flags, DeviceInfoSet, machineW, Reserved);
 
 end:
     HeapFree(GetProcessHeap(), 0, enumstrW);
@@ -1422,11 +1659,12 @@ CreateDeviceInterface(
     return TRUE;
 }
 
-static LONG SETUP_CreateDevListFromEnumerator(
-       struct DeviceInfoSet *list,
-       LPCGUID pClassGuid OPTIONAL,
-       LPCWSTR Enumerator,
-       HKEY hEnumeratorKey) /* handle to Enumerator registry key */
+static LONG
+SETUP_CreateDevListFromEnumerator(
+    struct DeviceInfoSet *list,
+    CONST GUID *pClassGuid OPTIONAL,
+    LPCWSTR Enumerator,
+    HKEY hEnumeratorKey) /* handle to Enumerator registry key */
 {
     HKEY hDeviceIdKey = NULL, hInstanceIdKey;
     WCHAR KeyBuffer[MAX_PATH];
@@ -1535,11 +1773,12 @@ cleanup:
     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 = HKEY_LOCAL_MACHINE;
     HKEY hEnumKey = NULL;
@@ -1624,19 +1863,20 @@ cleanup:
     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} */
@@ -1695,8 +1935,7 @@ static LONG SETUP_CreateInterfaceList(
             rc = ERROR_GEN_FAILURE;
             goto cleanup;
         }
-        if (InstancePath != NULL)
-            HeapFree(GetProcessHeap(), 0, InstancePath);
+        HeapFree(GetProcessHeap(), 0, InstancePath);
         InstancePath = HeapAlloc(GetProcessHeap(), 0, dwInstancePathLength + sizeof(WCHAR));
         if (!InstancePath)
         {
@@ -1797,8 +2036,7 @@ static LONG SETUP_CreateInterfaceList(
             InsertTailList(&list->ListHead, &deviceInfo->ListEntry);
 
             /* Step 2. Create an interface list for this element */
-            if (pSymbolicLink != NULL)
-                HeapFree(GetProcessHeap(), 0, pSymbolicLink);
+            HeapFree(GetProcessHeap(), 0, pSymbolicLink);
             pSymbolicLink = HeapAlloc(GetProcessHeap(), 0, (dwLength + 1) * sizeof(WCHAR));
             if (!pSymbolicLink)
             {
@@ -1851,50 +2089,49 @@ cleanup:
         RegCloseKey(hDeviceInstanceKey);
     if (hInterfaceKey != INVALID_HANDLE_VALUE)
         RegCloseKey(hInterfaceKey);
-    if (InstancePath != NULL)
-        HeapFree(GetProcessHeap(), 0, InstancePath);
-    if (pSymbolicLink != NULL)
-        HeapFree(GetProcessHeap(), 0, pSymbolicLink);
+    HeapFree(GetProcessHeap(), 0, InstancePath);
+    HeapFree(GetProcessHeap(), 0, pSymbolicLink);
     return rc;
 }
 
 /***********************************************************************
  *             SetupDiGetClassDevsExW (SETUPAPI.@)
  */
-HDEVINFO WINAPI SetupDiGetClassDevsExW(
-       CONST GUID *class OPTIONAL,
-       LPCWSTR enumstr OPTIONAL,
-       HWND parent OPTIONAL,
-       DWORD flags,
-       HDEVINFO deviceset OPTIONAL,
-       LPCWSTR machine OPTIONAL,
-       PVOID reserved)
+HDEVINFO WINAPI
+SetupDiGetClassDevsExW(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN PCWSTR Enumerator OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD Flags,
+    IN HDEVINFO DeviceInfoSet OPTIONAL,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     HDEVINFO hDeviceInfo = INVALID_HANDLE_VALUE;
     struct DeviceInfoSet *list;
-    LPGUID pClassGuid;
+    CONST GUID *pClassGuid;
     LONG rc;
     HDEVINFO ret = INVALID_HANDLE_VALUE;
 
-    TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(class), debugstr_w(enumstr),
-        parent, flags, deviceset, debugstr_w(machine), reserved);
+    TRACE("%s %s %p 0x%08lx %p %s %p\n", debugstr_guid(ClassGuid), debugstr_w(Enumerator),
+        hwndParent, Flags, DeviceInfoSet, debugstr_w(MachineName), Reserved);
 
     /* Create the deviceset if not set */
-    if (deviceset)
+    if (DeviceInfoSet)
     {
-        list = (struct DeviceInfoSet *)deviceset;
+        list = (struct DeviceInfoSet *)DeviceInfoSet;
         if (list->magic != SETUP_DEV_INFO_SET_MAGIC)
         {
             SetLastError(ERROR_INVALID_HANDLE);
             goto cleanup;
         }
-        hDeviceInfo = deviceset;
+        hDeviceInfo = DeviceInfoSet;
     }
     else
     {
          hDeviceInfo = SetupDiCreateDeviceInfoListExW(
-             flags & DIGCF_DEVICEINTERFACE ? NULL : class,
-             NULL, machine, NULL);
+             Flags & DIGCF_DEVICEINTERFACE ? NULL : ClassGuid,
+             NULL, MachineName, NULL);
          if (hDeviceInfo == INVALID_HANDLE_VALUE)
              goto cleanup;
          list = (struct DeviceInfoSet *)hDeviceInfo;
@@ -1905,12 +2142,12 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
     else
         pClassGuid = &list->ClassGuid;
 
-    if (flags & DIGCF_PROFILE)
+    if (Flags & DIGCF_PROFILE)
         FIXME(": flag DIGCF_PROFILE ignored\n");
 
-    if (flags & DIGCF_ALLCLASSES)
+    if (Flags & DIGCF_ALLCLASSES)
     {
-        rc = SETUP_CreateDevList(list, machine, pClassGuid, enumstr);
+        rc = SETUP_CreateDevList(list, MachineName, pClassGuid, Enumerator);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
@@ -1918,15 +2155,15 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
         }
         ret = hDeviceInfo;
     }
-    else if (flags & DIGCF_DEVICEINTERFACE)
+    else if (Flags & DIGCF_DEVICEINTERFACE)
     {
-        if (class == NULL)
+        if (ClassGuid == NULL)
         {
             SetLastError(ERROR_INVALID_PARAMETER);
             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);
@@ -1936,7 +2173,7 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
     }
     else
     {
-        rc = SETUP_CreateDevList(list, machine, (LPGUID)class, enumstr);
+        rc = SETUP_CreateDevList(list, MachineName, ClassGuid, Enumerator);
         if (rc != ERROR_SUCCESS)
         {
             SetLastError(rc);
@@ -1946,7 +2183,7 @@ HDEVINFO WINAPI SetupDiGetClassDevsExW(
     }
 
 cleanup:
-    if (!deviceset && hDeviceInfo != INVALID_HANDLE_VALUE && hDeviceInfo != ret)
+    if (!DeviceInfoSet && hDeviceInfo != INVALID_HANDLE_VALUE && hDeviceInfo != ret)
         SetupDiDestroyDeviceInfoList(hDeviceInfo);
     return ret;
 }
@@ -1955,9 +2192,10 @@ cleanup:
  *             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;
@@ -1999,10 +2237,11 @@ cleanup:
     return ret;
 }
 
-BOOL WINAPI SetupDiGetClassImageIndex(
-       IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN CONST GUID *ClassGuid,
-       OUT PINT ImageIndex)
+BOOL WINAPI
+SetupDiGetClassImageIndex(
+    IN PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN CONST GUID *ClassGuid,
+    OUT PINT ImageIndex)
 {
     struct ClassImageList *list;
     BOOL ret = FALSE;
@@ -2052,8 +2291,9 @@ cleanup:
 /***********************************************************************
  *             SetupDiGetClassImageList(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageList(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
+BOOL WINAPI
+SetupDiGetClassImageList(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData)
 {
     return SetupDiGetClassImageListExW(ClassImageListData, NULL, NULL);
 }
@@ -2061,10 +2301,11 @@ BOOL WINAPI SetupDiGetClassImageList(
 /***********************************************************************
  *             SetupDiGetClassImageListExA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageListExA(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN PCSTR MachineName OPTIONAL,
-       IN PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassImageListExA(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWSTR MachineNameW = NULL;
     BOOL ret;
@@ -2087,10 +2328,11 @@ BOOL WINAPI SetupDiGetClassImageListExA(
 /***********************************************************************
  *             SetupDiGetClassImageListExW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassImageListExW(
-       OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
-       IN PCWSTR MachineName OPTIONAL,
-       IN PVOID Reserved)
+BOOL WINAPI
+SetupDiGetClassImageListExW(
+    OUT PSP_CLASSIMAGELIST_DATA ClassImageListData,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     BOOL ret = FALSE;
 
@@ -2143,10 +2385,11 @@ cleanup:
 /***********************************************************************
  *             SetupDiLoadClassIcon(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiLoadClassIcon(
-       IN CONST GUID *ClassGuid,
-       OUT HICON *LargeIcon OPTIONAL,
-       OUT PINT MiniIconIndex OPTIONAL)
+BOOL WINAPI
+SetupDiLoadClassIcon(
+    IN CONST GUID *ClassGuid,
+    OUT HICON *LargeIcon OPTIONAL,
+    OUT PINT MiniIconIndex OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -2257,12 +2500,13 @@ cleanup:
 /***********************************************************************
  *             SetupDiEnumDeviceInterfaces (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiEnumDeviceInterfaces(
-       HDEVINFO DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       CONST GUID * InterfaceClassGuid,
-       DWORD MemberIndex,
-       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
+BOOL WINAPI
+SetupDiEnumDeviceInterfaces(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN CONST GUID *InterfaceClassGuid,
+    IN DWORD MemberIndex,
+    OUT PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
 {
     BOOL ret = FALSE;
 
@@ -2327,12 +2571,14 @@ BOOL WINAPI SetupDiEnumDeviceInterfaces(
     return ret;
 }
 
-static VOID ReferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+ReferenceInfFile(struct InfFileDetails* infFile)
 {
     InterlockedIncrement(&infFile->References);
 }
 
-static VOID DereferenceInfFile(struct InfFileDetails* infFile)
+static VOID
+DereferenceInfFile(struct InfFileDetails* infFile)
 {
     if (InterlockedDecrement(&infFile->References) == 0)
     {
@@ -2341,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);
@@ -2349,13 +2596,14 @@ static BOOL DestroyDriverInfoElement(struct DriverInfoElement* driverInfo)
     return TRUE;
 }
 
-static BOOL DestroyClassInstallParams(struct ClassInstallParams* installParams)
+static BOOL
+DestroyClassInstallParams(struct ClassInstallParams* installParams)
 {
-    HeapFree(GetProcessHeap(), 0, installParams->PropChange);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, installParams->PropChange);
 }
 
-static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
+static BOOL
+DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
 {
     PLIST_ENTRY ListEntry;
     struct DriverInfoElement *driverInfo;
@@ -2376,11 +2624,11 @@ static BOOL DestroyDeviceInfoElement(struct DeviceInfoElement* deviceInfo)
             return FALSE;
     }
     DestroyClassInstallParams(&deviceInfo->ClassInstallParams);
-    HeapFree(GetProcessHeap(), 0, deviceInfo);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, deviceInfo);
 }
 
-static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
+static BOOL
+DestroyDeviceInfoSet(struct DeviceInfoSet* list)
 {
     PLIST_ENTRY ListEntry;
     struct DeviceInfoElement *deviceInfo;
@@ -2396,21 +2644,22 @@ static BOOL DestroyDeviceInfoSet(struct DeviceInfoSet* list)
         RegCloseKey(list->HKLM);
     CM_Disconnect_Machine(list->hMachine);
     DestroyClassInstallParams(&list->ClassInstallParams);
-    HeapFree(GetProcessHeap(), 0, list);
-    return TRUE;
+    return HeapFree(GetProcessHeap(), 0, list);
 }
 
 /***********************************************************************
  *             SetupDiDestroyDeviceInfoList (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
+BOOL WINAPI
+SetupDiDestroyDeviceInfoList(
+    IN HDEVINFO DeviceInfoSet)
 {
     BOOL ret = FALSE;
 
-    TRACE("%p\n", devinfo);
-    if (devinfo && devinfo != (HDEVINFO)INVALID_HANDLE_VALUE)
+    TRACE("%p\n", DeviceInfoSet);
+    if (DeviceInfoSet && DeviceInfoSet != (HDEVINFO)INVALID_HANDLE_VALUE)
     {
-        struct DeviceInfoSet *list = (struct DeviceInfoSet *)devinfo;
+        struct DeviceInfoSet *list = (struct DeviceInfoSet *)DeviceInfoSet;
 
         if (list->magic == SETUP_DEV_INFO_SET_MAGIC)
             ret = DestroyDeviceInfoSet(list);
@@ -2427,13 +2676,14 @@ BOOL WINAPI SetupDiDestroyDeviceInfoList(HDEVINFO devinfo)
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
-      HDEVINFO DeviceInfoSet,
-      PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
-      PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData,
-      DWORD DeviceInterfaceDetailDataSize,
-      PDWORD RequiredSize,
-      PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiGetDeviceInterfaceDetailA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
+    OUT PSP_DEVICE_INTERFACE_DETAIL_DATA_A DeviceInterfaceDetailData OPTIONAL,
+    IN DWORD DeviceInterfaceDetailDataSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailDataW = NULL;
     DWORD sizeW = 0, sizeA;
@@ -2497,13 +2747,14 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailA(
 /***********************************************************************
  *             SetupDiGetDeviceInterfaceDetailW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
-      HDEVINFO DeviceInfoSet,
-      PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
-      PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData,
-      DWORD DeviceInterfaceDetailDataSize,
-      PDWORD RequiredSize,
-      PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiGetDeviceInterfaceDetailW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
+    OUT PSP_DEVICE_INTERFACE_DETAIL_DATA_W DeviceInterfaceDetailData OPTIONAL,
+    IN DWORD DeviceInterfaceDetailDataSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -2563,31 +2814,40 @@ BOOL WINAPI SetupDiGetDeviceInterfaceDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
-        HDEVINFO  devinfo,
-        PSP_DEVINFO_DATA  DeviceInfoData,
-        DWORD   Property,
-        PDWORD  PropertyRegDataType,
-        PBYTE   PropertyBuffer,
-        DWORD   PropertyBufferSize,
-        PDWORD  RequiredSize)
+BOOL WINAPI
+SetupDiGetDeviceRegistryPropertyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    OUT PDWORD PropertyRegDataType OPTIONAL,
+    OUT PBYTE PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize,
+    OUT PDWORD  RequiredSize OPTIONAL)
 {
     BOOL bResult;
     BOOL bIsStringProperty;
     DWORD RegType;
     DWORD RequiredSizeA, RequiredSizeW;
-    DWORD PropertyBufferSizeW;
-    PBYTE PropertyBufferW;
+    DWORD PropertyBufferSizeW = 0;
+    PBYTE PropertyBufferW = NULL;
 
-    TRACE("%p %p %ld %p %p %ld %p\n", devinfo, DeviceInfoData,
+    TRACE("%p %p %ld %p %p %ld %p\n", DeviceInfoSet, DeviceInfoData,
         Property, PropertyRegDataType, PropertyBuffer, PropertyBufferSize,
         RequiredSize);
 
-    PropertyBufferSizeW = PropertyBufferSize * 2;
-    PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
+    if (PropertyBufferSize != 0)
+    {
+        PropertyBufferSizeW = PropertyBufferSize * 2;
+        PropertyBufferW = HeapAlloc(GetProcessHeap(), 0, PropertyBufferSizeW);
+        if (!PropertyBufferW)
+        {
+            SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+            return FALSE;
+        }
+    }
 
     bResult = SetupDiGetDeviceRegistryPropertyW(
-        devinfo,
+        DeviceInfoSet,
         DeviceInfoData,
         Property,
         &RegType,
@@ -2641,14 +2901,15 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiGetDeviceRegistryPropertyW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
-        HDEVINFO  DeviceInfoSet,
-        PSP_DEVINFO_DATA  DeviceInfoData,
-        DWORD   Property,
-        PDWORD  PropertyRegDataType,
-        PBYTE   PropertyBuffer,
-        DWORD   PropertyBufferSize,
-        PDWORD  RequiredSize)
+BOOL WINAPI
+SetupDiGetDeviceRegistryPropertyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    OUT PDWORD PropertyRegDataType OPTIONAL,
+    OUT PBYTE PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize,
+    OUT PDWORD  RequiredSize OPTIONAL)
 {
     HKEY hEnumKey, hKey;
     DWORD rc;
@@ -2839,12 +3100,13 @@ BOOL WINAPI SetupDiGetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
-        IN HDEVINFO DeviceInfoSet,
-        IN OUT PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Property,
-        IN CONST BYTE *PropertyBuffer,
-        IN DWORD PropertyBufferSize)
+BOOL WINAPI
+SetupDiSetDeviceRegistryPropertyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    IN CONST BYTE *PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize)
 {
     FIXME("%p %p 0x%lx %p 0x%lx\n", DeviceInfoSet, DeviceInfoData,
         Property, PropertyBuffer, PropertyBufferSize);
@@ -2855,12 +3117,13 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyA(
 /***********************************************************************
  *             SetupDiSetDeviceRegistryPropertyW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
-        IN HDEVINFO DeviceInfoSet,
-        IN OUT PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Property,
-        IN const BYTE *PropertyBuffer,
-        IN DWORD PropertyBufferSize)
+BOOL WINAPI
+SetupDiSetDeviceRegistryPropertyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Property,
+    IN CONST BYTE *PropertyBuffer OPTIONAL,
+    IN DWORD PropertyBufferSize)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -2989,11 +3252,12 @@ BOOL WINAPI SetupDiSetDeviceRegistryPropertyW(
 /***********************************************************************
  *             SetupDiInstallClassA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassA(
-        IN HWND hwndParent OPTIONAL,
-        IN PCSTR InfFileName,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL)
+BOOL WINAPI
+SetupDiInstallClassA(
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR InfFileName,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL)
 {
     return SetupDiInstallClassExA(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3002,11 +3266,12 @@ BOOL WINAPI SetupDiInstallClassA(
 /***********************************************************************
  *             SetupDiInstallClassW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassW(
-        IN HWND hwndParent OPTIONAL,
-        IN PCWSTR InfFileName,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL)
+BOOL WINAPI
+SetupDiInstallClassW(
+    IN HWND hwndParent OPTIONAL,
+    IN PCWSTR InfFileName,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL)
 {
     return SetupDiInstallClassExW(hwndParent, InfFileName, Flags, FileQueue, NULL, NULL, NULL);
 }
@@ -3015,14 +3280,15 @@ BOOL WINAPI SetupDiInstallClassW(
 /***********************************************************************
  *             SetupDiInstallClassExA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiInstallClassExA(
-        IN HWND hwndParent OPTIONAL,
-        IN PCSTR InfFileName OPTIONAL,
-        IN DWORD Flags,
-        IN HSPFILEQ FileQueue OPTIONAL,
-        IN const GUID* InterfaceClassGuid OPTIONAL,
-        IN PVOID Reserved1,
-        IN PVOID Reserved2)
+BOOL WINAPI
+SetupDiInstallClassExA(
+    IN HWND hwndParent OPTIONAL,
+    IN PCSTR InfFileName OPTIONAL,
+    IN DWORD Flags,
+    IN HSPFILEQ FileQueue OPTIONAL,
+    IN CONST GUID *InterfaceClassGuid OPTIONAL,
+    IN PVOID Reserved1,
+    IN PVOID Reserved2)
 {
     PWSTR InfFileNameW = NULL;
     BOOL Result;
@@ -3046,7 +3312,8 @@ BOOL WINAPI SetupDiInstallClassExA(
 }
 
 
-static HKEY CreateClassKey(HINF hInf)
+static HKEY
+CreateClassKey(HINF hInf)
 {
     WCHAR FullBuffer[MAX_PATH];
     WCHAR Buffer[MAX_PATH];
@@ -3119,14 +3386,15 @@ cleanup:
 /***********************************************************************
  *             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;
 
@@ -3235,7 +3503,7 @@ BOOL WINAPI SetupDiInstallClassExW(
                 SectionName,
                 SPINST_REGISTRY | SPINST_FILES | SPINST_BITREG | SPINST_INIFILES | SPINST_INI2REG,
                 hRootKey,
-                NULL, /* SourceRootPath */
+                NULL, /* FIXME: SourceRootPath */
                 !(Flags & DI_NOVCP) && (Flags & DI_FORCECOPY) ? SP_COPY_FORCE_IN_USE : 0, /* CopyFlags */
                 SetupDefaultQueueCallbackW,
                 callback_context,
@@ -3278,9 +3546,10 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenClassRegKey  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKey(
-        const GUID* ClassGuid,
-        REGSAM samDesired)
+HKEY WINAPI
+SetupDiOpenClassRegKey(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN REGSAM samDesired)
 {
     return SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
                                      DIOCR_INSTALLER, NULL, NULL);
@@ -3290,12 +3559,13 @@ HKEY WINAPI SetupDiOpenClassRegKey(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExA  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKeyExA(
-        const GUID* ClassGuid OPTIONAL,
-        REGSAM samDesired,
-        DWORD Flags,
-        PCSTR MachineName OPTIONAL,
-        PVOID Reserved)
+HKEY WINAPI
+SetupDiOpenClassRegKeyExA(
+    IN CONST GUID *ClassGuid OPTIONAL,
+    IN REGSAM samDesired,
+    IN DWORD Flags,
+    IN PCSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     PWSTR MachineNameW = NULL;
     HKEY hKey;
@@ -3322,12 +3592,13 @@ HKEY WINAPI SetupDiOpenClassRegKeyExA(
 /***********************************************************************
  *             SetupDiOpenClassRegKeyExW  (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenClassRegKeyExW(
-        const GUID* ClassGuid OPTIONAL,
-        REGSAM samDesired,
-        DWORD Flags,
-        PCWSTR MachineName OPTIONAL,
-        PVOID Reserved)
+HKEY WINAPI
+SetupDiOpenClassRegKeyExW(
+    IN CONST GUID* ClassGuid OPTIONAL,
+    IN REGSAM samDesired,
+    IN DWORD Flags,
+    IN PCWSTR MachineName OPTIONAL,
+    IN PVOID Reserved)
 {
     LPWSTR lpGuidString = NULL;
     LPWSTR lpFullGuidString = NULL;
@@ -3421,8 +3692,7 @@ cleanup:
         RegCloseKey(hClassesKey);
     if (lpGuidString)
         RpcStringFreeW(&lpGuidString);
-    if (lpFullGuidString)
-        HeapFree(GetProcessHeap(), 0, lpFullGuidString);
+    HeapFree(GetProcessHeap(), 0, lpFullGuidString);
 
     return ret;
 }
@@ -3430,11 +3700,12 @@ cleanup:
 /***********************************************************************
  *             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);
@@ -3444,11 +3715,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceW(
 /***********************************************************************
  *             SetupDiOpenDeviceInterfaceA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiOpenDeviceInterfaceA(
-       HDEVINFO DeviceInfoSet,
-       PCSTR DevicePath,
-       DWORD OpenFlags,
-       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData)
+BOOL WINAPI
+SetupDiOpenDeviceInterfaceA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCSTR DevicePath,
+    IN DWORD OpenFlags,
+    OUT PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData OPTIONAL)
 {
     LPWSTR DevicePathW = NULL;
     BOOL bResult;
@@ -3470,11 +3742,12 @@ BOOL WINAPI SetupDiOpenDeviceInterfaceA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetClassInstallParamsA(
-       HDEVINFO  DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       PSP_CLASSINSTALL_HEADER ClassInstallParams,
-       DWORD ClassInstallParamsSize)
+BOOL WINAPI
+SetupDiSetClassInstallParamsA(
+    IN HDEVINFO  DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     FIXME("%p %p %x %lu\n",DeviceInfoSet, DeviceInfoData,
           ClassInstallParams->InstallFunction, ClassInstallParamsSize);
@@ -3484,11 +3757,12 @@ BOOL WINAPI SetupDiSetClassInstallParamsA(
 /***********************************************************************
  *             SetupDiSetClassInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetClassInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
-       IN DWORD ClassInstallParamsSize)
+BOOL WINAPI
+SetupDiSetClassInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -3553,11 +3827,12 @@ done:
     return ret;
 }
 
-static BOOL PropertyChangeHandler(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData,
-       IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
-       IN DWORD ClassInstallParamsSize)
+static BOOL
+PropertyChangeHandler(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
+    IN DWORD ClassInstallParamsSize)
 {
     PSP_PROPCHANGE_PARAMS PropChangeParams = (PSP_PROPCHANGE_PARAMS)ClassInstallParams;
     BOOL ret = FALSE;
@@ -3614,9 +3889,9 @@ done:
 
 static DWORD
 GetFunctionPointer(
-        IN PWSTR InstallerName,
-        OUT HMODULE* ModulePointer,
-        OUT PVOID* FunctionPointer)
+    IN PWSTR InstallerName,
+    OUT HMODULE* ModulePointer,
+    OUT PVOID* FunctionPointer)
 {
     HMODULE hModule = NULL;
     LPSTR FunctionNameA = NULL;
@@ -3675,8 +3950,8 @@ cleanup:
 
 static DWORD
 FreeFunctionPointer(
-        IN HMODULE ModulePointer,
-        IN PVOID FunctionPointer)
+    IN HMODULE ModulePointer,
+    IN PVOID FunctionPointer)
 {
     if (ModulePointer == NULL)
         return ERROR_SUCCESS;
@@ -3697,10 +3972,11 @@ IntSetupDiRegisterDeviceInfo(
 /***********************************************************************
  *             SetupDiCallClassInstaller (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCallClassInstaller(
-       IN DI_FUNCTION InstallFunction,
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+BOOL WINAPI
+SetupDiCallClassInstaller(
+    IN DI_FUNCTION InstallFunction,
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -4033,12 +4309,12 @@ BOOL WINAPI SetupDiCallClassInstaller(
             while (!IsListEmpty(&ClassCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&ClassCoInstallersListHead);
-                HeapFree(GetProcessHeap(), 0, ListEntry);
+                HeapFree(GetProcessHeap(), 0, CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry));
             }
             while (!IsListEmpty(&DeviceCoInstallersListHead))
             {
                 ListEntry = RemoveHeadList(&DeviceCoInstallersListHead);
-                HeapFree(GetProcessHeap(), 0, ListEntry);
+                HeapFree(GetProcessHeap(), 0, CONTAINING_RECORD(ListEntry, struct CoInstallerElement, ListEntry));
             }
 
             ret = (rc == NO_ERROR);
@@ -4052,9 +4328,10 @@ BOOL WINAPI SetupDiCallClassInstaller(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListClass  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInfoListClass(
-        IN HDEVINFO DeviceInfoSet,
-        OUT LPGUID ClassGuid)
+BOOL WINAPI
+SetupDiGetDeviceInfoListClass(
+    IN HDEVINFO DeviceInfoSet,
+    OUT LPGUID ClassGuid)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4081,9 +4358,10 @@ BOOL WINAPI SetupDiGetDeviceInfoListClass(
 /***********************************************************************
  *             SetupDiGetDeviceInfoListDetailW  (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
-        IN HDEVINFO DeviceInfoSet,
-        OUT PSP_DEVINFO_LIST_DETAIL_DATA_W DeviceInfoListDetailData)
+BOOL WINAPI
+SetupDiGetDeviceInfoListDetailW(
+    IN HDEVINFO DeviceInfoSet,
+    OUT PSP_DEVINFO_LIST_DETAIL_DATA_W DeviceInfoListDetailData)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4120,10 +4398,11 @@ BOOL WINAPI SetupDiGetDeviceInfoListDetailW(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstallParamsA(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       OUT PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
+BOOL WINAPI
+SetupDiGetDeviceInstallParamsA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    OUT PSP_DEVINSTALL_PARAMS_A DeviceInstallParams)
 {
     SP_DEVINSTALL_PARAMS_W deviceInstallParamsW;
     BOOL ret = FALSE;
@@ -4162,10 +4441,11 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsA(
 /***********************************************************************
  *             SetupDiGetDeviceInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-       OUT PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+BOOL WINAPI
+SetupDiGetDeviceInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    OUT PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4200,7 +4480,7 @@ BOOL WINAPI SetupDiGetDeviceInstallParamsW(
 
 static BOOL
 CheckDeviceInstallParameters(
-       IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+    IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     DWORD SupportedFlags =
         DI_NOVCP |                            /* 0x00000008 */
@@ -4255,10 +4535,11 @@ CheckDeviceInstallParameters(
 /***********************************************************************
  *             SetupDiSetDeviceInstallParamsW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiSetDeviceInstallParamsW(
-       IN HDEVINFO DeviceInfoSet,
-       IN PSP_DEVINFO_DATA DeviceInfoData,
-       IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
+BOOL WINAPI
+SetupDiSetDeviceInstallParamsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN PSP_DEVINSTALL_PARAMS_W DeviceInstallParams)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4294,12 +4575,13 @@ BOOL WINAPI SetupDiSetDeviceInstallParamsW(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstanceIdA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        OUT PSTR DeviceInstanceId OPTIONAL,
-        IN DWORD DeviceInstanceIdSize,
-        OUT PDWORD RequiredSize OPTIONAL)
+BOOL WINAPI
+SetupDiGetDeviceInstanceIdA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    OUT PSTR DeviceInstanceId OPTIONAL,
+    IN DWORD DeviceInstanceIdSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
     PWSTR DeviceInstanceIdW = NULL;
     BOOL ret = FALSE;
@@ -4340,12 +4622,13 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdA(
 /***********************************************************************
  *             SetupDiGetDeviceInstanceIdW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetDeviceInstanceIdW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        OUT PWSTR DeviceInstanceId OPTIONAL,
-        IN DWORD DeviceInstanceIdSize,
-        OUT PDWORD RequiredSize OPTIONAL)
+BOOL WINAPI
+SetupDiGetDeviceInstanceIdW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    OUT PWSTR DeviceInstanceId OPTIONAL,
+    IN DWORD DeviceInstanceIdSize,
+    OUT PDWORD RequiredSize OPTIONAL)
 {
     BOOL ret = FALSE;
 
@@ -4389,13 +4672,14 @@ BOOL WINAPI SetupDiGetDeviceInstanceIdW(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsA(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDevPropertySheetsA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-        IN LPPROPSHEETHEADERA PropertySheetHeader,
-        IN DWORD PropertySheetHeaderPageListSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        IN DWORD PropertySheetType)
+BOOL WINAPI
+SetupDiGetClassDevPropertySheetsA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN LPPROPSHEETHEADERA PropertySheetHeader,
+    IN DWORD PropertySheetHeaderPageListSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN DWORD PropertySheetType)
 {
     PROPSHEETHEADERW psh;
     BOOL ret = FALSE;
@@ -4427,9 +4711,10 @@ struct ClassDevPropertySheetsData
     DWORD NumberOfPages;
 };
 
-static BOOL WINAPI GetClassDevPropertySheetsCallback(
-        IN HPROPSHEETPAGE hPropSheetPage,
-        IN OUT LPARAM lParam)
+static BOOL WINAPI
+GetClassDevPropertySheetsCallback(
+    IN HPROPSHEETPAGE hPropSheetPage,
+    IN OUT LPARAM lParam)
 {
     struct ClassDevPropertySheetsData *PropPageData;
 
@@ -4448,13 +4733,14 @@ static BOOL WINAPI GetClassDevPropertySheetsCallback(
 /***********************************************************************
  *             SetupDiGetClassDevPropertySheetsW(SETUPAPI.@)
  */
-BOOL WINAPI SetupDiGetClassDevPropertySheetsW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
-        IN OUT LPPROPSHEETHEADERW PropertySheetHeader,
-        IN DWORD PropertySheetHeaderPageListSize,
-        OUT PDWORD RequiredSize OPTIONAL,
-        IN DWORD PropertySheetType)
+BOOL WINAPI
+SetupDiGetClassDevPropertySheetsW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT LPPROPSHEETHEADERW PropertySheetHeader,
+    IN DWORD PropertySheetHeaderPageListSize,
+    OUT PDWORD RequiredSize OPTIONAL,
+    IN DWORD PropertySheetType)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -4579,14 +4865,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyA (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiCreateDevRegKeyA(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Scope,
-        IN DWORD HwProfile,
-        IN DWORD KeyType,
-        IN HINF InfHandle OPTIONAL,
-        IN PCSTR InfSectionName OPTIONAL)
+HKEY WINAPI
+SetupDiCreateDevRegKeyA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN HINF InfHandle OPTIONAL,
+    IN PCSTR InfSectionName OPTIONAL)
 {
     PCWSTR InfSectionNameW = NULL;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4613,9 +4900,9 @@ HKEY WINAPI SetupDiCreateDevRegKeyA(
 
 static HKEY
 OpenHardwareProfileKey(
-        IN HKEY HKLM,
-        IN DWORD HwProfile,
-        IN DWORD samDesired)
+    IN HKEY HKLM,
+    IN DWORD HwProfile,
+    IN DWORD samDesired)
 {
     HKEY hHWProfilesKey = NULL;
     HKEY hHWProfileKey = NULL;
@@ -4671,14 +4958,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDevRegKeyW (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiCreateDevRegKeyW(
-        IN HDEVINFO DeviceInfoSet,
-        IN PSP_DEVINFO_DATA DeviceInfoData,
-        IN DWORD Scope,
-        IN DWORD HwProfile,
-        IN DWORD KeyType,
-        IN HINF InfHandle OPTIONAL,
-        IN PCWSTR InfSectionName OPTIONAL)
+HKEY WINAPI
+SetupDiCreateDevRegKeyW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN HINF InfHandle OPTIONAL,
+    IN PCWSTR InfSectionName OPTIONAL)
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -4871,13 +5159,14 @@ cleanup:
 /***********************************************************************
  *             SetupDiOpenDevRegKey (SETUPAPI.@)
  */
-HKEY WINAPI SetupDiOpenDevRegKey(
-       HDEVINFO DeviceInfoSet,
-       PSP_DEVINFO_DATA DeviceInfoData,
-       DWORD Scope,
-       DWORD HwProfile,
-       DWORD KeyType,
-       REGSAM samDesired)
+HKEY WINAPI
+SetupDiOpenDevRegKey(
+    IN HDEVINFO DeviceInfoSet,
+    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN DWORD Scope,
+    IN DWORD HwProfile,
+    IN DWORD KeyType,
+    IN REGSAM samDesired)
 {
     struct DeviceInfoSet *list;
     HKEY ret = INVALID_HANDLE_VALUE;
@@ -5016,14 +5305,15 @@ cleanup:
 /***********************************************************************
  *             SetupDiCreateDeviceInfoA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCreateDeviceInfoA(
-       HDEVINFO DeviceInfoSet,
-       PCSTR DeviceName,
-       CONST GUID *ClassGuid,
-       PCSTR DeviceDescription,
-       HWND hwndParent,
-       DWORD CreationFlags,
-       PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiCreateDeviceInfoA(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCSTR DeviceName,
+    IN CONST GUID *ClassGuid,
+    IN PCSTR DeviceDescription OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD CreationFlags,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     LPWSTR DeviceNameW = NULL;
     LPWSTR DeviceDescriptionW = NULL;
@@ -5060,14 +5350,15 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(
 /***********************************************************************
  *             SetupDiCreateDeviceInfoW (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCreateDeviceInfoW(
-       HDEVINFO DeviceInfoSet,
-       PCWSTR DeviceName,
-       CONST GUID *ClassGuid,
-       PCWSTR DeviceDescription,
-       HWND hwndParent,
-       DWORD CreationFlags,
-       PSP_DEVINFO_DATA DeviceInfoData)
+BOOL WINAPI
+SetupDiCreateDeviceInfoW(
+    IN HDEVINFO DeviceInfoSet,
+    IN PCWSTR DeviceName,
+    IN CONST GUID *ClassGuid,
+    IN PCWSTR DeviceDescription OPTIONAL,
+    IN HWND hwndParent OPTIONAL,
+    IN DWORD CreationFlags,
+    OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     struct DeviceInfoSet *list;
     BOOL ret = FALSE;
@@ -5564,6 +5855,47 @@ done:
     return Result;
 }
 
+static struct InfFileDetails *
+CreateInfFileDetails(
+    IN LPCWSTR InfFileName)
+{
+    struct InfFileDetails *details;
+    PWCHAR last;
+    DWORD Needed;
+
+    last = strrchrW(InfFileName, '\\');
+    Needed = FIELD_OFFSET(struct InfFileDetails, szData)
+        + strlenW(InfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
+    if (last != NULL)
+    Needed += (last - InfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
+
+    details = HeapAlloc(GetProcessHeap(), 0, Needed);
+    if (!details)
+    {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return NULL;
+    }
+
+    memset(details, 0, Needed);
+    if (last)
+    {
+        details->DirectoryName = details->szData;
+        details->FullInfFileName = &details->szData[last - InfFileName + 1];
+        strncpyW(details->DirectoryName, InfFileName, last - InfFileName);
+    }
+    else
+        details->FullInfFileName = details->szData;
+    strcpyW(details->FullInfFileName, InfFileName);
+    ReferenceInfFile(details);
+    details->hInf = SetupOpenInfFileW(InfFileName, NULL, INF_STYLE_WIN4, NULL);
+    if (details->hInf == INVALID_HANDLE_VALUE)
+    {
+        HeapFree(GetProcessHeap(), 0, details);
+        return NULL;
+    }
+    return details;
+}
+
 /***********************************************************************
  *             SetupDiBuildDriverInfoList (SETUPAPI.@)
  */
@@ -5728,23 +6060,9 @@ SetupDiBuildDriverInfoList(
                 strcpyW(pFullFilename, filename);
                 TRACE("Opening file %s\n", debugstr_w(FullInfFileName));
 
-                currentInfFileDetails = HeapAlloc(
-                    GetProcessHeap(),
-                    0,
-                    FIELD_OFFSET(struct InfFileDetails, FullInfFileName) + strlenW(FullInfFileName) * sizeof(WCHAR) + sizeof(UNICODE_NULL));
+                currentInfFileDetails = CreateInfFileDetails(FullInfFileName);
                 if (!currentInfFileDetails)
                     continue;
-                memset(currentInfFileDetails, 0, sizeof(struct InfFileDetails));
-                strcpyW(currentInfFileDetails->FullInfFileName, FullInfFileName);
-
-                currentInfFileDetails->hInf = SetupOpenInfFileW(FullInfFileName, NULL, INF_STYLE_WIN4, NULL);
-                ReferenceInfFile(currentInfFileDetails);
-                if (currentInfFileDetails->hInf == INVALID_HANDLE_VALUE)
-                {
-                    HeapFree(GetProcessHeap(), 0, currentInfFileDetails);
-                    currentInfFileDetails = NULL;
-                    continue;
-                }
 
                 if (!GetVersionInformationFromInfFile(
                     currentInfFileDetails->hInf,
@@ -5753,8 +6071,7 @@ SetupDiBuildDriverInfoList(
                     &DriverDate,
                     &DriverVersion))
                 {
-                    SetupCloseInfFile(currentInfFileDetails->hInf);
-                    HeapFree(GetProcessHeap(), 0, currentInfFileDetails->hInf);
+                    DereferenceInfFile(currentInfFileDetails);
                     currentInfFileDetails = NULL;
                     continue;
                 }
@@ -5878,7 +6195,7 @@ SetupDiBuildDriverInfoList(
                                 DriverAlreadyAdded = FALSE;
                                 for (DriverRank = 0, currentId = (LPCWSTR)HardwareIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                 {
-                                    if (wcsicmp(DeviceId, currentId) == 0)
+                                    if (strcmpiW(DeviceId, currentId) == 0)
                                     {
                                         AddDriverToList(
                                             pDriverListHead,
@@ -5899,7 +6216,7 @@ SetupDiBuildDriverInfoList(
                                 {
                                     for (DriverRank = 0, currentId = (LPCWSTR)CompatibleIDs; !DriverAlreadyAdded && *currentId; currentId += strlenW(currentId) + 1, DriverRank++)
                                     {
-                                        if (wcsicmp(DeviceId, currentId) == 0)
+                                        if (strcmpiW(DeviceId, currentId) == 0)
                                         {
                                             AddDriverToList(
                                                 pDriverListHead,
@@ -5993,7 +6310,7 @@ SetupDiDeleteDeviceInfo(
 BOOL WINAPI
 SetupDiDestroyDriverInfoList(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData,
+    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN DWORD DriverType)
 {
     struct DeviceInfoSet *list;
@@ -6530,7 +6847,7 @@ SetupDiSetSelectedDevice(
 BOOL WINAPI
 SetupDiSetSelectedDriverA(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN OUT PSP_DRVINFO_DATA_A DriverInfoData OPTIONAL)
 {
     SP_DRVINFO_DATA_V1_W DriverInfoDataW;
@@ -6593,7 +6910,7 @@ SetupDiSetSelectedDriverA(
 BOOL WINAPI
 SetupDiSetSelectedDriverW(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN OUT PSP_DRVINFO_DATA_W DriverInfoData OPTIONAL)
 {
     BOOL ret = FALSE;
@@ -6682,7 +6999,7 @@ SetupDiGetDriverInfoDetailA(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_A DriverInfoData,
-    OUT PSP_DRVINFO_DETAIL_DATA_A DriverInfoDetailData OPTIONAL,
+    IN OUT PSP_DRVINFO_DETAIL_DATA_A DriverInfoDetailData OPTIONAL,
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6883,7 +7200,7 @@ SetupDiGetDriverInfoDetailW(
     IN HDEVINFO DeviceInfoSet,
     IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
     IN PSP_DRVINFO_DATA_W DriverInfoData,
-    OUT PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailData OPTIONAL,
+    IN OUT PSP_DRVINFO_DETAIL_DATA_W DriverInfoDetailData OPTIONAL,
     IN DWORD DriverInfoDetailDataSize,
     OUT PDWORD RequiredSize OPTIONAL)
 {
@@ -6957,7 +7274,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = HardwareIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
-            TRACE("Adding %S to list\n", DeviceID);
+            TRACE("Adding %s to list\n", debugstr_w(DeviceID));
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -6974,7 +7291,7 @@ SetupDiGetDriverInfoDetailW(
         DeviceID = CompatibleIDs;
         while (DeviceID && *DeviceID && (size = wcslen(DeviceID)) + 1 < sizeLeft)
         {
-            TRACE("Adding %S to list\n", DeviceID);
+            TRACE("Adding %s to list\n", debugstr_w(DeviceID));
             wcscpy(pBuffer, DeviceID);
             DeviceID += size + 1;
             pBuffer += size + 1;
@@ -7093,7 +7410,7 @@ static BOOL StopDevice(
 BOOL WINAPI
 SetupDiChangeState(
     IN HDEVINFO DeviceInfoSet,
-    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData)
 {
     PSP_PROPCHANGE_PARAMS PropChange;
     HKEY hKey = INVALID_HANDLE_VALUE;
@@ -7205,7 +7522,7 @@ cleanup:
 BOOL WINAPI
 SetupDiSelectBestCompatDrv(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
 {
     SP_DRVINFO_DATA_W drvInfoData;
     BOOL ret;
@@ -7241,7 +7558,7 @@ SetupDiSelectBestCompatDrv(
 BOOL WINAPI
 SetupDiInstallDriverFiles(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL)
+    IN PSP_DEVINFO_DATA DeviceInfoData)
 {
     BOOL ret = FALSE;
 
@@ -7303,7 +7620,7 @@ SetupDiInstallDriverFiles(
         }
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
+            SPINST_FILES, NULL, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7313,7 +7630,7 @@ SetupDiInstallDriverFiles(
         lstrcatW(SectionName, DotCoInstallers);
         ret = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            SPINST_FILES, NULL, NULL, SP_COPY_NEWER,
+            SPINST_FILES, NULL, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             InstallMsgHandler, InstallMsgHandlerContext,
             DeviceInfoSet, DeviceInfoData);
         if (!ret)
@@ -7410,7 +7727,7 @@ SetupDiRegisterCoDeviceInstallers(
         }
         Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
             SelectedDriver->InfFileDetails->hInf, SectionName,
-            DoAction, hKey, NULL, SP_COPY_NEWER,
+            DoAction, hKey, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
             SetupDefaultQueueCallback, Context,
             DeviceInfoSet, DeviceInfoData);
         if (!Result)
@@ -7573,7 +7890,7 @@ cleanup:
     return ret;
 }
 
-BOOL
+static BOOL
 InfIsFromOEMLocation(
     IN PCWSTR FullName,
     OUT LPBOOL IsOEMLocation)
@@ -7618,7 +7935,7 @@ InfIsFromOEMLocation(
 BOOL WINAPI
 SetupDiInstallDevice(
     IN HDEVINFO DeviceInfoSet,
-    IN PSP_DEVINFO_DATA DeviceInfoData)
+    IN OUT PSP_DEVINFO_DATA DeviceInfoData)
 {
     SP_DEVINSTALL_PARAMS_W InstallParams;
     struct DriverInfoElement *SelectedDriver;
@@ -7717,7 +8034,7 @@ SetupDiInstallDevice(
     pSectionName = &SectionName[strlenW(SectionName)];
 
     /* Get information from [Version] section */
-    if (!SetupDiGetINFClassW(SelectedDriver->Details.InfFileName, &ClassGuid, ClassName, MAX_CLASS_NAME_LEN, &RequiredSize))
+    if (!SetupDiGetINFClassW(SelectedDriver->InfFileDetails->FullInfFileName, &ClassGuid, ClassName, MAX_CLASS_NAME_LEN, &RequiredSize))
         goto cleanup;
     /* Format ClassGuid to a string */
     if (UuidToStringW((UUID*)&ClassGuid, &lpGuidString) != RPC_S_OK)
@@ -7759,7 +8076,7 @@ SetupDiInstallDevice(
     *pSectionName = '\0';
     Result = SetupInstallFromInfSectionW(InstallParams.hwndParent,
         SelectedDriver->InfFileDetails->hInf, SectionName,
-        DoAction, hKey, NULL, SP_COPY_NEWER,
+        DoAction, hKey, SelectedDriver->InfFileDetails->DirectoryName, SP_COPY_NEWER,
         SetupDefaultQueueCallback, Context,
         DeviceInfoSet, DeviceInfoData);
     if (!Result)
@@ -7779,7 +8096,7 @@ SetupDiInstallDevice(
     TRACE("InfSectionExt   : '%s'\n", debugstr_w(&SectionName[strlenW(SelectedDriver->Details.SectionName)]));
     TRACE("MatchingDeviceId: '%s'\n", debugstr_w(SelectedDriver->MatchingId));
     TRACE("ProviderName    : '%s'\n", debugstr_w(SelectedDriver->Info.ProviderName));
-    swprintf(Buffer, L"%u-%u-%u", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
+    sprintfW(Buffer, L"%u-%u-%u", DriverDate.wMonth, DriverDate.wDay, DriverDate.wYear);
     rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
     if (rc == ERROR_SUCCESS)
         rc = RegSetValueEx(hKey, REGSTR_DRIVER_DATE_DATA, 0, REG_BINARY, (const BYTE *)&SelectedDriver->Info.DriverDate, sizeof(FILETIME));
@@ -7787,7 +8104,7 @@ SetupDiInstallDevice(
         rc = RegSetValueEx(hKey, REGSTR_VAL_DRVDESC, 0, REG_SZ, (const BYTE *)SelectedDriver->Info.Description, (strlenW(SelectedDriver->Info.Description) + 1) * sizeof(WCHAR));
     if (rc == ERROR_SUCCESS)
     {
-        swprintf(Buffer, L"%u.%u.%u.%u", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
+        sprintfW(Buffer, L"%u.%u.%u.%u", fullVersion.HighPart >> 16, fullVersion.HighPart & 0xffff, fullVersion.LowPart >> 16, fullVersion.LowPart & 0xffff);
         rc = RegSetValueEx(hKey, REGSTR_DRIVER_VERSION, 0, REG_SZ, (const BYTE *)Buffer, (strlenW(Buffer) + 1) * sizeof(WCHAR));
     }
     if (rc == ERROR_SUCCESS)