获取系统的com口

#include "StdAfx.h"

#include "windows.h"
#include <iostream>
#include <string>
#include <Setupapi.h>
#pragma comment(lib,"setupapi")

using namespace std;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  

int  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)  
				{  
					/*if (NULL!=strstr(fname,""))
					{
					return fname;

					}*/
					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;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值