setupapi: Avoid unnecessary buffer allocation in SetupDiGetDeviceInstanceIdA().
Signed-off-by: Zebediah Figura <z.figura12@gmail.com> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
parent
ad5314040b
commit
81d2b419b8
|
@ -1607,57 +1607,25 @@ BOOL WINAPI SetupDiEnumDeviceInfo(HDEVINFO devinfo, DWORD index, SP_DEVINFO_DATA
|
||||||
/***********************************************************************
|
/***********************************************************************
|
||||||
* SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
|
* SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
|
||||||
*/
|
*/
|
||||||
BOOL WINAPI SetupDiGetDeviceInstanceIdA(
|
BOOL WINAPI SetupDiGetDeviceInstanceIdA(HDEVINFO devinfo, SP_DEVINFO_DATA *device_data,
|
||||||
HDEVINFO DeviceInfoSet,
|
char *id, DWORD size, DWORD *needed)
|
||||||
PSP_DEVINFO_DATA DeviceInfoData,
|
|
||||||
PSTR DeviceInstanceId,
|
|
||||||
DWORD DeviceInstanceIdSize,
|
|
||||||
PDWORD RequiredSize)
|
|
||||||
{
|
{
|
||||||
BOOL ret = FALSE;
|
WCHAR idW[MAX_DEVICE_ID_LEN];
|
||||||
DWORD size;
|
|
||||||
PWSTR instanceId;
|
|
||||||
|
|
||||||
TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
|
TRACE("devinfo %p, device_data %p, id %p, size %d, needed %p.\n",
|
||||||
DeviceInstanceIdSize, RequiredSize);
|
devinfo, device_data, id, size, needed);
|
||||||
|
|
||||||
SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
|
if (!SetupDiGetDeviceInstanceIdW(devinfo, device_data, idW, ARRAY_SIZE(idW), NULL))
|
||||||
DeviceInfoData,
|
|
||||||
NULL,
|
|
||||||
0,
|
|
||||||
&size);
|
|
||||||
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
instanceId = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
|
|
||||||
if (instanceId)
|
|
||||||
{
|
|
||||||
ret = SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
|
|
||||||
DeviceInfoData,
|
|
||||||
instanceId,
|
|
||||||
size,
|
|
||||||
&size);
|
|
||||||
if (ret)
|
|
||||||
{
|
|
||||||
int len = WideCharToMultiByte(CP_ACP, 0, instanceId, -1,
|
|
||||||
DeviceInstanceId,
|
|
||||||
DeviceInstanceIdSize, NULL, NULL);
|
|
||||||
|
|
||||||
if (!len)
|
if (needed)
|
||||||
ret = FALSE;
|
*needed = WideCharToMultiByte(CP_ACP, 0, idW, -1, NULL, 0, NULL, NULL);
|
||||||
else
|
|
||||||
{
|
if (size && WideCharToMultiByte(CP_ACP, 0, idW, -1, id, size, NULL, NULL))
|
||||||
if (len > DeviceInstanceIdSize)
|
return TRUE;
|
||||||
{
|
|
||||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||||
ret = FALSE;
|
return FALSE;
|
||||||
}
|
|
||||||
if (RequiredSize)
|
|
||||||
*RequiredSize = len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
HeapFree(GetProcessHeap(), 0, instanceId);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***********************************************************************
|
/***********************************************************************
|
||||||
|
|
Loading…
Reference in New Issue