Author: hpoussin
Date: Sun Jun 11 00:48:17 2006
New Revision: 22299
URL:
http://svn.reactos.ru/svn/reactos?rev=22299&view=rev
Log:
Cleanup:
- Check that mandatory arguments are provided
- Better tracing system
- Prepend private function names with SETUP_
- Use MultiByteToUnicode instead of MultiByteToUnicode + HeapAlloc
- ...
Modified:
trunk/reactos/dll/win32/setupapi/devclass.c
Modified: trunk/reactos/dll/win32/setupapi/devclass.c
URL:
http://svn.reactos.ru/svn/reactos/trunk/reactos/dll/win32/setupapi/devclass…
==============================================================================
--- trunk/reactos/dll/win32/setupapi/devclass.c (original)
+++ trunk/reactos/dll/win32/setupapi/devclass.c Sun Jun 11 00:48:17 2006
@@ -45,7 +45,7 @@
IN DWORD ClassInstallParamsSize);
static BOOL
-PropertyChangeHandler(
+SETUP_PropertyChangeHandler(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData OPTIONAL,
IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
@@ -69,7 +69,7 @@
NULL, /* DIF_DETECT */
NULL, /* DIF_INSTALLWIZARD */
NULL, /* DIF_DESTROYWIZARDDATA */
- PropertyChangeHandler, /* DIF_PROPERTYCHANGE */
+ SETUP_PropertyChangeHandler, /* DIF_PROPERTYCHANGE */
NULL, /* DIF_ENABLECLASS */
NULL, /* DIF_DETECTVERIFY */
NULL, /* DIF_INSTALLDEVICEFILES */
@@ -153,7 +153,8 @@
LPWSTR MachineNameW = NULL;
BOOL bResult;
- TRACE("\n");
+ TRACE("0x%lx %p %lu %p %s %p\n", Flags, ClassGuidList,
+ ClassGuidListSize, RequiredSize, debugstr_a(MachineName), Reserved);
if (MachineName)
{
@@ -165,8 +166,7 @@
ClassGuidListSize, RequiredSize,
MachineNameW, Reserved);
- if (MachineNameW)
- MyFree(MachineNameW);
+ MyFree(MachineNameW);
return bResult;
}
@@ -210,8 +210,16 @@
TRACE("0x%lx %p %lu %p %s %p\n", Flags, ClassGuidList,
ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
- if (RequiredSize != NULL)
- *RequiredSize = 0;
+ if (!RequiredSize)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ goto cleanup;
+ }
+ else if (!ClassGuidList && ClassGuidListSize > 0)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ goto cleanup;
+ }
hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
KEY_ENUMERATE_SUB_KEYS,
@@ -232,7 +240,6 @@
NULL,
NULL,
NULL);
- TRACE("RegEnumKeyExW() returns %ld\n", lError);
if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
{
TRACE("Key name: %s\n", debugstr_w(szKeyName));
@@ -288,7 +295,6 @@
{
if (szKeyName[0] == L'{' && szKeyName[37] ==
L'}')
szKeyName[37] = 0;
- TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
UuidFromStringW(&szKeyName[1],
&ClassGuidList[dwGuidListIndex]);
@@ -296,9 +302,11 @@
dwGuidListIndex++;
}
+ else
+ TRACE("RegEnumKeyExW() returns 0x%lx\n", lError);
if (lError != ERROR_SUCCESS)
- break;
+ break;
}
if (RequiredSize != NULL)
@@ -360,31 +368,34 @@
{
LPWSTR ClassNameW = NULL;
LPWSTR MachineNameW = NULL;
- BOOL bResult;
-
- TRACE("\n");
+ BOOL bResult = FALSE;
+
+ TRACE("%s %p %lu %p %s %p\n", debugstr_a(ClassName), ClassGuidList,
+ ClassGuidListSize, RequiredSize, debugstr_a(MachineName), Reserved);
+
+ if (!ClassName)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ goto cleanup;
+ }
ClassNameW = MultiByteToUnicode(ClassName, CP_ACP);
if (ClassNameW == NULL)
- return FALSE;
-
- if (MachineNameW)
+ goto cleanup;
+
+ if (MachineName)
{
MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
if (MachineNameW == NULL)
- {
- MyFree(ClassNameW);
- return FALSE;
- }
+ goto cleanup;
}
bResult = SetupDiClassGuidsFromNameExW(ClassNameW, ClassGuidList,
ClassGuidListSize, RequiredSize,
MachineNameW, Reserved);
- if (MachineNameW)
- MyFree(MachineNameW);
-
+cleanup:
+ MyFree(MachineNameW);
MyFree(ClassNameW);
return bResult;
@@ -403,7 +414,7 @@
IN PVOID Reserved)
{
WCHAR szKeyName[MAX_GUID_STRING_LEN + 1];
- WCHAR szClassName[256];
+ WCHAR szClassName[MAX_CLASS_NAME_LEN];
HKEY hClassesKey = INVALID_HANDLE_VALUE;
HKEY hClassKey = NULL;
DWORD dwLength;
@@ -415,8 +426,17 @@
TRACE("%s %p %lu %p %s %p\n", debugstr_w(ClassName), ClassGuidList,
ClassGuidListSize, RequiredSize, debugstr_w(MachineName), Reserved);
- if (RequiredSize != NULL)
- *RequiredSize = 0;
+ if (!ClassName || !RequiredSize)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ goto cleanup;
+ }
+ else if (!ClassGuidList && ClassGuidListSize > 0)
+ {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ goto cleanup;
+ }
+ *RequiredSize = 0;
hClassesKey = SetupDiOpenClassRegKeyExW(NULL,
KEY_ENUMERATE_SUB_KEYS,
@@ -437,7 +457,6 @@
NULL,
NULL,
NULL);
- TRACE("RegEnumKeyExW() returns %ld\n", lError);
if (lError == ERROR_SUCCESS || lError == ERROR_MORE_DATA)
{
TRACE("Key name: %s\n", debugstr_w(szKeyName));
@@ -453,7 +472,7 @@
goto cleanup;
}
- dwLength = 256 * sizeof(WCHAR);
+ dwLength = MAX_CLASS_NAME_LEN * sizeof(WCHAR);
if (RegQueryValueExW(hClassKey,
Class,
NULL,
@@ -472,7 +491,6 @@
{
if (szKeyName[0] == L'{' && szKeyName[37] ==
L'}')
szKeyName[37] = 0;
- TRACE("Guid: %s\n", debugstr_w(&szKeyName[1]));
UuidFromStringW(&szKeyName[1],
&ClassGuidList[dwGuidListIndex]);
@@ -482,6 +500,8 @@
}
}
}
+ else
+ TRACE("RegEnumKeyExW() returns 0x%lx\n", lError);
if (lError != ERROR_SUCCESS)
break;
@@ -552,14 +572,16 @@
if (MachineName)
MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
ret = SetupDiClassNameFromGuidExW(ClassGuid, ClassNameW, MAX_CLASS_NAME_LEN,
- NULL, MachineNameW, Reserved);
+ RequiredSize, MachineNameW, Reserved);
if (ret)
{
int len = WideCharToMultiByte(CP_ACP, 0, ClassNameW, -1, ClassName,
- ClassNameSize, NULL, NULL);
-
- if (!ClassNameSize && RequiredSize)
- *RequiredSize = len;
+ ClassNameSize, NULL, NULL);
+ if (len > ClassNameSize)
+ {
+ SetLastError(ERROR_INSUFFICIENT_BUFFER);
+ ret = FALSE;
+ }
}
MyFree(MachineNameW);
return ret;
@@ -674,23 +696,22 @@
IN PCSTR MachineName OPTIONAL,
IN PVOID Reserved)
{
- PWCHAR ClassDescriptionW;
+ PWCHAR ClassDescriptionW = NULL;
LPWSTR MachineNameW = NULL;
- BOOL ret;
-
- TRACE("\n");
+ BOOL ret = FALSE;
+
+ TRACE("%s %p %lu %p %s %p\n", debugstr_guid(ClassGuid), ClassDescription,
+ ClassDescriptionSize, RequiredSize, debugstr_a(MachineName), Reserved);
+
if (ClassDescriptionSize > 0)
{
- ClassDescriptionW = HeapAlloc(GetProcessHeap(), 0, ClassDescriptionSize *
sizeof(WCHAR));
+ ClassDescriptionW = MyMalloc(ClassDescriptionSize * sizeof(WCHAR));
if (!ClassDescriptionW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
- ret = FALSE;
- goto end;
- }
- }
- else
- ClassDescriptionW = NULL;
+ goto cleanup;
+ }
+ }
if (MachineName)
{
@@ -698,24 +719,25 @@
if (!MachineNameW)
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ goto cleanup;
+ }
+ }
+
+ ret = SetupDiGetClassDescriptionExW(ClassGuid, ClassDescriptionW,
+ ClassDescriptionSize * sizeof(WCHAR), RequiredSize, MachineNameW, Reserved);
+ if (ret)
+ {
+ int len = WideCharToMultiByte(CP_ACP, 0, ClassDescriptionW, -1,
ClassDescription,
+ ClassDescriptionSize, NULL, NULL);
+ if (len > ClassDescriptionSize)
+ {
+ SetLastError(ERROR_INSUFFICIENT_BUFFER);
ret = FALSE;
- goto end;
- }
- }
-
- ret = SetupDiGetClassDescriptionExW(ClassGuid, ClassDescriptionW,
ClassDescriptionSize * sizeof(WCHAR),
- NULL, MachineNameW, Reserved);
- if (ret)
- {
- int len = WideCharToMultiByte(CP_ACP, 0, ClassDescriptionW, -1,
ClassDescription,
- ClassDescriptionSize, NULL, NULL);
-
- if (!ClassDescriptionSize && RequiredSize)
- *RequiredSize = len;
- }
-
-end:
- HeapFree(GetProcessHeap(), 0, ClassDescriptionW);
+ }
+ }
+
+cleanup:
+ MyFree(ClassDescriptionW);
MyFree(MachineNameW);
return ret;
}
@@ -782,7 +804,6 @@
cleanup:
if (hKey != INVALID_HANDLE_VALUE)
RegCloseKey(hKey);
-
return ret;
}
@@ -827,49 +848,50 @@
IN PCSTR MachineName OPTIONAL,
IN PVOID Reserved)
{
- HDEVINFO ret;
- LPWSTR enumstrW = NULL;
- LPWSTR machineW = NULL;
+ LPWSTR EnumeratorW = NULL;
+ LPWSTR MachineNameW = NULL;
+ HDEVINFO ret = (HDEVINFO)INVALID_HANDLE_VALUE;
if (Enumerator)
{
- 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, Enumerator, -1, enumstrW, len);
+ EnumeratorW = MultiByteToUnicode(Enumerator, CP_ACP);
+ if (!EnumeratorW)
+ goto cleanup;
}
if (MachineName)
{
- 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, MachineName, -1, machineW, len);
- }
- ret = SetupDiGetClassDevsExW(ClassGuid, enumstrW, hwndParent, Flags, DeviceInfoSet,
machineW, Reserved);
-
-end:
- HeapFree(GetProcessHeap(), 0, enumstrW);
- HeapFree(GetProcessHeap(), 0, machineW);
+ MachineNameW = MultiByteToUnicode(MachineName, CP_ACP);
+ if (!MachineNameW)
+ goto cleanup;
+ }
+ ret = SetupDiGetClassDevsExW(ClassGuid, EnumeratorW, hwndParent,
+ Flags, DeviceInfoSet, MachineNameW, Reserved);
+
+cleanup:
+ MyFree(EnumeratorW);
+ MyFree(MachineNameW);
return ret;
}
/***********************************************************************
- * Helper functions for SetupDiGetClassDevsExW
+ * SETUP_CreateDevicesListFromEnumerator
+ *
+ * PARAMS
+ * list [I] Device info set to fill with discovered devices.
+ * pClassGuid [I] If specified, only devices which belong to this class will be added.
+ * Enumerator [I] Location to search devices to add.
+ * hEnumeratorKey [O] Registry key corresponding to Enumerator key. Must have
KEY_ENUMERATE_SUB_KEYS right.
+ *
+ * RETURNS
+ * Success: ERROR_SUCCESS.
+ * Failure: an error code.
*/
static LONG
-SETUP_CreateDevListFromEnumerator(
- struct DeviceInfoSet *list,
- CONST GUID *pClassGuid OPTIONAL,
- LPCWSTR Enumerator,
- HKEY hEnumeratorKey) /* handle to Enumerator registry key */
+SETUP_CreateDevicesListFromEnumerator(
+ IN OUT struct DeviceInfoSet *list,
+ IN CONST GUID *pClassGuid OPTIONAL,
+ IN LPCWSTR Enumerator,
+ IN HKEY hEnumeratorKey) /* handle to Enumerator registry key */
{
HKEY hDeviceIdKey = NULL, hInstanceIdKey;
WCHAR KeyBuffer[MAX_PATH];
@@ -947,7 +969,7 @@
}
else
{
- KeyBuffer[37] = '\0'; /* Replace the } by a NULL character */
+ KeyBuffer[MAX_GUID_STRING_LEN - 2] = '\0'; /* Replace the } by a
NULL character */
if (UuidFromStringW(&KeyBuffer[1], &KeyGuid) != RPC_S_OK)
/* Bad GUID, skip the entry */
continue;
@@ -979,11 +1001,11 @@
}
static LONG
-SETUP_CreateDevList(
- struct DeviceInfoSet *list,
- PCWSTR MachineName OPTIONAL,
- CONST GUID *class OPTIONAL,
- PCWSTR Enumerator OPTIONAL)
+SETUP_CreateDevicesList(
+ IN OUT struct DeviceInfoSet *list,
+ IN PCWSTR MachineName OPTIONAL,
+ IN CONST GUID *Class OPTIONAL,
+ IN PCWSTR Enumerator OPTIONAL)
{
HKEY HKLM = HKEY_LOCAL_MACHINE;
HKEY hEnumKey = NULL;
@@ -993,8 +1015,8 @@
DWORD dwLength;
DWORD rc;
- if (class && IsEqualIID(class, &GUID_NULL))
- class = NULL;
+ if (Class && IsEqualIID(Class, &GUID_NULL))
+ Class = NULL;
/* Open Enum key (if applicable) */
if (MachineName != NULL)
@@ -1013,8 +1035,8 @@
if (rc != ERROR_SUCCESS)
goto cleanup;
- /* If enumerator is provided, call directly SETUP_CreateDevListFromEnumerator.
- * Else, enumerate all enumerators and call SETUP_CreateDevListFromEnumerator
+ /* If enumerator is provided, call directly SETUP_CreateDevicesListFromEnumerator.
+ * Else, enumerate all enumerators and call SETUP_CreateDevicesListFromEnumerator
* for each one.
*/
if (Enumerator)
@@ -1027,7 +1049,7 @@
&hEnumeratorKey);
if (rc != ERROR_SUCCESS)
goto cleanup;
- rc = SETUP_CreateDevListFromEnumerator(list, class, Enumerator, hEnumeratorKey);
+ rc = SETUP_CreateDevicesListFromEnumerator(list, Class, Enumerator,
hEnumeratorKey);
}
else
{
@@ -1050,8 +1072,8 @@
if (rc != ERROR_SUCCESS)
goto cleanup;
- /* Call SETUP_CreateDevListFromEnumerator */
- rc = SETUP_CreateDevListFromEnumerator(list, class, KeyBuffer,
hEnumeratorKey);
+ /* Call SETUP_CreateDevicesListFromEnumerator */
+ rc = SETUP_CreateDevicesListFromEnumerator(list, Class, KeyBuffer,
hEnumeratorKey);
if (rc != ERROR_SUCCESS)
goto cleanup;
}
@@ -1121,7 +1143,7 @@
if (Flags & DIGCF_ALLCLASSES)
{
- rc = SETUP_CreateDevList(list, MachineName, pClassGuid, Enumerator);
+ rc = SETUP_CreateDevicesList(list, MachineName, pClassGuid, Enumerator);
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
@@ -1147,7 +1169,7 @@
}
else
{
- rc = SETUP_CreateDevList(list, MachineName, ClassGuid, Enumerator);
+ rc = SETUP_CreateDevicesList(list, MachineName, ClassGuid, Enumerator);
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
@@ -1166,7 +1188,7 @@
* SetupDiGetClassImageIndex (SETUPAPI.@)
*/
static BOOL
-GetIconIndex(
+SETUP_GetIconIndex(
IN HKEY hClassKey,
OUT PINT ImageIndex)
{
@@ -1240,7 +1262,7 @@
hKey = SetupDiOpenClassRegKeyExW(ClassGuid, KEY_QUERY_VALUE, DIOCR_INTERFACE,
list->MachineName, NULL);
if (hKey == INVALID_HANDLE_VALUE)
goto cleanup;
- if (!GetIconIndex(hKey, &iconIndex))
+ if (!SETUP_GetIconIndex(hKey, &iconIndex))
goto cleanup;
if (iconIndex >= 0)
@@ -1292,8 +1314,7 @@
ret = SetupDiGetClassImageListExW(ClassImageListData, MachineNameW, Reserved);
- if (MachineNameW)
- MyFree(MachineNameW);
+ MyFree(MachineNameW);
return ret;
}
@@ -1379,7 +1400,7 @@
if (hKey == INVALID_HANDLE_VALUE)
goto cleanup;
- if (!GetIconIndex(hKey, &iconIndex))
+ if (!SETUP_GetIconIndex(hKey, &iconIndex))
goto cleanup;
if (iconIndex > 0)
@@ -1534,7 +1555,7 @@
* Helper function for SetupDiInstallClassExW
*/
static HKEY
-CreateClassKey(HINF hInf)
+SETUP_CreateClassKey(HINF hInf)
{
WCHAR FullBuffer[MAX_PATH];
WCHAR Buffer[MAX_PATH];
@@ -1698,7 +1719,7 @@
else
{
/* Create or open the class registry key
'HKLM\CurrentControlSet\Class\{GUID}' */
- hRootKey = CreateClassKey(hInf);
+ hRootKey = SETUP_CreateClassKey(hInf);
if (hRootKey == INVALID_HANDLE_VALUE)
goto cleanup;
@@ -1788,7 +1809,8 @@
PWSTR MachineNameW = NULL;
HKEY hKey;
- TRACE("\n");
+ TRACE("%s 0x%lx 0x%lx %s %p\n", debugstr_guid(ClassGuid), samDesired,
+ Flags, debugstr_a(MachineName), Reserved);
if (MachineName)
{
@@ -1800,8 +1822,7 @@
hKey = SetupDiOpenClassRegKeyExW(ClassGuid, samDesired,
Flags, MachineNameW, Reserved);
- if (MachineNameW)
- MyFree(MachineNameW);
+ MyFree(MachineNameW);
return hKey;
}
@@ -1836,7 +1857,7 @@
lpKeyName = REGSTR_PATH_DEVICE_CLASSES;
else
{
- ERR("Invalid Flags parameter!\n");
+ TRACE("Unknown flags: 0x%lx\n", Flags);
SetLastError(ERROR_INVALID_FLAGS);
goto cleanup;
}
@@ -1858,7 +1879,8 @@
0,
ClassGuid ? 0 : samDesired,
&hClassesKey);
- if (MachineName != NULL) RegCloseKey(HKLM);
+ if (MachineName != NULL)
+ RegCloseKey(HKLM);
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
@@ -1933,7 +1955,7 @@
* Helper functions for SetupDiSetClassInstallParamsW
*/
static BOOL
-PropertyChangeHandler(
+SETUP_PropertyChangeHandler(
IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
IN PSP_CLASSINSTALL_HEADER ClassInstallParams OPTIONAL,
@@ -2108,7 +2130,7 @@
};
static BOOL WINAPI
-GetClassDevPropertySheetsCallback(
+SETUP_GetClassDevPropertySheetsCallback(
IN HPROPSHEETPAGE hPropSheetPage,
IN OUT LPARAM lParam)
{
@@ -2230,7 +2252,7 @@
PropPageData.PropertySheetPages =
&PropertySheetHeader->phpage[PropertySheetHeader->nPages];
PropPageData.MaximumNumberOfPages = PropertySheetHeaderPageListSize -
PropertySheetHeader->nPages;
PropPageData.NumberOfPages = 0;
- ret = pPropPageProvider(&Request, GetClassDevPropertySheetsCallback,
(LPARAM)&PropPageData);
+ ret = pPropPageProvider(&Request, SETUP_GetClassDevPropertySheetsCallback,
(LPARAM)&PropPageData);
if (!ret)
goto cleanup;