Get the function of native IP

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.