【作者】hagar / scarfman
【内文】http://delphi.ktop.com.tw/topic.asp?topic_id=56997
Q1:
取得本机计算机IP程序如下, 但这个方法只能取得一个,
请问如何取得本机计算机所有 IP ?
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 then begin
IP := HostEnt^.h_addr_list^;
host_ip := IntToStr(Integer(IP[0]))
+ '.' + IntToStr(Integer(IP[1]))
+ '.' + IntToStr(Integer(IP[2]))
+ '.' + IntToStr(Integer(IP[3]));
end
else
host_ip := '(N/A)';
Result:=host_ip;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Text:=GetIP;
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.
// 使用 Delphi 7 Eneterprise 的 Indy
// GetIP 取得所有本机计算机的 IP 地址
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:
请问如果不使用 Indy 该如何写 ?
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
{==============================================================================}
{ 判断字符串是IP或是HostName, 若是IP则回传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 := StrToInt(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) then Exit;
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) then Exit;
sByte := Copy(sUnknown, 1, iPos-1);
if not IsStringAByteNumber(sByte) then Exit;
// Check Byte3
iPos := Pos('.', sUnknown);
if (iPos = 0) or (iPos = 1) then Exit;
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) then Exit;
sByte := Copy(sUnknown, 1, iPos-1);
Delete(sUnknown, 1, iPos);
if not IsStringAByteNumber(sByte) then Exit;
Result := True;
end;
{==============================================================================}
{ 函式叙述: }
{ 将指定的计算机主机名称(HostName)转换成IP地址 }
{ 参数: }
{ AHostName: }
{ 要转换的计算机主机名称。 }
{ AIP: }
{ 若成功,由此传回转换后的IP。 }
{ 传回值: }
{ 成功传回True,失败传回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 then
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;
s: 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 // 如果是IP字符串, 先取得HostName
begin
k := inet_addr(aBuffer);
if (k <> INADDR_NONE) then
begin
hostent := gethostbyaddr(@k, 4, PF_INET);
if (hostent <> nil) then
StrCopy(aBuffer, hostent.h_name);
end;
end;
hostent := gethostbyname(aBuffer);
if hostent = nil then
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(addr);
AIPs.Add(s);
Inc(p);
end;
end;
finally
WSACleanup;
end;
end;