-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuWinSock.pas
67 lines (58 loc) · 1.34 KB
/
uWinSock.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
unit uWinSock;
interface
uses System.SysUtils,System.classes, Winapi.Windows, WinSock;
type
TWinSockLib = class(TObject)
public
function GetIP(): TStrings;
end;
implementation
{ TWinSockLib }
{ function TWinSockLib.GetIP: String;
var
wData: WSADATA;
HostName: String;
pHostInfo: pHostEnt;
begin
WSAStartup(MAKEWORD(2, 2), wData);
GetHostName(PAnsiChar(HostName), 512);
pHostInfo := GetHostByName(PAnsiChar(HostName));
if Assigned(pHostInfo) then
Result := inttostr(ord(pHostInfo.h_addr_list^[0])) + '.' +
inttostr(ord(pHostInfo.h_addr_list^[1])) + '.' +
inttostr(ord(pHostInfo.h_addr_list^[2])) + '.' +
inttostr(ord(pHostInfo.h_addr_list^[3])){ + '.' +
inttostr(ord(pHostInfo.h_addr_list^[4])) }
{ else
Result := '';
WSACleanup;
end; }
{ TWinSockLib }
function TWinSockLib.GetIP: TStrings;
type
TaPInAddr = array [0 .. 10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: PAnsiChar;
I: Integer;
WSAData: TWSAData;
begin
WSAStartup(MakeWord(2, 2), WSAData);
Result := TStringList.Create;
Result.Clear;
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(Buffer);
if phe = nil then
Exit;
pptr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do
begin
Result.Add(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanUp;
end;
end.