Archived

This topic is now archived and is closed to further replies.

Yanroy

Local IP address

Recommended Posts

Ok, this one really has me stumped. I can''t find any function that will return the IP address of the local machine. Someone once gave me some sample code that supposedly does it involving gethostname() and gethostbyname(), but crashes when I try to convert the data those functions return into something inet_ntoa() will accept. Is there an easy way to do this? Here''s the code I have now:
char HostName[256];
char IP[16];

gethostname(HostName, sizeof(HostName));
HOSTENT * HostData = gethostbyname(HostName);
sockaddr_in addr;
memset(&addr,0,sizeof(addr));
memcpy(&(LocalAddress.sin_addr.s_addr), HostData->h_addr,4);
strcpy(IP, inet_ntoa(LocalAddress.sin_addr));
MultiByteToWideChar(CP_ACP, NULL, IP, -1, LocalIP, RemoteCommIPLen);
--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
Basically, you can''t. Or, more precisely, you can. To expand on that, your computer may have multiple IP addresses, depending on what interface you''re looking at. So it doesn''t make sense to think of "the computer''s IP address".

What, exactly, are you trying to do? Some more info about your problem would help us help you.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I dunno about sockets, but here is what the MSDN says in Platform SDK: Internet Protocol Helper:

// Link with IPHlpAPI.lib

//

#include <stdio.h>
#include <windows.h>
#include <iphlpapi.h>

main() {

FIXED_INFO * FixedInfo;
ULONG ulOutBufLen;
DWORD dwRetVal;
IP_ADDR_STRING * pIPAddr;

FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
ulOutBufLen = sizeof( FIXED_INFO );

if( ERROR_BUFFER_OVERFLOW == GetNetworkParams( FixedInfo, &ulOutBufLen ) ) {
GlobalFree( FixedInfo );
FixedInfo = GlobalAlloc( GPTR, ulOutBufLen );
}

if ( dwRetVal = GetNetworkParams( FixedInfo, &ulOutBufLen ) ) {
printf( "Call to GetNetworkParams failed. Return Value: %08x\n", dwRetVal );
}
else {
printf( "Host Name: %s\n", FixedInfo -> HostName );
printf( "Domain Name: %s\n", FixedInfo -> DomainName );

printf( "DNS Servers:\n" );
printf( "\t%s\n", FixedInfo -> DnsServerList.IpAddress.String );

pIPAddr = FixedInfo -> DnsServerList.Next;
while ( pIPAddr ) {
printf( "\t%s\n", pIPAddr ->IpAddress.String );
pIPAddr = pIPAddr ->Next;
}
}
exit( 0 );
}



-solo (my site)

Share this post


Link to post
Share on other sites
Haha, 127.0.01 isn''t much help to me. I''m working off an iPAQ handheld PC, and I want it to display its IP on the screen so I don''t have to remember what it is when I want to connect to it with my laptop. As far as I''ve been able to tell, developing for Pocket PC is identical to working with any other Windows platform, even to the point where the emulator doesn''t really emulate much... it just passes the function calls through to the desktop''s OS. This iPAQ is going to be the brain of a robot, and the laptop is the remote control, thanks to 802.11b wireless LAN. Does this help? If worse comes to worse, I can just stick a post-it note on the iPAQ, but that''s not quite as clever as having it find it''s own IP.

Share this post


Link to post
Share on other sites