Dev-C++ version
#include
<
iostream
>
#include
<
winsock2.h
>

using
namespace
std;

bool
GetLocalIP(
char
*
ipBuff);

int
main()

...
{
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA data;
int err;
err = WSAStartup(wVersionRequested, &data);

if ( err != 0 ) ...{

/**//* Tell the user that we could not find a usable */

/**//* WinSock DLL. */
cout << "We could not find a usable winsock DLL!" << endl;
return 0;
}


char IP[16] = ...{ 0 };
if( GetLocalIP( IP ) )
cout << IP << endl;
else
cout << "Failed to GetLocalIP() !" << endl;
WSACleanup();
system("PAUSE");
return 0;
}


bool
GetLocalIP(
char
*
ipBuff)

...
{
if ( ipBuff == NULL )
return false;

char hostName[80] = ...{ 0 };
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
return false;
struct hostent * phe = gethostbyname( hostName );
if ( phe == NULL )
return false;
if ( phe->h_addr_list[0] == 0 )
return false;
struct in_addr addr;
memcpy( &addr, phe->h_addr_list[0] , sizeof( struct in_addr ) );

char * tempIP = inet_ntoa( addr );
memcpy(ipBuff, tempIP , strlen(tempIP) );

return true;
}
VC++ version
#include
<
iostream.h
>
#include
<
winsock2.h
>

bool
GetLocalIP(
char
*
ipBuff);

void
main()

...
{
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA data;
int err;
err = WSAStartup(wVersionRequested, &data);

if ( err != 0 ) ...{

/**//* Tell the user that we could not find a usable */

/**//* WinSock DLL. */
cout << "We could not find a usable winsock DLL!" << endl;
return;
}


char IP[16] = ...{ 0 };
if( GetLocalIP( IP ) )
cout << IP << endl;
else
cout << "Failed to GetLocalIP() !" << endl;
WSACleanup();

system("PAUSE");
}


bool
GetLocalIP(
char
*
ipBuff)

...
{
if ( ipBuff == NULL )
return false;

char hostName[80] = ...{ 0 };
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
return false;
struct hostent * phe = gethostbyname( hostName );
if ( phe == NULL )
return false;
if ( phe->h_addr_list[0] == 0 )
return false;
struct in_addr addr;
memcpy( &addr, phe->h_addr_list[0] , sizeof( struct in_addr ) );

char * tempIP = inet_ntoa( addr );
memcpy(ipBuff, tempIP , strlen(tempIP) );

return true;
}
下面是GetLocalIP的重载函数,在设置控件的时候可以用到:
bool
GetLocalIP(BYTE
&
nField0 , BYTE
&
nField1 , BYTE
&
nField2 , BYTE
&
nField3)

...
{
nField0 = nField1 = nField2 = nField3 = 0;

char hostName[80] = ...{ 0 };
if ( gethostname( hostName , 80 ) == SOCKET_ERROR )
return false;
struct hostent * phe = gethostbyname( hostName );
if ( phe == NULL )
return false;
if ( phe->h_addr_list[0] == 0 )
return false;
struct in_addr addr;
memcpy( &addr, phe->h_addr_list[0] , sizeof( struct in_addr ) );

char * tempIP = inet_ntoa( addr );
UINT size = strlen( tempIP );
UINT pos = 0;

while ( tempIP[pos] != '.' )...{
nField0 = nField0*10 + (tempIP[pos++] - 0x30);
}
pos++;

while ( tempIP[pos] != '.' )...{
nField1 = nField1*10 + (tempIP[pos++] - 0x30);
}
pos++;

while ( tempIP[pos] != '.' )...{
nField2 = nField2*10 + (tempIP[pos++] - 0x30);
}
pos++;

while ( tempIP[pos] != '/0' )...{
nField3 = nField3*10 + (tempIP[pos++] - 0x30);
}
return true;
}