Delphi K.TOP Newspaper No. 085
This issue [Article] How do I get all my IP? [Edit] Pilot Angel this week's selection [Article] How do I get all my IP? [Edit] pilot angel [Author] hagar / scarfman [inland] http : //delphi.ktop.com.tw/topic.asp? Topic_ID = 56997Q1: Native computer IP programs are as follows, but this method can only get one, how do I get all my IP of this machine?
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Winsock
Type
TFORM1 = Class (TFORM)
Memo1: TMEMO;
Procedure formcreate (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
VAR
FORM1: TFORM1;
IMPLEMENTATION
{$ R * .dfm}
Function Getip: String;
VAR
WVersionRequired: Word;
WSDATA: TWSADATA;
Status: integer;
Name: array [0..255] of char;
Hostent: phostent;
IP: PCHAR;
Host_ip: String;
Begin
WVersionRequired: = MakeWord (1, 1);
Status: = WSASTARTUP (WVersionRequired, WSDATA);
IF status <> 0 THEN Begin
Messagedlg ('Error Occured', MTERROR, [Mbok], 0);
EXIT;
END;
GethostName (Name, Sizeof (Name));
Hostent: = gethostbyname (@name);
IF hostent <> nil kil dam
IP: = HOSTENT ^ .h_addr_list ^;
Host_ip: = INTOSTR (Integer (IP [0])))
'.' INTSTR (Integer (IP [1]))
'.' INTSTR (Integer (IP [2])))
'.' INTOSTR (Integer (IP [3]));
end
Else
Host_ip: = '(N / A)';
Result: = Host_IP;
END;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Memo1.text: = Get;
END;
End.
A1:
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, stdctrls;
Type
TFORM1 = Class (TFORM)
Memo1: TMEMO;
Procedure formcreate (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
VAR
Form1: TFORM1; IMPLEMENTATION
{$ R * .dfm}
Uses
Unit2;
Procedure TFORM1.FormCreate (Sender: TOBJECT);
Begin
Memo1.Lines.Text: = getAllip;
Memo1.Lines.Add ('');
Memo1.Lines.Add (Memo1.Lines [0]);
END;
End.
// Use Delphi 7 EnerPrise's INDY
// getip acquiring all the IP addresses of all local computers
Unit unit2;
Interface
Function getAllip: String;
IMPLEMENTATION
Uses IDStack;
Function getAllip: String;
VAR
IDSTACK: TidStack;
Begin
IDSTACK: = TidStack.createStack;
Try
Result: = idStack.localaddresses.Text;
Finally
Idstack.free;
END;
END;
End.
Q2: Do you do not use indey how to write? A2: Interface
Function hostnametoip (Ahostname: string; var aip: string): boolean; overload;
Function HostnameToip (Ahostname: String; AIPS: TSTRINGS): Boolean; Overload;
Function isstringanip (const hostnameorip: string): boolean;
IMPLEMENTATION
{================================================================================================================================================================================================================ ==============================}
{Judgment string is IP or hostname, if it is IP, return true}
{================================================================================================================================================================================================================ ==============================}
Function isstringanip (const hostnameorip: string): boolean;
VAR
Sbyte,
SUNKNOWN: STRING;
IPOS: Integer;
Function isStringabyTenumber (const s: string): boolean;
VAR
i: integer;
Begin
Result: = FALSE;
Try
i: = start (s);
Result: = ((i> = 0) AND (i <256));
Except
END;
END;
Begin
Result: = FALSE;
SUNKNOWN: = Hostnameorip;
// check byte1
IPOS: = POS ('.', sunknown);
IF (ipos = 0) or (ipos = 1).
Sbyte: = COPY (SunKNown, 1, IPOS-1);
DELETE (SunKNown, 1, IPOS);
IF not isstringabyTenumber (sbyte) THEN EXIT;
// check byte2
IPOS: = POS ('.', sunknown);
IF (ipos = 0) or (ipos = 1).
Sbyte: = COPY (SunKNown, 1, IPOS-1);
IF not isstringabyTenumber (sbyte) THEN EXIT
// check byte3
IPOS: = POS ('.', sunknown);
IF (ipos = 0) or (ipos = 1).
Sbyte: = COPY (SunKNown, 1, IPOS-1);
DELETE (SunKNown, 1, IPOS);
IF not isstringabyTenumber (sbyte) THEN EXIT
// check byte4
IPOS: = POS ('.', sunknown);
IF (ipos = 0) or (ipos = 1).
Sbyte: = COPY (SunKNown, 1, IPOS-1);
DELETE (SunKNown, 1, IPOS);
IF not isstringabyTenumber (sbyte) THEN EXIT;
RESULT: = TRUE;
END;
{================================================================================================================================================================================================================ ==============================}
{Funny Narration:}
{Convert the specified computer host name (hostname) into IP address}
{Parameter:} {ahostname:}
{Computer host name to be converted. }
{AIP:}
{If successful, thereby transmits back the converted IP. }
{Pass value:}
{Successfully passed back TRUE, failed to return false. }
{================================================================================================================================================================================================================ ==============================}
Function hostnametoip (Ahostname: string; var aip: string): boolean;
VAR
WSA: TWSADATA;
ABUFFER: Array [0..255] of char;
Hostent: phostent;
PCADDR: PCHAR;
Begin
Result: = FALSE;
IF WSASTARTUP ($ 0101, WSA) <> 0 THEN
EXIT;
Try
IF gethostname (abuffer, sizeof (abuffer) <> 0 THEN
EXIT;
Strpcopy (abuffer, ahostname);
Hostent: = gethostByName (abuffer);
if hostent = nil dam
EXIT;
IF (hostent ^ .h_addr_list <> nil) THEN
Begin
RESULT: = TRUE;
PCADDR: = Hostent ^ .h_addr_list ^;
IF (PCADDR <> NIL) THEN
Begin
AIP: = format ('% d.% D.% D.% D', [Byte (pcaddr [0]), Byte (PCADDR [1]),
BYTE (PCADDR [2]), Byte (PcAddr [3])]);
END;
END;
Finally
WSACLEANUP;
END;
END;
Function hostnametoip (AhostName: string; aips: tstrings): boolean; var
WSA: TWSADATA;
ABUFFER: Array [0..255] of char;
Hostent: phostent;
String;
P: ^ pchar;
AddR: in_addr;
K: DWORD;
Begin
Result: = FALSE;
IF WSASTARTUP ($ 0101, WSA) <> 0 THEN
EXIT;
Try
IF gethostname (abuffer, sizeof (abuffer) <> 0 THEN
EXIT;
Strpcopy (abuffer, ahostname);
If ISSTRINGANIP (AhostName) Then // If it is an IP string, get hostname first
Begin
K: = INET_ADDR (ABUFFER);
IF (k <> inaddr_none).
Begin
Hostent: = gethostbyaddr (@K, 4, pf_inet);
IF (Hostent <> NIL) THEN
Strcopy (abuffer, hostent.h_name);
END;
END;
Hostent: = gethostByName (abuffer);
if hostent = nil dam
EXIT;
IF (hostent ^ .h_addr_list <> nil) THEN
Begin
RESULT: = TRUE;
P: = POINTER (Hostent ^ .h_addr_list);
While (P <> nil) and (p ^ <> nil) do
Begin
CopyMemory (@addr, p ^, sizeof (addr));
S: = inet_ntoa (add);
AIPS.ADD (s);
INC (P);
END;
END;
Finally
WSACLEANUP;
END;
End; this week's selection [edit] Delphi K.TOP Electronic Newspaper Department 2004/11/20 [Original] Gmail Reader Beta 0.4, Moming2K2004 / 11/19 Timer shutdown and music alarm clock, note reminder applet (source), poemkevin2004/11 / 19 DOS mode under Windows Design Location, DLLEE2004 / 11/17 "C Program Language Classics", 16663622004/11/17 Geperts 1.21 (Stable Release) November 12, 20, GoAT2004 / 11/15 Display RGB Troubles with TCHART Figure, 1666362004/11/15 MSP430F single-chip NOKIA3310 mobile phone panel making wave display, 16663622004/11/15 Timer shutdown and music alarm clock, note reminder applet Listbox components. Wameng