Click here to Skip to main content
Licence 
First Posted 16 Aug 2002
Views 78,695
Bookmarked 17 times

Class to Determine a Local Machine's IP Address

By | 23 Aug 2002 | Article
This is a class for determining a local machine's IP address using iphlpapi or sockets and to convert between a couple of standard formats

Introduction

I would like to thank Igor Proskuriakov, Len Holgate, and Farooque Khan. My purpose for this class is to programatically determine the IP address that is assigned to a local machine. This class will return the machine's IP address in the form of a CString, a DWORD in host byte order, and a DWORD in network byte order.

IPAddressBase Class Usage

An object of this class will contain an IP address value and perform format conversion. The IP address is stored in a CString. Member functions have been provided to converted the stored CString to a network byte order DWORD or host byte order DWORD. The default constuctor initializes the class's data member m_csAddress to "127.0.0.1".
	void SetHBO(DWORD dwInValue);
Member function SetHBO(DWORD dwInValue) takes a DWORD value(in host byte order) and coverts the value to a CString. The CString equivalent is then stored in m_csAddress;
	void SetNBO(DWORD dwInValue);
Member function SetNBO(DWORD dwInValue) takes a DWORD value(in network byte order) and coverts the value to a CString. The CString equivalent is then stored in m_csAddress;
	void SetString(CString csInString);
Member function SetString(CString csInString) takes a CString and stores the value in m_csAddress;
	DWORD GetHBO()const;
Member function GetHBO()const coverts m_csAddress to a DWORD in host byte order and returns that converted value.
	DWORD GetNBO()const;
Member function GetNBO()const coverts m_csAddress to a DWORD in network byte order and returns that converted value.
	CString GetString()const;
Member function GetString()const returns m_csAddress.

LocalIPQuery Class Usage

This class performs the extraction of the machine's IP address/addresses. This class contains two different methods for extraction that are programmer selectable with conditional compilation. The container that holds the extracted IP address/addresses is a STL vector.

The LIPQ_MULTI method is derived from Farooque Khan's "Using IP Helper API’s" article. This method takes the local machine's TCP/IP table and extracts each active unique IP address on the local machine. The extracted values are stored as IPAddressBase objects in the vector.

The LIPQ_PORTABLE method is from http://tangentsoft.net/wskfaq/examples/ipaddr.html. This method extracts the local machine IP address from a socket it creates. The extracted value is stored as an IPAddressBase object in the vector. This method will only extract a single IP address, but it is portable since it uses sockets.

Selection of the methods is done in the LocalIPQuery.h file.

To use LIPQ_MULTI verify that the LocalIPQuery header file is configured as follows before compiling

// User must choose which method they want to use
//#define USE_THIS	LIPQ_PORTABLE
#define USE_THIS	LIPQ_MULTI
To use LIPQ_PORTABLE verify that the LocalIPQuery header file is configured as follows before compiling
// User must choose which method they want to use
#define USE_THIS	LIPQ_PORTABLE
//#define USE_THIS	LIPQ_MULTI

The Dialog App

The dialog app shows the extracted IP address/addresses and one way of using the LocalIPQuery class with an IP Address Control.

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

niverson



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThings to improve. PinmemberLen Holgate0:10 21 Aug '02  
GeneralRe: Things to improve. PinmemberIgor Proskuriakov6:47 21 Aug '02  
GeneralRe: Things to improve. PinmemberLen Holgate6:57 21 Aug '02  
GeneralRe: Things to improve. Pinmemberniverson14:32 24 Aug '02  
GeneralRe: Things to improve. PinmemberLen Holgate23:03 24 Aug '02  
GeneralRe: Things to improve. Pinmembervmihalj22:59 13 Aug '03  
GeneralUseless PinmemberIgor Proskuriakov3:38 20 Aug '02  
Absolutely useless article!!!
As far as I understood from description, the goal is to get local IP address.
Normally everyone can do it easily using gethostbyname. See for example Winsock Programmer's FAQ
Example: Getting the Local IP Address
http://tangentsoft.net/wskfaq/examples/ipaddr.html
 
The main advantage of this approach that it is portable and does not require any Winsock specifics. By some unknown reasons author decides to use IP Helper. Even if IP Helper API provides well known way to get local address, which is GetAdaptersInfo, our Author decides to use GetTcpTable, which enumerates all exiting TCP connections. If you look into the code you see that author stupidly goes through every connection, gets local address, every time overwriting previous result and returns the last one. And this is bascially the core of that class!!!
 
I even can go into details and mention that this thing should not exist as a class as it does not care about any internal variable and all those member functions should become non member. See for example famous "How Non-Member Functions Improve Encapsulation" http://www.cuj.com/articles/2000/0002/0002c/0002c.htm.
 
Actually, the article maybe a joke but some readers might not understand it and even use this code!!!!

 
Igor Proskuriakov
GeneralRe: Useless PinmemberDaniel Turini4:00 20 Aug '02  
GeneralRe: Useless PinmemberIgor Proskuriakov6:39 21 Aug '02  
GeneralRe: Useless Pinmemberniverson4:17 20 Aug '02  
General192.168.x.x PinmemberSwinefeaster10:52 19 Aug '02  
GeneralRe: 192.168.x.x Pinmemberniverson13:40 19 Aug '02  
GeneralRe: 192.168.x.x PinmemberDavid Simmons5:00 20 Aug '02  
GeneralRe: 192.168.x.x PinmemberNeville Franks12:07 20 Aug '02  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 24 Aug 2002
Article Copyright 2002 by niverson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid