📄 xlaninfodlg.cpp
字号:
// xLANInfoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "xLANInfo.h"
#include "xLANInfoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CXLANInfoDlg dialog
CXLANInfoDlg::CXLANInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXLANInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CXLANInfoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_IsTTP = FALSE;
}
void CXLANInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CXLANInfoDlg)
DDX_Control(pDX, IDC_LIST_LANINFO, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CXLANInfoDlg, CDialog)
//{{AFX_MSG_MAP(CXLANInfoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_COPY_LANINFO, OnCopyLanInfo)
ON_BN_CLICKED(IDC_GET_LANINFO, OnGetLanInfo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CXLANInfoDlg message handlers
#include "xLANInfo.h"
BOOL CXLANInfoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, TRUE); // Set small icon
// TODO: Add extra initialization here
( ( CXLANInfoApp* )AfxGetApp() )->dwBaseUnits = GetDialogBaseUnits();
m_list.AddStringEx( _T("Click [Refresh] button to get LAN information,") );
m_list.AddStringEx( _T("Perhaps you must wait a second...") );
m_tip.Create( this );
m_tip.AddTool( this,_T("xLANInfo v1.00 - (C)2003 YangTze presents.") );
m_tip.AddTool( GetDlgItem( IDC_LIST_LANINFO ),_T("Ha: I'm YangTze!") );
m_tip.Activate( TRUE );
m_IsTTP = TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CXLANInfoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CXLANInfoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CXLANInfoDlg::OnCopyLanInfo()
{
CString str,tmp;
str = _T("The LAN Information:\r\n");
str += _T("==========================================================================================\r\n");
for( int i = 0; i < m_list.GetCount(); i++ ) {
m_list.GetText( i,tmp );
str = str + tmp + _T("\r\n");
}
str += _T("==========================================================================================\r\n");
str += _T(" xLANInfo v1.00 - (C)2003 YangTze presents\r\n");
HGLOBAL hglbCopy;
LPTSTR pCopyStr;
int cch = str.GetLength() + 1;
hglbCopy = GlobalAlloc(GMEM_MOVEABLE,cch * sizeof( TCHAR ) );
pCopyStr = ( LPTSTR )GlobalLock( hglbCopy );
if( hglbCopy == NULL ) {
AfxMessageBox( _T("Cannot allocate memory!") );
return;
}
lstrcpy( pCopyStr,str );
pCopyStr[ cch ] = ( TCHAR )0;
GlobalUnlock( hglbCopy );
if( !OpenClipboard() ) {
AfxMessageBox( _T("Cannot open the Clipboard!") );
return;
}
// Remove the current Clipboard contents
if( !EmptyClipboard() ) {
AfxMessageBox( _T("Cannot empty the Clipboard!") );
return;
}
if( ::SetClipboardData( CF_TEXT,hglbCopy ) == NULL ) {
AfxMessageBox( _T("Unable to set Clipboard data") );
CloseClipboard();
return;
}
CloseClipboard();
AfxMessageBox( _T("The LAN Information has been copied to Clipboard!") );
}
// =============================================================================
// 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" )
void CXLANInfoDlg::OnGetLanInfo()
{
// Enable/Disable [Copy] button.
BOOL bCopyList = FALSE;
// Clear contents of List Box.
m_list.ResetContent();
// Waiting...
CWaitCursor wait;
CString strTemp = _T("");
CString strTemp1 = _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++ ) {
m_list.UpdateWindow();
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;
strTemp.Format( _T("%d.%d.%d.%d"),a,b,c,d );
GetRemoteMAC( strTemp,strTemp1 );
if( strTemp1 != _T("Hello,YangTze!") ) {
strTemp.Format( _T("[IP] %3d.%3d.%3d.%3d [MAC] %s [Host] %s"),a,b,c,d,strTemp1,strFullName );
}
else {
strTemp.Format( _T("[IP] %3d.%3d.%3d.%3d [Host] %s"),a,b,c,d,strFullName );
}
// 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 );
}
}
bCopyList = TRUE;
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
WSACleanup();
CButton *pCopyListBtn = ( CButton * )GetDlgItem( IDC_COPY_LANINFO );
pCopyListBtn->EnableWindow( bCopyList );
}
void CXLANInfoDlg::GetRemoteMAC(CString &IP, CString &strMAC)
{
HRESULT hr;
IPAddr ipAddr;
ULONG pulMac[ 2 ];
ULONG ulLen;
CString tmp0,tmp1;
strMAC = _T("Hello,YangTze!");
ipAddr = inet_addr( ( char * )( LPCTSTR )IP );
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 CXLANInfoDlg::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++ ) {
m_list.UpdateWindow();
if( pSubNetResource->lpRemoteName ) {
tmp.Format( "%s?",pSubNetResource->lpRemoteName );
strRet += tmp;
}
}
}
delete Buffer;
WNetCloseEnum( hEnum );
}
return TRUE;
}
BOOL CXLANInfoDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if( m_IsTTP ) m_tip.RelayEvent( pMsg );
return CDialog::PreTranslateMessage(pMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -