Click here to Skip to main content
6,822,123 members and growing! (20,323 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate

Getting Addresses IP informations

By PhR

IP, protocols installed, some useful informations
VC6, VC7WinXP, MFC, Dev
Posted:16 Feb 2003
Updated:5 Mar 2003
Views:75,127
Bookmarked:40 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 2.26 Rating: 2.37 out of 5
4 votes, 44.4%
1
1 vote, 11.1%
2

3
3 votes, 33.3%
4
1 vote, 11.1%
5

Sample Image - V2.jpg

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));




// Getting hostname if(gethostname(szHostName,sizeof(szHostName)) == SOCKET_ERROR)
return (ShowNetworkError("gethostname failed, returned error code : %d")); lpHostEnt = gethostbyname(szHostName); if(!lpHostEnt)
return (
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())
return (
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)
return (ShowNetworkError("Memory allocation failed"));
// Returns number of protocols installed iNumberOfProtocols = WSAEnumProtocols(NULL, lpProtocolBuf,&dwBufLen) ;
if(iNumberOfProtocols == SOCKET_ERROR) { MemoryFree(lpProtocolBuf); return (
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 (
ShowNetworkError("GetNetworkParams failed, returned error code : %d")); } } if(iResult = (int) GetNetworkParams(lpFixedInfo,&ulOutBufLen)) { if(iResult != ERROR_SUCCESS) { MemoryFree(lpFixedInfo) ; MemoryFree(lpProtocolBuf) ; return (
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 (
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

About the Author

PhR


Member

Occupation: Web Developer
Location: France France

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
GeneralDoes your code get the NIC MAC PinmemberReader Man San2:24 1 Nov '06  
GeneralIPv6 Support ? PinmemberAlpha Siera2:28 16 Sep '04  
GeneralThe Hard Way PinmemberSteve Pullan19:39 27 Apr '04  
GeneralRe: The Hard Way PinsussAnonymous17:16 6 Nov '04  
GeneralAny on line service? PinsussSan Jose 12320:26 29 Mar '03  
GeneralVague things Pinmembergordonfreeman3:31 7 Mar '03  
Generaldon't work in win2k Pinmembergu mingqiu3:23 7 Mar '03  
GeneralRe: don't work in win2k Pinmembertorkel221:34 6 Nov '04  
GeneralRe: don't work in win2k PinmemberThatsAlok22:51 6 Nov '04  
GeneralRe: don't work in win2k PinsussAnonymous1:27 7 Nov '04  
GeneralRe: don't work in win2k PinmemberThatsAlok3:01 7 Nov '04  
GeneralRe: don't work in win2k Pinmembertorkel20:24 8 Nov '04  
GeneralUpdated Pinmemberfifi16:32 6 Mar '03  
GeneralTime? PinmemberKant13:12 20 Feb '03  
GeneralCool sample image ... PinmemberClaudius Mokler5:49 18 Feb '03  
GeneralRe: Cool sample image ... PinmemberPhR8:56 18 Feb '03  
GeneralDownload is broken PinmemberThe jackal23:19 17 Feb '03  
GeneralRe: Download is broken PinmemberPhR14:16 19 Feb '03  
Generaldownload is broken PinmemberThomT11:34 17 Feb '03  
GeneralRe: download is broken PinmemberPhR12:06 17 Feb '03  
GeneralRe: download is broken Pinmembergjp1730214:06 17 Feb '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 5 Mar 2003
Editor: Nick Parker
Copyright 2003 by PhR
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project