C/C++枚举设备管理器上显示的物理串口(能够枚举出USB转串口等)

      调用EnumPort API可以获取到串口,并口,TCP/IP打印端口等。不过该API获取到的串口有很多不是真实的物理串口。要获取物理串口,我们可以通过串口设备类型的GUID获取设备管理器中的实际串口。如下是实现方式:

      

BOOL WcharIsDigit(WCHAR ch)
{
	if(ch >= L'0' && ch <= L'9')
	{
		return TRUE;
	}

	return FALSE;
}

// The following define is from ntddser.h in the DDK. It is also
// needed for serial port enumeration.
#ifndef GUID_CLASS_COMPORT
DEFINE_GUID(GUID_CLASS_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, \
			0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73);
#endif

BOOL EnumPortsWdm()
{
	// Create a device information set that will be the container for 
	// the device interfaces.
	GUID *guidDev = (GUID*) &GUID_CLASS_COMPORT;
	HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
	SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;

	hDevInfo = SetupDiGetClassDevs( guidDev,
		NULL,
		NULL,
		DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
		);

	if(INVALID_HANDLE_VALUE == hDevInfo) 
	{
		return FALSE;
	}

	// Enumerate the serial ports
	BOOL bOk = TRUE;
	SP_DEVICE_INTERFACE_DATA ifcData;
	DWORD dwDetDataSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + 256;
	pDetData = (SP_DEVICE_INTERFACE_DETAIL_DATA*)new char[dwDetDataSize];
	if(!pDetData)
	{
		return FALSE;
	}
	// This is required, according to the documentation. Yes,
	// it's weird.
	ifcData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
	pDetData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
	for (DWORD ii=0; bOk; ii++) 
	{
		bOk = SetupDiEnumDeviceInterfaces(hDevInfo,
			NULL, guidDev, ii, &ifcData);
		if (bOk) 
		{
			// Got a device. Get the details.
			SP_DEVINFO_DATA devdata = {sizeof(SP_DEVINFO_DATA)};
			bOk = SetupDiGetDeviceInterfaceDetail(hDevInfo,
				&ifcData, pDetData, dwDetDataSize, NULL, &devdata);
			if (bOk) {
				// Got a path to the device. Try to get some more info.
				WCHAR fname[256] = {0};
				WCHAR desc[256] = {0};
				BOOL bSuccess = SetupDiGetDeviceRegistryProperty(
					hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL,
					(PBYTE)fname, sizeof(fname), NULL);
				bSuccess = bSuccess && SetupDiGetDeviceRegistryProperty(
					hDevInfo, &devdata, SPDRP_DEVICEDESC, NULL,
					(PBYTE)desc, sizeof(desc), NULL);
				BOOL bUsbDevice = FALSE;
				WCHAR locinfo[256] = {0};
				if (SetupDiGetDeviceRegistryProperty(
					hDevInfo, &devdata, SPDRP_LOCATION_INFORMATION, NULL,
					(PBYTE)locinfo, sizeof(locinfo), NULL))
				{
					// Just check the first three characters to determine
					// if the port is connected to the USB bus. This isn't
					// an infallible method; it would be better to use the
					// BUS GUID. Currently, Windows doesn't let you query
					// that though (SPDRP_BUSTYPEGUID seems to exist in
					// documentation only).
					bUsbDevice = (wcsncmp(locinfo,L"USB", 3)==0);
				}
				if (bSuccess)
				{
					printf("FriendlyName = %S\r\n",fname);
					printf("Port Desc = %S\r\n",desc);
				}

			}
			else 
			{
				if (pDetData != NULL)
				{
					delete [] (char*)pDetData;
				}
				if (hDevInfo != INVALID_HANDLE_VALUE)
				{
					SetupDiDestroyDeviceInfoList(hDevInfo);
				}
				return FALSE;
			}
		}
		else 
		{
			DWORD err = GetLastError();
			if (err != ERROR_NO_MORE_ITEMS) 
			{
				if (pDetData != NULL)
				{
					delete [] (char*)pDetData;
				}
				if (hDevInfo != INVALID_HANDLE_VALUE)
				{
					SetupDiDestroyDeviceInfoList(hDevInfo);
				}
				return FALSE;
			}
		}
	}

	if (pDetData != NULL)
	{
		delete [] (char*)pDetData;
	}
	if (hDevInfo != INVALID_HANDLE_VALUE)
	{
		SetupDiDestroyDeviceInfoList(hDevInfo);
	}

	return TRUE;
}

int main(int argc,char* argv[])
{
	setlocale(LC_ALL,"chs");
	EnumPortsWdm();

	getchar();
	return 0;
}
       测试运行如下:

        


        如上,为了printf函数可以打印出中文,这里调用了 setlocale(LC_ALL,"chs") 函数即可正常打印出中文。

      

虽然简单,但是实用 HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i,j; int ret=0; hDevInfo = SetupDiGetClassDevs((LPGUID) &GUID;_DEVCLASS_MOUSE, 0, 0,DIGCF_PRESENT);//DIGCF_PROFILE);// /*   GUID_DEVCLASS_FDC软盘控制器   GUID_DEVCLASS_DISPLAY显示卡   GUID_DEVCLASS_CDROM光驱   GUID_DEVCLASS_KEYBOARD键盘   GUID_DEVCLASS_COMPUTER计算机   GUID_DEVCLASS_SYSTEM系统   GUID_DEVCLASS_DISKDRIVE磁盘驱动器   GUID_DEVCLASS_MEDIA声音、视频和游戏控制器   GUID_DEVCLASS_MODEMMODEM   GUID_DEVCLASS_MOUSE鼠标和其他指针设备   GUID_DEVCLASS_NET网络设备器   GUID_DEVCLASS_USB通用串行总线控制器   GUID_DEVCLASS_FLOPPYDISK软盘驱动器   GUID_DEVCLASS_UNKNOWN未知设备   GUID_DEVCLASS_SCSIADAPTERSCSI 和 RAID 控制器   GUID_DEVCLASS_HDCIDE ATA/ATAPI 控制器   GUID_DEVCLASS_PORTS端口(COM 和 LPT)   GUID_DEVCLASS_MONITOR监视器   */ if (hDevInfo == INVALID_HANDLE_VALUE){ // Insert error handling here. // return ; } // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData;);i++) { DWORD DataT; //LPTSTR buffer = NULL; char buffer[2048]; DWORD buffersize =sizeof(buffer); while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData;, SPDRP_FRIENDLYNAME, &DataT;, (PBYTE)buffer, buffersize, &buffersize;)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. //if (buffer) LocalFree(buffer); //buffer = (PSP_INF_INFORMATION)LocalAlloc(LPTR,buffersize); // <!--[if !supportEmptyParas]--><!--[endif]--> } else { // Insert error handling here. break; } } if (buffer != NULL && i == 0) { // temp.Format(""); // str += temp; } // temp.Format("%s",buffer); // str += temp; if (buffer) LocalFree(buffer); } if (i != 0) { // temp.Format(""); // str += temp; } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { return ; } // <!--[if !supportEmptyParas]--><!--[endif]--> // Cleanup SetupDiDestroyDeviceInfoList(hDevInfo);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值