/
/
ListDevice.cpp : 此文件包含
"main"
函数。程序执行将在此处开始并结束。
/
/
int
enum_usb_device_info()
{
int
i
=
0
;
int
res
=
0
;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData
=
{ sizeof(DeviceInfoData) };
/
/
get device
class
information handle
hDevInfo
=
SetupDiGetClassDevs(&GUID_DEVCLASS_DISKDRIVE,
0
,
0
, DIGCF_PRESENT );
if
(hDevInfo
=
=
INVALID_HANDLE_VALUE)
{
res
=
GetLastError();
return
res;
}
/
/
enumerute device information
DWORD required_size
=
0
;
for
(i
=
0
; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i
+
+
)
{
DWORD DataT;
char friendly_name[
2046
]
=
{
0
};
DWORD buffersize
=
2046
;
DWORD req_bufsize
=
0
;
char DeviceBuf[
4096
]
=
{
0
};
char DeviceParent[
4096
]
=
{
0
};
auto res
=
CM_Get_Device_IDA(DeviceInfoData.DevInst, DeviceBuf,
4096
,
0
);
if
(!res)
{
DEVNODE pdnDevInst
=
0
;
CM_Get_Parent(&pdnDevInst, DeviceInfoData.DevInst,
0
);
CM_Get_Device_IDA(pdnDevInst, DeviceParent,
4096
,
0
);
if
(strstr(DeviceParent,
"USB"
)!
=
nullptr)
{
ULONG pulStatus
=
0
;
ULONG pulProblemNumber
=
0
;
res
=
CM_Get_DevNode_Status(&pulStatus, &pulProblemNumber, pdnDevInst,
0
);
if
(!res)
{
SP_PROPCHANGE_PARAMS propChange
=
{ sizeof(SP_CLASSINSTALL_HEADER) };
propChange.ClassInstallHeader.InstallFunction
=
DIF_PROPERTYCHANGE;
propChange.Scope
=
DICS_FLAG_GLOBAL;
propChange.StateChange
=
DICS_DISABLE;
res
=
SetupDiSetClassInstallParams
(
hDevInfo,
&DeviceInfoData,
(SP_CLASSINSTALL_HEADER
*
)&propChange,
sizeof(propChange)
);
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData);
}
}
}
/
/
get device description information
if
(!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_SERVICE, &DataT, (LPBYTE)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
;
}
int
main()
{
enum_usb_device_info();
std::cout <<
"Hello World!\n"
;
}