Commit 81d2b419 authored by Zebediah Figura's avatar Zebediah Figura Committed by Alexandre Julliard

setupapi: Avoid unnecessary buffer allocation in SetupDiGetDeviceInstanceIdA().

parent ad531404
......@@ -1607,57 +1607,25 @@ BOOL WINAPI SetupDiEnumDeviceInfo(HDEVINFO devinfo, DWORD index, SP_DEVINFO_DATA
/***********************************************************************
* SetupDiGetDeviceInstanceIdA (SETUPAPI.@)
*/
BOOL WINAPI SetupDiGetDeviceInstanceIdA(
HDEVINFO DeviceInfoSet,
PSP_DEVINFO_DATA DeviceInfoData,
PSTR DeviceInstanceId,
DWORD DeviceInstanceIdSize,
PDWORD RequiredSize)
BOOL WINAPI SetupDiGetDeviceInstanceIdA(HDEVINFO devinfo, SP_DEVINFO_DATA *device_data,
char *id, DWORD size, DWORD *needed)
{
BOOL ret = FALSE;
DWORD size;
PWSTR instanceId;
WCHAR idW[MAX_DEVICE_ID_LEN];
TRACE("%p %p %p %d %p\n", DeviceInfoSet, DeviceInfoData, DeviceInstanceId,
DeviceInstanceIdSize, RequiredSize);
TRACE("devinfo %p, device_data %p, id %p, size %d, needed %p.\n",
devinfo, device_data, id, size, needed);
SetupDiGetDeviceInstanceIdW(DeviceInfoSet,
DeviceInfoData,
NULL,
0,
&size);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
if (!SetupDiGetDeviceInstanceIdW(devinfo, device_data, idW, ARRAY_SIZE(idW), NULL))
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)
ret = FALSE;
else
{
if (len > DeviceInstanceIdSize)
{
if (needed)
*needed = WideCharToMultiByte(CP_ACP, 0, idW, -1, NULL, 0, NULL, NULL);
if (size && WideCharToMultiByte(CP_ACP, 0, idW, -1, id, size, NULL, NULL))
return TRUE;
SetLastError(ERROR_INSUFFICIENT_BUFFER);
ret = FALSE;
}
if (RequiredSize)
*RequiredSize = len;
}
}
HeapFree(GetProcessHeap(), 0, instanceId);
}
return ret;
return FALSE;
}
/***********************************************************************
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment