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.