Delphi obtains the local IP Address

Source: Internet
Author: User

There are many methods to get the IP address of the Local Machine. Now we use Windows API to implement the following in Delphi;

1. reference the Winsock unit in uses.

2. The source code is as follows:

Function getlocalip: string; Type tapinaddr = array [0 .. 10] of pinaddr; // list of IP addresses used to store activity papinaddr = ^ tapinaddr; var Phe: phostent; pptr: papinaddr; Buffer: array [0 .. 63] of char; // store hostname I: integer; ginitdata: twsadata; wversion: word; begin wversion: = makeword (1, 1); // Winsock dll version result: = ''; If wsastartup (wversion, ginitdata) = 0 then // initialize Windows Socket begin if gethostname (buffer, sizeof (buffer) = 0 then // computer name Phe: = gethostbyname (buffer); If Phe = nil then exit; pptr: = papinaddr (PHE ^. h_addr_list); I: = 0; while pptr ^ [I] <> nil do begin result: = strpas (inet_ntoa (pptr ^ [I] ^); Inc (I ); end; wsacleanup; // close and clear Windows Socket end;

3. The disadvantage of the source code is that if there are two or more NICs on the machine, the last one is obtained.

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.