Click here to Skip to main content
11,437,713 members (32,323 online)
Click here to Skip to main content

xLANInfo

, 25 Nov 2003
Rate this:
Please Sign up or sign in to vote.
A useful tool for enumerating LAN information such as IP,MAC,Shared resources.

Sample Image - xLANInfo.jpg

Introduction

xLANInfo is a useful tool for getting some information about LAN, such as remote-machine's IP/MAC/Host name/Shared resources.

Key code fragment

//=======================================================================
// 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;
}

Thanks

Thanks for your review!

History

  • First written - By YangTze, 11/26/2K3

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

YangTze
Web Developer
China China
Hi,I'm YangTze!

Comments and Discussions

 
Questionexecutable instead of a msgbox? Pin
WL.LEE1-Apr-07 18:27
memberWL.LEE1-Apr-07 18:27 
GeneralReusable version Pin
TRK13-Dec-06 3:36
memberTRK13-Dec-06 3:36 
GeneralLibary files ! Pin
Bui Tan Duoc22-Nov-06 20:49
memberBui Tan Duoc22-Nov-06 20:49 
GeneralHi Pin
Reader Man San1-Nov-06 2:31
memberReader Man San1-Nov-06 2:31 
General&#25903;&#25345; Pin
pcasa5-Jul-06 21:44
memberpcasa5-Jul-06 21:44 
Generalinformation on traffic Pin
Falcao_22-May-06 3:22
memberFalcao_22-May-06 3:22 
GeneralLAN traffic Pin
Vasil Nikolov25-Jul-05 22:44
memberVasil Nikolov25-Jul-05 22:44 
Questionwhy doesn't it work? Pin
yanping wang1-Jul-05 19:54
memberyanping wang1-Jul-05 19:54 
Generalgetting information from workgroup Pin
badzio18-Mar-05 3:39
sussbadzio18-Mar-05 3:39 
GeneralRe: getting information from workgroup Pin
Member 314043817-Apr-08 2:01
memberMember 314043817-Apr-08 2:01 
GeneralWorkgroup name Pin
G.A.21-Jan-05 0:15
memberG.A.21-Jan-05 0:15 
QuestionHow to make it execute on big (usual) networks Pin
Franz Brunner23-Oct-04 4:37
memberFranz Brunner23-Oct-04 4:37 
AnswerRe: How to make it execute on big (usual) networks Pin
WL.LEE1-Apr-07 18:24
memberWL.LEE1-Apr-07 18:24 
GeneralxLANInfo.exe - 3 error(s), 2 warning(s) Pin
surtei28-May-04 1:59
membersurtei28-May-04 1:59 
GeneralRe: xLANInfo.exe - 3 error(s), 2 warning(s) Pin
YangTze30-May-04 16:48
memberYangTze30-May-04 16:48 
GeneralRe: xLANInfo.exe - 3 error(s), 2 warning(s) Pin
surtei31-May-04 23:01
membersurtei31-May-04 23:01 
GeneralRe: xLANInfo.exe - 3 error(s), 2 warning(s) Pin
Anonymous15-Apr-05 22:42
sussAnonymous15-Apr-05 22:42 
Questionwhy doesn't it work? Pin
BarçaBoy30-Dec-03 2:38
memberBarçaBoy30-Dec-03 2:38 
Questionwhy doesn't it work? Pin
BarçaBoy30-Dec-03 2:37
memberBarçaBoy30-Dec-03 2:37 
QuestionWhere is 'Iphlpapi.h' file ? Pin
pinhopro3-Dec-03 2:38
memberpinhopro3-Dec-03 2:38 
AnswerRe: Where is 'Iphlpapi.h' file ? Pin
YangTze3-Dec-03 15:24
memberYangTze3-Dec-03 15:24 
AnswerRe: Where is 'Iphlpapi.h' file ? Pin
Anonymous27-Jan-05 22:34
sussAnonymous27-Jan-05 22:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 26 Nov 2003
Article Copyright 2003 by YangTze
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid