gpt4 book ai didi

c++ - SetupDiXxx 结构的堆栈损坏

转载 作者:行者123 更新时间:2023-11-28 00:18:49 40 4
gpt4 key购买 nike

我在使用用于获取设备信息的结构时遇到了一些问题。据我所知,正确设置 cbSize 有点棘手,因此 API 正在将数据写入超出预期的位置(导致堆栈损坏)。到目前为止,我有以下代码:

GUID guid;
HidD_GetHidGuid(&guid);

HDEVINFO info;
info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

SP_DEVINFO_DATA DeviceInfoData;

memset(&DeviceInfoData, 0, sizeof(SP_DEVINFO_DATA));
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);


int deviceIndex = 0;
while (SetupDiEnumDeviceInfo(info, deviceIndex++, &DeviceInfoData))
{
SP_INTERFACE_DEVICE_DATA data;
data.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

int interfaceIndex = 0;


while (SetupDiEnumDeviceInterfaces(info, &DeviceInfoData, &guid, interfaceIndex++, &data))
{

//https://msdn.microsoft.com/en-us/library/windows/hardware/ff551120%28v=vs.85%29.aspx
//Get the required buffer size. Call SetupDiGetDeviceInterfaceDetail with a NULLDeviceInterfaceDetailData pointer,
//a DeviceInterfaceDetailDataSize of zero, and a valid RequiredSize variable. In response to such a call, this function
//returns the required buffer size at RequiredSize and fails with GetLastError returning ERROR_INSUFFICIENT_BUFFER.

SP_DEVICE_INTERFACE_DETAIL_DATA interfaceData;
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

DWORD bufferSize = 0;
SetupDiGetDeviceInterfaceDetail(info, &data, NULL, 0, &bufferSize, nullptr);

if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
//Call the function again
SetupDiGetDeviceInterfaceDetail(info, &data, &interfaceData, bufferSize, NULL, &DeviceInfoData);

DWORD error = GetLastError();
if (error != ERROR_SUCCESS)
{
printf("Could not obtain device interface details. Error: %d \n", error);
}
}
}

我得到的错误是:

    Run-Time Check Failure #2 - Stack around the variable 'DeviceInfoData' was corrupted.

虽然我已经看到 SP_INTERFACE_DEVICE_DATASP_DEVICE_INTERFACE_DETAIL_DATA 导致相同的错误

非常感谢任何帮助!

最佳答案

看来您的 interfaceData 缓冲区太小了。

检查 the documentation再次将 DeviceInterfaceDetailData 参数传递给 SetupDiGetDeviceInterfaceDetail

关于c++ - SetupDiXxx 结构的堆栈损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619910/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com