//具有完整的设备名
#include <windows.h>
#include <devguid.h>
#include <setupapi.h>
#pragma comment(lib, "Setupapi.lib")
void main()
{
HDEVINFO hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, 0);
if(hDevInfo)
{
SP_DEVINFO_DATA SpDevInfo={sizeof(SP_DEVINFO_DATA)};
for(DWORD iDevIndex=0; SetupDiEnumDeviceInfo(hDevInfo, iDevIndex, &SpDevInfo); iDevIndex++)
{
TCHAR szName[512] = {0};
if(SetupDiGetDeviceRegistryProperty(hDevInfo, &SpDevInfo, SPDRP_FRIENDLYNAME,
NULL, (PBYTE)szName, sizeof(szName), NULL))
{
_tprintf(_T("%s\n"), szName);
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
}
调试输出
USB Serial Port (COM3)
Xenon 1900 Area-Imaging Scanner (COM4)
Xenon 1900 Area-Imaging Scanner (COM5)