c++ - 使用 SetupDiGetDeviceRegistryPropertyW 获取所需大小的错误代码 122
问题描述
我想通过使用 Setup API 来获取 USB 记忆棒的设备 ID,但首先我想了解一些我必须使用的功能。文档说我可以将SetupDiGetDeviceRegistryProperty
NULL 发送到缓冲区和缓冲区大小以获得所需的大小,但我收到122
错误代码,这意味着:
传递给系统调用的数据区域太小
谁能告诉我我做错了什么?
到目前为止,这是我的代码:
#include <Windows.h>
#include <wchar.h>
#include <SetupAPI.h>
#pragma comment(lib, "Setupapi.lib")
int wmain(int argc, wchar_t *arv[])
{
// SetupDiGetClassDevs
HDEVINFO hDeviceInfo;
DWORD filterDevInfo = DIGCF_ALLCLASSES;
hDeviceInfo = SetupDiGetClassDevsW(NULL, NULL, NULL, filterDevInfo);
if (hDeviceInfo != INVALID_HANDLE_VALUE)
{
// SetupDiEnumDeviceInfo
DWORD memberIndex = 0;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
if (!SetupDiEnumDeviceInfo(
hDeviceInfo,
memberIndex,
&devInfoData))
{
fwprintf(stderr, L"Error on enum: %u\n", GetLastError());
return 1;
}
// SetupDiGetDeviceRegistryProperty
DWORD propertyRetrieved = SPDRP_DEVICEDESC;
DWORD requiredSize;
// First call to get the required size for the buffer
if (!SetupDiGetDeviceRegistryPropertyW(
hDeviceInfo,
&devInfoData,
propertyRetrieved,
NULL,
NULL,
0,
&requiredSize))
{
fwprintf(stderr, L"Error on registry property: %u\n", GetLastError());
return 1;
}
}
else
{
wprintf(L"Error code: %u\n", GetLastError());
return 1;
}
return 0;
}
解决方案
推荐阅读
- android - 我应该让 LiveData 或 Repository 类的 AsyncTask 成员 - 作为 Loader 的替代品
- python - Python中的多列编码
- javascript - 在 React 中显示模态的最佳方式
- mysql - 在 SQL 的 SELECT 语句中组合 CONCAT
- database - odoo 域搜索“id 中的 id”
- umbraco - Umbraco 无头 Node.js 客户端无法验证无头客户端
- bash - 无法弄清楚我如何在 Ubuntu 中设置我的 PYTHONPATH
- java - 找不到 Mvn 命令?
- python - Jupyter Notebook:使用 ipywidgets 时使用的重复散点图
- angularjs - ng-show 在表单中不起作用