1

I'm calling the Setup API function SetupDiGetDriverInfoDetail like this:

SP_DRVINFO_DETAIL_DATA_W driverDetailData = SP_DRVINFO_DETAIL_DATA_W();
driverDetailData.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);
  
DWORD reqSize = 0;
  
ok = SetupDiGetDriverInfoDetailW(deviceList, nullptr, &driverData, &driverDetailData, sizeof(SP_DRVINFO_DETAIL_DATA_W), &reqSize);

The call returns false, the last Windows error code is 0x7a ("ERROR_INSUFFICIENT_BUFFER"). When I compare cbSize and reqSize I can see why: cbSize is 1584 bytes, reqSize is 1622 bytes.

If I understand the MSDN page on SetupDiGetDriverInfoDetail correctly (https://learn.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetdriverinfodetailw), my call should work as expected.

What did I miss? How does one call SetupDiGetDriverInfoDetail correctly, so that the input buffer is large enough for the call to succeed?

Boris
  • 8,551
  • 25
  • 67
  • 120

1 Answers1

1

SP_DRVINFO_DETAIL_DATA is a variable length structure because of the HardwareID[ANYSIZE_ARRAY]; buffer at the end. Once the required size is known, it can be allocated dynamically.

DWORD reqSize = 0;
SetupDiGetDriverInfoDetailW(deviceList, nullptr, &driverData, NULL, 0, &reqSize);

SP_DRVINFO_DETAIL_DATA_W *driverDetailData = (SP_DRVINFO_DETAIL_DATA_W *)calloc(1, reqSize);
driverDetailData->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA_W);

SetupDiGetDriverInfoDetailW(deviceList, nullptr, &driverData, driverDetailData, reqSize, &reqSize);
dxiv
  • 16,984
  • 2
  • 27
  • 49
  • 1
    Related: [Why do some structures end with an array of size 1?](https://devblogs.microsoft.com/oldnewthing/20040826-00/?p=38043) – IInspectable Nov 25 '20 at 07:18