Create a new Win32 console program with the following code:
#include <winsock2.h>
#include <stdio.h>
void Getlocalip ();
int main ()
{
Getlocalip ();
return 0;
}
void Getlocalip ()
{
Char chostname[256] = {0};
WORD wversionrequested;
Wsadata Wsadata;
int err;
wversionrequested = Makeword (2, 2);
Err = WSAStartup (wversionrequested, &wsadata);
if (err! = 0)
{
Return
}
if (GetHostName (chostname, up) = = Socket_error)
{
Return
}
struct Hostent * phe = gethostbyname (chostname);
if (phe = = NULL)
{
Return
}
if (phe->h_addr_list[0] = = 0)
{
Return
}
struct IN_ADDR addr;
memcpy (&ADDR, phe->h_addr_list[0], sizeof (struct in_addr));
DWORD dw1=inet_addr (Inet_ntoa (addr));
WORD Wd1=loword (DW1);//Fetch high 16 bits
WORD Wd2=hiword (DW1);//Take down 16 bits
BYTE Bte1=lobyte (WD1);
BYTE Bte2=hibyte (WD1);
BYTE Bte3=lobyte (WD2);
BYTE Bte4=hibyte (WD2);
printf ("%d.%d.%d.%d", bte1,bte2,bte3,bte4);
}
Normal conditions can be directly compiled and run, if encountered LNK2001, unresolved external symbols: Error, add Ws2_32.lib to the project properties [linker]->[command line] additional option and recompile.
This article is from the "adwen2010" blog, make sure to keep this source http://adwen2010.blog.51cto.com/1820717/1584334
Get the function of native IP