我正在这样调用安装程序API函数 SetupDiGetDriverInfoDetail :
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);
该调用返回false,最后一个Windows错误代码为0x7a(“ERROR_INSUFFICIENT_BUFFER”)。当我比较cbSize和reqSize时,我可以看到原因:cbSize是1584字节,reqSize是1622字节。如果我正确理解SetupDiGetDriverInfoDetail上的MSDN页面(https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetdriverinfodetailw),则我的 call 应该可以正常工作。
我错过了什么?如何正确调用SetupDiGetDriverInfoDetail,以便输入缓冲区足够大以使调用成功?
最佳答案
SP_DRVINFO_DETAIL_DATA
是可变长度的结构,因为最后是HardwareID[ANYSIZE_ARRAY];
缓冲区。一旦知道所需的大小,就可以动态分配。
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);