How do Windows applications obtain the local IP address?

Source: Internet
Author: User

We can see some routines that use winsocket. In fact, we all know that ipconfig can meet our requirements. The IP assistant API provides similar functions. getadaptersinfo is used in this article. You can check msdn for the specific parameter meanings.

Example:

// You need to connect to lplpapi. Lib

# Include <iostream>
# Include <atlstr. h>
# Include <iphlpapi. h>
Void gethostip (cstring & address)
{
Ulong Len = 0; address. Empty ();
Getadaptersinfo (null, & Len );
Pip_adapter_info P = static_cast <pip_adapter_info> (malloc (LEN ));
Getadaptersinfo (p, & Len );
For (pip_adapter_info q = P; Q! = NULL; q = Q-> next)
{
Address + = Q-> ipaddresslist. IPaddress. String;
Address + = '/N ';
}
Free (P );
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Cstring address;
Gethostip (Address );
STD: cout <address;
Return 0;
}

The first call, Len returns the required buffer length; the second call gets information. Because there may be more than one network card, each IP address is separated by a line break.

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.