检测com端口代码实现
1:scan
HRESULT CDevHound::Scan(const vector<CString> &guiInfo, vector<DEV_INFO> &vPortInfo) // scan port
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
DEV_INFO epi; // Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVCLASS_PORTS,
0, // Enumerator
0,
DIGCF_PRESENT); if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
} // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for(DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData); i++)
{ memset( &epi,0,sizeof(epi) );
int msgType = WM_DEV_MSG;
TCHAR szDIS[MAX_PATH] = {0};
DWORD nsize = 0;
memset( &szDIS, 0, MAX_PATH);
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
memset( &epi,0,sizeof(epi) );
if(FALSE==m_bWork)
{
break;
}
//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
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 = (LPTSTR)LocalAlloc(LPTR,buffersize);
}
else
{
// Insert error handling here.
break;
}
}
SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL,(PBYTE)epi.szFriendlyName, DEV_NAME_MAX_LEN*sizeof(TCHAR), NULL);
// Get port description from registry
SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL,(PBYTE)epi.szDescription, DEV_NAME_MAX_LEN*sizeof(TCHAR), NULL); HKEY hDevKey = SetupDiOpenDevRegKey( hDevInfo,&DeviceInfoData,DICS_FLAG_GLOBAL,0,DIREG_DEV,KEY_READ );
if( INVALID_HANDLE_VALUE != hDevKey )
{
// Get port name
DWORD dwCount = 256;
RegQueryValueEx( hDevKey,_T( "PortName" ),NULL,NULL,(BYTE*)epi.szPortName,&dwCount );
RegCloseKey( hDevKey );
} // TCHAR szDIS[MAX_PATH] = {0};
// DWORD nsize = 0;
BOOL bGet = SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, szDIS, sizeof(szDIS), &nsize);
if(TRUE==bGet)
{
tstring strid(szDIS);
tstring strchar(_T("\\"));
std::size_t pos = strid.rfind(_T("\\"));
strchar = strid.substr(0,pos);
if(GuidInfoFind(strchar, msgType,guiInfo))
{
pos = strid.find_first_of('&');
_tcscpy(epi.szVid,strid.substr(0,pos).c_str());
pos = strid.rfind(_T("\\"));
strchar = strid.substr(pos+1);
_tcscpy(epi.szGUID,strchar.c_str());
InsertPortInfo(epi, false, msgType,vPortInfo);
}
else
{
continue;
}
} } if(hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
hDevInfo = INVALID_HANDLE_VALUE;
} return S_OK;
}
2:detect for one type
UINT CDevHound::HoundThreadProc() //detect for one type port
{
GUID *guidDev = (GUID*)&GUID_CLASS_PORT;
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE; // Get Port class set
// Note:We use DIGCF_PRESENT flag,so maybe you can see
// some ports on the device manager,but they are not
// enumerated by SetupDiEnumDeviceInterfaces in the do-while
// loop,because their driver are disabled,no application
// can open and use them. hDevInfo = SetupDiGetClassDevs( guidDev,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
); if(hDevInfo == INVALID_HANDLE_VALUE)
{
return 0;
} SP_DEVICE_INTERFACE_DATA ifcData;
ifcData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); DWORD dwIndex = 0; DEV_INFO epi; bool bInit = true; // Enumerate port and modem class device interfaces
do
{
memset( &epi,0,sizeof(epi) ); if( SetupDiEnumDeviceInterfaces(hDevInfo,NULL, guidDev, dwIndex, &ifcData) )
{
SP_DEVINFO_DATA devData;
devData.cbSize = sizeof( SP_DEVINFO_DATA ); if( !SetupDiGetDeviceInterfaceDetail( hDevInfo,&ifcData,NULL,0,NULL,&devData ) )
{
if( ERROR_INSUFFICIENT_BUFFER != GetLastError() )
{
// Can not get detail interface info
continue;
}
} // Get Friendly name from registry
SetupDiGetDeviceRegistryProperty( hDevInfo, &devData, SPDRP_FRIENDLYNAME, NULL,(PBYTE)epi.szFriendlyName, DEV_NAME_MAX_LEN*sizeof(TCHAR), NULL);
// Get port description from registry
SetupDiGetDeviceRegistryProperty( hDevInfo, &devData, SPDRP_DEVICEDESC, NULL,(PBYTE)epi.szDescription, DEV_NAME_MAX_LEN*sizeof(TCHAR), NULL);
// Get class name from registry
//TCHAR szClass[EPI_MAX_LEN];
//SetupDiGetDeviceRegistryProperty( hDevInfo, &devData, SPDRP_CLASS, NULL,(PBYTE)szClass, EPI_MAX_LEN*sizeof(TCHAR), NULL);
//epi.nType = CheckDeviceClass( szClass ); HKEY hDevKey = SetupDiOpenDevRegKey( hDevInfo,&devData,DICS_FLAG_GLOBAL,0,DIREG_DEV,KEY_READ );
if( INVALID_HANDLE_VALUE != hDevKey )
{
// Get port name
DWORD dwCount = DEV_NAME_MAX_LEN;
RegQueryValueEx( hDevKey,_T( "PortName" ),NULL,NULL,(BYTE*)epi.szPortName,&dwCount );
RegCloseKey( hDevKey );
} // Insert to port info array
TCHAR szDIS[MAX_PATH] = {0};
DWORD nsize = 0;
BOOL bGet = SetupDiGetDeviceInstanceId(hDevInfo, &devData, szDIS, sizeof(szDIS), &nsize);
if(TRUE==bGet)
{
tstring strid(szDIS);
tstring strchar(_T("\\"));
std::size_t pos = strid.rfind(_T("\\"));
std::size_t posbegin = strid.find_first_of('\\');
strchar = strid.substr(0,pos);
dwIndex++;
USES_CONVERSION;
if(tstring::npos != strchar.find(_T("USB\\VID_1782&PID_4D00")))
{
strchar = strchar.substr(posbegin+1,pos);
_tcscpy(epi.szVid,strchar.c_str());
pos = strid.rfind(_T("\\"));
strchar = strid.substr(pos+1);
tstring strcominfo(epi.szPortName);
tstring strcominfo2 = boost::to_lower_copy(strcominfo);
boost::erase_first(strcominfo2, "com");
_tcscpy(epi.szGUID,strcominfo2.c_str());
#if 0
string strlog;
ofstream out("d:\\scanport.log",ios::app);
strlog = convert<string>(bInit);
out <<"init="<< strlog << "\n";
out << W2A(epi.szDescription) << "\n";
out << W2A(epi.szFriendlyName)<< "\n";
#endif
for(size_t nthreadx=0; nthreadx<m_guidvec.size(); nthreadx++)
{
if(m_guidvec[nthreadx]==strcominfo2)
{
InsertPortInfo( epi,nthreadx, bInit);
#ifdef _WRITE_PORT_LOGFILE
string strthrdxlog;
string strinitlog;
strthrdxlog = convert<string>(nthreadx);
ofstream out("d:\\scanport2.log",ios::app);
strinitlog = convert<string>(bInit);
out <<"init="<< strinitlog << "\n";
out << "thread id= " << strthrdxlog << "\n";
#endif }
}
} }
}
else
{
if( ERROR_NO_MORE_ITEMS != GetLastError() )
{
dwIndex++;
continue;
} dwIndex = 0;
bInit = false;
ClearState();
Sleep(1); SetupDiDestroyDeviceInfoList(hDevInfo);
hDevInfo = INVALID_HANDLE_VALUE;
hDevInfo = SetupDiGetClassDevs( guidDev,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
); memset(&ifcData,0,sizeof(SP_DEVICE_INTERFACE_DATA));
ifcData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
} }while( m_bWork && (hDevInfo != INVALID_HANDLE_VALUE) ); if(hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
hDevInfo = INVALID_HANDLE_VALUE;
} return 0;
}
检测com端口代码实现的更多相关文章
- linux 检测远程端口是否打开
linux 检测远程端口是否打开 检测远程端口是否打开 常用telnet 110.101.101.101 80方式测试远程主机端口是否打开. 除此之外还可以使用: 方法1.nmap i ...
- Dlib库中实现正脸人脸关键点(landmark)检测的测试代码
Dlib库中提供了正脸人脸关键点检测的接口,这里参考dlib/examples/face_landmark_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸关键点检测的 ...
- keepalived使用nc命令检测udp端口
keepalived支持的健康检测方式有:HTTP_GET|SSL_GET.TCP_CHECK.SMTP_CHECK.MISC_CHECK. 由于keepalived自身并不支持udp检测,有TCP_ ...
- Dlib库中实现正脸人脸检测的测试代码
Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...
- shell-006:检测80端口的存活情况
注意细节问题,如下图所示 #!/bin/bash # 检测80端口是否存在 while : do n=`netstat -lnpt |grep ':80 ' |wc -l` if [ $n -eq ] ...
- C#如何检测网络端口连接的状态
原文:C#如何检测网络端口连接的状态 C#如何检测/监控远程连接网络端口的情况(例如:3389端口是否处于监听状态,是否建立了连接等). using System; using System.Coll ...
- Mask R-CNN用于目标检测和分割代码实现
Mask R-CNN用于目标检测和分割代码实现 Mask R-CNN for object detection and instance segmentation on Keras and Tenso ...
- 心跳机制tcp keepalive的讨论、应用及“断网”、"断电"检测的C代码实现(Windows环境下)
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权. https://blog.csdn.net/stpeace/article/details/441 ...
- Python批量检测服务器端口可用性与Socket函数使用
socket函数 简述 socket又称套间字或者插口,是网络通信中必不可少的工具.有道是:"无socket,不网络".由于socket最早在BSD Unix上使用,而Unix/L ...
随机推荐
- 使用LR的socket协议进行进行性能测试,转解决方案
在用LR对公司delphi开发的C/S程序进行测试时,发现只有选择socket协议可以录制代码,经研究是通过TCP/IP的方式将参数保存在buffer中发送的方式来完成操作,但由于将buffer内容参 ...
- 51nod 1952 栈
题目链接戳这 如果只是从尾端插入,那么问题就是基础的:求取栈内最大值的问题,这用单调栈解决即可. 但是前端也能插入,一般的单调栈已经不能满足.那么想象,如果在前端插入一个小值,相当于在栈底多加一个值罢 ...
- 如何恢复在Windows 10中被永久删除的照片?
照片被误删除了需要恢复?这里推荐一款软件:winutilities.使用WinUtilities文件恢复向导允许您通过简单的点击恢复已删除的照片或从Windows 10回收站中恢复被删除的照片. 恢复 ...
- Linux下安装PHP并在nginx服务器中进行配置的详细方法
先介绍一下使用的环境:centos 7.4, PHP 7.0 , nginx 1.12 Linux系统版本可以通过命令:lsb_release -a 查看. 现在开始步入正题了! 1. 首先查看一下 ...
- LayIM.AspNetCore Middleware 开发日记(六)嵌入资源的使用,layim.config的封装
前言 距离上一篇博客竟然已经10多天了...工作上的事,个人原因,种种吧.不多说废话,本文将会重点介绍layim的入口配置. LayIM配置 其实在开发者文档里面已经描述的很清楚了.除了几个重要的接口 ...
- ethersjs运行时出现的问题
1.在运行部署在ganache1.1.0上的合约中的某个函数时,出现了这样的问题,之前部署在geth上时并没有这样的问题 userdeMacBook-Pro:test-ethers user$ nod ...
- iOS 二维码生成 记录一下
#import <CoreImage/CoreImage.h> @property (nonatomic, nonnull , strong) UIImageView *showImage ...
- Bluetooth® Low Energy Beacons
Bluetooth® Low Energy Beacons ABSTRACT (abstract ) 1.This application report presents the concept of ...
- 基于.net core 微服务的另类实现
基于.net core 的微服务,网上很多介绍都是千篇一律基于类似webapi,通过http请求形式进行访问,但这并不符合大家使用习惯.如何像形如[ GetService<IOrderServi ...
- Android程序项目结构(二)
利用Android Studio创建完第一个Hello World项目后,我们会看到使用project模式的项目结构. 一..gradle和.idea 这两个目录放置的是Android Studio自 ...