获取系统的com口

来源:互联网 发布:win10配置java 编辑:程序博客网 时间:2024/05/20 00:17
#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;  }  

原创粉丝点击