📄 qerylan.cpp
字号:
#include "..\stdafx.h"
// =============================================================================
// Get LAN Information.
// =============================================================================
#include <Winsock2.h>
#include <Iphlpapi.h>
#pragma comment( lib,"Ws2_32.lib" )
#pragma comment( lib,"Mpr.lib" )
#pragma comment( lib,"iphlpapi.lib" )
BOOL GetSubResource(NETRESOURCE *pContainer, CString &strRet);
void GetRemoteMAC(CString &strIP, CString &strMAC);
BOOL GetLanInfo(CMapStringToString & cMapPCIP)
{
// Enable/Disable [Copy] button.
BOOL bFind = FALSE;
// Clear contents of List Box.
// m_list.ResetContent();
// Waiting...
CWaitCursor wait;
CString strIP = _T("");
CString strTemp = _T("");
CString strMAC = _T("");
CString strSubResource = _T("");
struct hostent *host;
struct in_addr *ptr;
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WSADATA wsaData;
// Initialize Windows Socket Library.
WSAStartup( MAKEWORD( 1,1 ),&wsaData );
WNetOpenEnum( dwScope,NULL,NULL,NULL,&hEnum );
// If we get a valid handle...
if( hEnum ) {
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[ 2048 ];
WNetEnumResource( hEnum,&Count,Buffer,&BufferSize );
NetResource = ( NETRESOURCE * )Buffer;
for( unsigned int i = 0; i < Count; i++, NetResource++ ) {
if( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) {
if( NetResource->lpRemoteName ) {
// Ha: What the Host shared?
{
strSubResource.Empty();
BOOL ret = GetSubResource( NetResource,strSubResource );
}
CString strFullName = NetResource->lpRemoteName;
if( 0 == strFullName.Left( 2 ).Compare( "\\\\" ) ) strFullName = strFullName.Right( strFullName.GetLength() - 2 );
// Get Host
host = gethostbyname( strFullName );
if( host == NULL ) continue;
ptr = ( struct in_addr * )host->h_addr_list[ 0 ];
// Get IP
int a = ptr->S_un.S_un_b.s_b1;
int b = ptr->S_un.S_un_b.s_b2;
int c = ptr->S_un.S_un_b.s_b3;
int d = ptr->S_un.S_un_b.s_b4;
strIP.Format( _T("%d.%d.%d.%d"),a,b,c,d );
/*
GetRemoteMAC( strIP,strMAC );
if( strMAC != _T("MAC") ) {
strTemp.Format( _T("[Host] %s [IP] %3d.%3d.%3d.%3d [MAC] %s"),strFullName,a,b,c,d,strMAC );
}
else {
strTemp.Format( _T("[Host] %s [IP] %3d.%3d.%3d.%3d"),strFullName,a,b,c,d );
}
//*/
//添加计算机项
cMapPCIP.SetAt (strFullName,strIP);
if( !strSubResource.IsEmpty() ) {
int at = 0;
while( 1 ) {
// m_list.UpdateWindow();
at = strSubResource.Find( '?',0 );
if( at == -1 ) break;
strTemp = " |__";
strTemp += strSubResource.Left( at );
// int index = m_list.AddStringEx( strTemp );
// m_list.SetCurSel( index );
strSubResource = strSubResource.Right( strSubResource.GetLength()-at-1 );
}
}
bFind = TRUE;
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
WSACleanup();
if(!bFind)
AfxMessageBox(_T("没有发现"));
return bFind;
}
BOOL GetLanInfo(CStringArray & strArray)
{
// Enable/Disable [Copy] button.
BOOL bFind = FALSE;
// Clear contents of List Box.
// m_list.ResetContent();
// Waiting...
CWaitCursor wait;
CString strIP = _T("");
CString strTemp = _T("");
CString strMAC = _T("");
CString strSubResource = _T("");
struct hostent *host;
struct in_addr *ptr;
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WSADATA wsaData;
// Initialize Windows Socket Library.
WSAStartup( MAKEWORD( 1,1 ),&wsaData );
WNetOpenEnum( dwScope,NULL,NULL,NULL,&hEnum );
// If we get a valid handle...
if( hEnum ) {
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[ 2048 ];
WNetEnumResource( hEnum,&Count,Buffer,&BufferSize );
NetResource = ( NETRESOURCE * )Buffer;
for( unsigned int i = 0; i < Count; i++, NetResource++ ) {
if( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY ) {
if( NetResource->lpRemoteName ) {
// Ha: What the Host shared?
{
strSubResource.Empty();
BOOL ret = GetSubResource( NetResource,strSubResource );
}
CString strFullName = NetResource->lpRemoteName;
if( 0 == strFullName.Left( 2 ).Compare( "\\\\" ) ) strFullName = strFullName.Right( strFullName.GetLength() - 2 );
// Get Host
host = gethostbyname( strFullName );
if( host == NULL ) continue;
ptr = ( struct in_addr * )host->h_addr_list[ 0 ];
// Get IP
int a = ptr->S_un.S_un_b.s_b1;
int b = ptr->S_un.S_un_b.s_b2;
int c = ptr->S_un.S_un_b.s_b3;
int d = ptr->S_un.S_un_b.s_b4;
strIP.Format( _T("%d.%d.%d.%d"),a,b,c,d );
GetRemoteMAC( strIP,strMAC );
if( strMAC != _T("MAC") ) {
strTemp.Format( _T("[Host] %s [IP] %3d.%3d.%3d.%3d [MAC] %s"),strFullName,a,b,c,d,strMAC );
}
else {
strTemp.Format( _T("[Host] %s [IP] %3d.%3d.%3d.%3d"),strFullName,a,b,c,d );
}
//添加计算机项
strArray.Add(strTemp);
// Add to List Box.
// int index = m_list.AddStringEx( strTemp );
// m_list.SetCurSel( index );
if( !strSubResource.IsEmpty() ) {
int at = 0;
while( 1 ) {
// m_list.UpdateWindow();
at = strSubResource.Find( '?',0 );
if( at == -1 ) break;
strTemp = " |__";
strTemp += strSubResource.Left( at );
// int index = m_list.AddStringEx( strTemp );
// m_list.SetCurSel( index );
strSubResource = strSubResource.Right( strSubResource.GetLength()-at-1 );
}
}
bFind = TRUE;
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
WSACleanup();
if(!bFind)
AfxMessageBox(_T("没有发现"));
return bFind;
}
void GetRemoteMAC(CString &strIP, CString &strMAC)
{
HRESULT hr;
IPAddr ipAddr;
ULONG pulMac[ 2 ];
ULONG ulLen;
CString tmp0,tmp1;
strMAC = _T("MAC");
ipAddr = inet_addr( ( char * )( LPCTSTR )strIP );
memset( pulMac,0xff,sizeof( pulMac ) );
ulLen = 6;
hr = SendARP( ipAddr,0,pulMac,&ulLen );
if( NO_ERROR != hr || ulLen == 0 ) return;
ULONG i;
char * szMac = new char[ ulLen * 3 ];
PBYTE pbHexMac = ( PBYTE )pulMac;
// Convert the binary MAC address into human-readable
for( i = 0; i < ulLen - 1; ++ i ) {
tmp0.Format( "%02X:",pbHexMac[ i ] );
tmp1 += tmp0;
}
tmp0.Format( "%02X",pbHexMac[ i ] );
tmp1 += tmp0;
delete [] szMac;
strMAC = tmp1;
}
BOOL GetSubResource(NETRESOURCE *pContainer, CString &strRet)
{
NETRESOURCE NetResource;
NETRESOURCE *pSubNetResource = NULL;
HANDLE hEnum = NULL;
DWORD ret;
CopyMemory( ( PVOID )&NetResource,( PVOID )pContainer,sizeof( NETRESOURCE ) );
NetResource.dwScope = RESOURCE_GLOBALNET;
NetResource.dwType = RESOURCETYPE_ANY;
NetResource.dwUsage = RESOURCEUSAGE_CONTAINER;
ret = WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_ANY,0,&NetResource,&hEnum );
if( NO_ERROR != ret ) return FALSE;
if( hEnum ) {
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[ 2048 ];
ret = WNetEnumResource( hEnum,&Count,Buffer,&BufferSize );
pSubNetResource = ( NETRESOURCE * )Buffer;
if( NO_ERROR == ret ) {
CString tmp;
for( unsigned int i = 0; i < Count; i++, pSubNetResource++ ) {
if( pSubNetResource->lpRemoteName ) {
tmp.Format( "%s?",pSubNetResource->lpRemoteName );
strRet += tmp;
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -