Click here to Skip to main content
11,493,391 members (69,604 online)
Click here to Skip to main content

Getting Addresses IP informations

, 5 Mar 2003 92K 4.2K 45
Rate this:
Please Sign up or sign in to vote.
IP, protocols installed, some useful informations
<!-- Article image -->

Sample Image - V2.jpg

<!-- Add the rest of your HTML here -->

Introduction

My Ip gives you information about IP addresses, adapter and installed protocols. It gives details about WSADATA structure with comments form MS SDK. No interpretation of the meanings is done this is textually the SDK text. IP addresses are retrieved using the GetNetworkParams function. Another useful feature is IP mask, shown too. This tool could be useful for testing network configurations and for teaching. Beginning programmers could be interesting in the way to detect network parameters. The program is a dialog box including a clock. The WinSock 2.2 DLL must be installed for running My Ip. I have tested My Ip under Windows XP not on others OS. The main WinSock functions used are gethostbyname(...), WSAEnumProtocols(...), GetNumberOfInterfaces(...) and GetNetworkParams(...). I made the tool to detect parameters without beeing connected, and to resolve conflicts between hardware and software. The source code was written with Visual Studio .NET 2002, FRENCH Version. The main work is done with the bool InitNetwork(void) function.

bool InitNetwork(void)
{
    int     iResult;
    DWORD   dwOutBufLen;

    char    szTmp[256];

    iResult = WSAStartup(MAKEWORD(2,2),&WsaData);
    if(iResult)
    {
        // Can't call WSAGetLastError() because WinSock DLL is not loaded !

        wsprintf(szTmp,"WSAStartup failed, returned error code : %d",iResult);
        MessageBox(NULL,szTmp,szError,MB_OK|MB_ICONEXCLAMATION);
        return (FALSE);
    }

    memset(szHostName,0,sizeof(szHostName));<BR><BR><BR> <BR><BR>    // Getting hostname
    if(gethostname(szHostName,sizeof(szHostName)) == SOCKET_ERROR)    <BR>     return (ShowNetworkError("gethostname failed, returned error code : %d"));

    lpHostEnt = gethostbyname(szHostName);
    if(!lpHostEnt)    <BR>     return (<BR>     ShowNetworkError("gethostbyname failed, returned error code : %d"));

    dwBufLen = 0 ;    // Setting to 0, to have the real size of the new buffer

    iResult = WSAEnumProtocols(NULL,lpProtocolBuf,&dwBufLen);
    if(WSAENOBUFS != WSAGetLastError())    <BR>     return (<BR>      ShowNetworkError("WSAEnumProtocols failed, returned error code : %d"));
    
    // The buffer is too small, try to reallocate one with the desired size

    lpProtocolBuf = (WSAPROTOCOL_INFO *) MemoryAlloc(dwBufLen);
    if(!lpProtocolBuf)                    <BR>      return (ShowNetworkError("Memory allocation failed"));
    <BR>    // Returns number of protocols installed
    iNumberOfProtocols = WSAEnumProtocols(NULL, lpProtocolBuf,&dwBufLen) ;    <BR>    if(iNumberOfProtocols == SOCKET_ERROR)
    {
      MemoryFree(lpProtocolBuf);
      return (<BR>      ShowNetworkError("WSAEnumProtocols failed, returned error code : %d"));
    }

    lpFixedInfo = (FIXED_INFO *) MemoryAlloc(sizeof(FIXED_INFO));
    if(!lpFixedInfo)
    {
        MemoryFree(lpProtocolBuf) ;
        return (ShowNetworkError("Memory allocation failed")) ;
    }

    ulOutBufLen = sizeof(FIXED_INFO) ;
   
    iResult = (int) GetNetworkParams(lpFixedInfo,&ulOutBufLen);
    if(iResult == ERROR_BUFFER_OVERFLOW)
    {
        MemoryFree(lpFixedInfo) ;

        lpFixedInfo = (FIXED_INFO *) MemoryAlloc(ulOutBufLen);
        if(!lpFixedInfo)
        {
            MemoryFree(lpProtocolBuf) ;
            return (ShowNetworkError("Memory allocation failed"));
        }
    }
    else
    {
        if(iResult != ERROR_SUCCESS)
        {
            MemoryFree(lpFixedInfo) ;
            MemoryFree(lpProtocolBuf) ;
            return (<BR>      ShowNetworkError("GetNetworkParams failed, returned error code : %d"));
        }
    }

    if(iResult = (int) GetNetworkParams(lpFixedInfo,&ulOutBufLen))
    {
        if(iResult != ERROR_SUCCESS)
        {
            MemoryFree(lpFixedInfo) ;
            MemoryFree(lpProtocolBuf) ;

            return (<BR>      ShowNetworkError("GetNetworkParams failed, returned error code : %d"));
        }
    }

    dwOutBufLen = sizeof(IP_ADAPTER_INFO) ;
    lpAdapterInfo = (PIP_ADAPTER_INFO) MemoryAlloc(dwOutBufLen);
    if(!lpAdapterInfo)
    {
        MemoryFree(lpFixedInfo);
        MemoryFree(lpProtocolBuf);

        return (ShowNetworkError("Memory allocation failed"));
    }

    iResult = GetAdaptersInfo(lpAdapterInfo,&dwOutBufLen);

    if(iResult == ERROR_BUFFER_OVERFLOW)
    {
        MemoryFree(lpAdapterInfo) ;
        lpAdapterInfo = (PIP_ADAPTER_INFO) MemoryAlloc(dwOutBufLen);

        if(!lpAdapterInfo)
        {
            MemoryFree(lpFixedInfo);
            MemoryFree(lpProtocolBuf);

            return (ShowNetworkError("Memory allocation failed"));
        }
    }
    else
    {
        if(iResult != ERROR_SUCCESS)
        {
            MemoryFree(lpFixedInfo);
            MemoryFree(lpProtocolBuf);
            MemoryFree(lpAdapterInfo);

            return (<BR>        ShowNetworkError("GetAdaptersInfo failed, returned error code : %d"));
        }
    }

    dwNumInterfaces = 0;
    GetNumberOfInterfaces(&dwNumInterfaces);

    return (WSACleanup() != SOCKET_ERROR);
}

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

PhR
Web Developer
France France
No Biography provided

Comments and Discussions

 
GeneralDoes your code get the NIC MAC Pin
Reader Man San1-Nov-06 2:24
memberReader Man San1-Nov-06 2:24 
QuestionIPv6 Support ? Pin
Alpha Siera16-Sep-04 2:28
memberAlpha Siera16-Sep-04 2:28 
GeneralThe Hard Way Pin
Steve Pullan27-Apr-04 19:39
memberSteve Pullan27-Apr-04 19:39 
GeneralRe: The Hard Way Pin
Anonymous6-Nov-04 17:16
sussAnonymous6-Nov-04 17:16 
QuestionAny on line service? Pin
San Jose 12329-Mar-03 20:26
sussSan Jose 12329-Mar-03 20:26 
GeneralVague things Pin
gordonfreeman7-Mar-03 3:31
membergordonfreeman7-Mar-03 3:31 
Generaldon't work in win2k Pin
gu mingqiu7-Mar-03 3:23
membergu mingqiu7-Mar-03 3:23 
GeneralRe: don't work in win2k Pin
torkel26-Nov-04 21:34
membertorkel26-Nov-04 21:34 
GeneralRe: don't work in win2k Pin
ThatsAlok6-Nov-04 22:51
memberThatsAlok6-Nov-04 22:51 
GeneralRe: don't work in win2k Pin
Anonymous7-Nov-04 1:27
sussAnonymous7-Nov-04 1:27 
GeneralRe: don't work in win2k Pin
ThatsAlok7-Nov-04 3:01
memberThatsAlok7-Nov-04 3:01 
GeneralRe: don't work in win2k Pin
torkel28-Nov-04 0:24
membertorkel28-Nov-04 0:24 
GeneralUpdated Pin
fifi6-Mar-03 16:32
memberfifi6-Mar-03 16:32 
QuestionTime? Pin
Kant20-Feb-03 13:12
memberKant20-Feb-03 13:12 
GeneralCool sample image ... Pin
Claudius Mokler18-Feb-03 5:49
memberClaudius Mokler18-Feb-03 5:49 
GeneralRe: Cool sample image ... Pin
PhR18-Feb-03 8:56
memberPhR18-Feb-03 8:56 
GeneralDownload is broken Pin
The jackal17-Feb-03 23:19
memberThe jackal17-Feb-03 23:19 
GeneralRe: Download is broken Pin
PhR19-Feb-03 14:16
memberPhR19-Feb-03 14:16 
Generaldownload is broken Pin
ThomT17-Feb-03 11:34
memberThomT17-Feb-03 11:34 
GeneralRe: download is broken Pin
PhR17-Feb-03 12:06
memberPhR17-Feb-03 12:06 
GeneralRe: download is broken Pin
gjp1730217-Feb-03 14:06
membergjp1730217-Feb-03 14:06 

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.150520.1 | Last Updated 6 Mar 2003
Article Copyright 2003 by PhR
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid