1
2 { You must be connected to the internet and don't forget to add this
3 unit to get it to work :-) }
4
5 unit Unit1;
6
7 interface
8
9 uses
10 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
11 Dialogs,winsock, StdCtrls;
12
13 type
14 TForm1 = class(TForm)
15 Button1: TButton;
16 Edit1: TEdit;
17 procedure Button1Click(Sender: TObject);
18 private
19 { Private declarations }
20 public
21 function ConvertDomainIP( sHost:string): Boolean;
22 { Public declarations }
23 end;
24
25 type
26 TName = array[0..100] of Char;
27 PName = ^TName;
28
29 var
30 Form1: TForm1;
31 sErrorWAS:string;
32
33 implementation
34
35 {$R *.dfm}
36
37 function Tform1.ConvertDomainIP( sHost:string): Boolean;
38 var
39 HEnt: pHostEnt;
40 HName: PName;
41 WSAData: TWSAData;
42 sIPAddr, sWSAError: string;
43 iCnt,i: Integer;
44 begin
45 Result := False;
46 if WSAStartup($0101, WSAData) <> 0 then begin
47 sWSAError := 'WSAStartup error';
48 Exit;
49 end;
50 sIPAddr := '';
51 sWSAError := '';
52 New(HName);
53 for i:=0 to length(sHost) do
54 HName^[i]:=shost[i+1];
55 HEnt := GetHostByName(HName^);
56 if HEnt <> nil then begin
57 Result := True;
58 for iCnt := 0 to HEnt^.h_length - 1 do
59 sIPAddr := sIPAddr + IntToStr(Ord(HEnt^.h_addr_list^[iCnt])) + '.';
60 SetLength(sIPAddr, Length(sIPAddr) - 1);
61
62 edit1.Text :=sIPAddr; //displays the IP address of the domain name
63 end
64 else begin
65 result :=false;
66 case WSAGetLastError of // possible errors to display
67 WSANOTINITIALISED:
68 sWSAError := 'A successful WSAStartup must occur before using this function.';
69 WSAENETDOWN:
70 sWSAError := 'The network subsystem has failed.';
71 WSAHOST_NOT_FOUND:
72 sWSAError := 'Authoritative Answer Host not found.';
73 WSATRY_AGAIN:
74 sWSAError := 'Non-Authoritative Host not found, or server failed.';
75 WSANO_RECOVERY:
76 sWSAError := 'Nonrecoverable error occurred.';
77 WSANO_DATA:
78 sWSAError := 'Valid name, no data record of requested type.';
79 WSAEINPROGRESS:
80 sWSAError := 'A blocking Windows Sockets 1.1 call is in progress, or the ' +
81 'service provider is still processing a callback function.';
82 WSAEAFNOSUPPORT:
83 sWSAError := 'The type specified is not supported by the Windows '+
84 'Sockets implementation.';
85 WSAEFAULT:
86 sWSAError := 'The addr parameter is not a valid part of the user address '+
87 'space, or the len parameter is too small.';
88 WSAEINTR:
89 sWSAError := 'A blocking Windows Socket 1.1 call was canceled through '+
90 'WSACancelBlockingCall.';
91 else
92 sWSAError := 'UNKNOWN';
93 end;
94 sErrorWAS :=sWSAError;
95 end;
96 Dispose(HName);
97 WSACleanup;
98 end;
99
100 procedure TForm1.Button1Click(Sender: TObject);
101 begin
102 if ConvertDomainIP(edit1.text) then
103 showmessage('Okay')
104 else
105 showmessage('NO! '+sErrorWAS);
106 end;
107
108 end.
109
|