我有一个打印机连接USB端口,我想了解一些有关它的信息。我正在使用SetupDiEnumDeviceInfo函数从setupapi获取信息。我正在做任何事情,就像MSDN中描述的那样。
#include <string>
#include <windows.h>
#include <vector>
#include <iostream>
#include <setupapi.h>
#include <winerror.h>
#pragma comment (lib, "SetupAPI.lib")
static GUID GUID_DEVCLASS_PORTS = { 0x4d36e978, 0xe325, 0x11ce, 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
int main()
{
SP_DEVINFO_DATA devInfoData;
HDEVINFO deviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD nDevice = 0;
if (SetupDiEnumDeviceInfo(deviceInfo, nDevice, &devInfoData))
{
}
return 0;
}问题是我总是得到false的结果。GetLastError()函数重放259。我做错了什么?
发布于 2019-09-23 09:14:56
这是我的样本。我添加devGuide.h,并使用GUID_DEVCLASS_USB。
#include <string>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#pragma comment (lib, "SetupAPI.lib")
int main()
{
int res = 0;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData = { sizeof(DeviceInfoData) };
// get device class information handle
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_USB, 0, 0, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
res = GetLastError();
return res;
}
// enumerute device information
DWORD required_size = 0;
for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
{
DWORD DataT;
char friendly_name[2046] = { 0 };
DWORD buffersize = 2046;
DWORD req_bufsize = 0;
// get device description information
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_CLASSGUID, &DataT, (PBYTE)friendly_name, buffersize, &req_bufsize))
{
res = GetLastError();
continue;
}
char temp[512] = { 0 };
sprintf_s(temp, 512, "USB device %d: %s", i, friendly_name);
puts(temp);
}
return 0;
}https://stackoverflow.com/questions/58030553
复制相似问题