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端口代码实现的更多相关文章

  1. linux 检测远程端口是否打开

    linux 检测远程端口是否打开   检测远程端口是否打开   常用telnet 110.101.101.101 80方式测试远程主机端口是否打开.   除此之外还可以使用:   方法1.nmap i ...

  2. Dlib库中实现正脸人脸关键点(landmark)检测的测试代码

    Dlib库中提供了正脸人脸关键点检测的接口,这里参考dlib/examples/face_landmark_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸关键点检测的 ...

  3. keepalived使用nc命令检测udp端口

    keepalived支持的健康检测方式有:HTTP_GET|SSL_GET.TCP_CHECK.SMTP_CHECK.MISC_CHECK. 由于keepalived自身并不支持udp检测,有TCP_ ...

  4. Dlib库中实现正脸人脸检测的测试代码

    Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...

  5. shell-006:检测80端口的存活情况

    注意细节问题,如下图所示 #!/bin/bash # 检测80端口是否存在 while : do n=`netstat -lnpt |grep ':80 ' |wc -l` if [ $n -eq ] ...

  6. C#如何检测网络端口连接的状态

    原文:C#如何检测网络端口连接的状态 C#如何检测/监控远程连接网络端口的情况(例如:3389端口是否处于监听状态,是否建立了连接等). using System; using System.Coll ...

  7. Mask R-CNN用于目标检测和分割代码实现

    Mask R-CNN用于目标检测和分割代码实现 Mask R-CNN for object detection and instance segmentation on Keras and Tenso ...

  8. 心跳机制tcp keepalive的讨论、应用及“断网”、"断电"检测的C代码实现(Windows环境下)

    版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权. https://blog.csdn.net/stpeace/article/details/441 ...

  9. Python批量检测服务器端口可用性与Socket函数使用

    socket函数 简述 socket又称套间字或者插口,是网络通信中必不可少的工具.有道是:"无socket,不网络".由于socket最早在BSD Unix上使用,而Unix/L ...

随机推荐

  1. Mysql学习---索引的学习 180101

    索引:约束 + 快速查找 索引是数据库中用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可. 问:为什么索引可以这么快?[类似 ...

  2. Foj 2296 Alice and Bob(博弈、搜索)

    Foj 2296 Alice and Bob 题意 两个人博弈,规则如下:轮流取0~9中的数字,最后Alice所得的数字个数为1~n中,数位在Alice所取集合中出现奇数次的. 双方想获得尽量多,问A ...

  3. codeforces 633E Startup Funding(浮点数处理)

    codeforces 633E Startup Funding 题意 枚举左端点,对于每个左端点求一个最大的右端点使得最大. 对于得到的这个数组,随机选择k个数,求最小值期望. 题解 对于每个左端点, ...

  4. Maximum Flow and Minimum Cut

    最大流最小割 Introduction Mincut Problem 最小割问题,输入是带权有向图,有一个源点 s(source)和一个汇点 t(target),边的权重在这里称作容量(capacit ...

  5. Chapter 3 Top 10 List

    3.1 Introduction Given a set of (key-as-string, value-as-integer) pairs, then finding a Top-N ( wher ...

  6. 单例模式中的volatile关键字

    在之前学习了单例模式在多线程下的设计,疑惑为何要加volatile关键字.加与不加有什么区别呢?这里我们就来研究一下.单例模式的设计可以参考个人总结的这篇文章   背景:在早期的JVM中,synchr ...

  7. VS2013打开项目 提示Asp.net4.5未在web服务器上注册 F5运行 启动不来 权限

    打一个补丁 http://blogs.msdn.com/b/webdev/archive/2014/11/11/dialog-box-may-be-displayed-to-users-when-op ...

  8. 015.1 Lock接口

    内容:Lock接口使用步骤,同步生产大白兔奶糖的例子 同步代码块的锁是隐式的,显式容易让我们理解.所以我们使用这个显式的方法,方便理解代码.######实现同步步骤:1.获取锁:lock()2.同步代 ...

  9. 环信REST API python SDK

    今天鼓起勇气,决定把这个贡献出来.不敢误人子弟,也一直担心,在不良质量的产品会祸害人,但自己已经使用,它本身也没技术. 平庸的代码,高效地实用,为环信贡献点力,如有问题,欢迎斧正,一起学习,一起成长, ...

  10. Java中this关键字使用小结

    当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this. 因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this ...