Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting IP Address of Local Machine

Status
Not open for further replies.

huricane

Programmer
Dec 28, 2002
6
0
0
GB
I am new to VC++ and VC++.NET

Does anyone konw how or have a code sniplet that can retrieve the IP Address of the Local Machine I am running an application on?

I am developing with VC++.NET and want to running the app on a win98 machine

Any help will be appreciated
 
[tt]CString GetLocalAddress(void)
{
char* buffer="";
gethostname (buffer,strlen(buffer));
HOSTENT* lpHostEnt = gethostbyname(buffer);
if (lpHostEnt==NULL) return "127.0.0.1";
CString lpAddr = (char*)inet_ntoa(*(LPIN_ADDR)*(lpHostEnt->h_addr_list));
return lpAddr;
}[/tt]

returns 127.0.0.1 if user not connected to a network - otherwise returns local IP address
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Gednick gave a nice bit of code thanks, but
It's a pity he didn't comment some of the lines like:-

HOSTENT* lpHostEnt = gethostbyname(buffer);

CString lpAddr = (char*)inet_ntoa(*(LPIN_ADDR)*(lpHostEnt->h_addr_list));

apart from that thanks Gednick :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top