你看看这段代码可以不~!我可以得到 这是我通过Ip返回机器名的代码,你改一下,应该就可以了~! function GetNameByIPAddr(IPAddr : String;var MacName:String): Boolean; var SockAddrIn: TSockAddrIn; HostEnt: PHostEnt; WSAData: TWSAData; begin Result := False; if IpAddr = '' then exit; try WSAStartup(2, WSAData); SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr)); HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET); if HostEnt <> nil then MacName := StrPas(Hostent^.h_name); Result := True; finally WSACleanup; end; end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, winsock, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } function GetHostIP(HostName: String): String; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}function Tform1.GetHostIP(HostName: String): String; var buf:pChar; iWsaRet:Integer; Data:WSAData; hostent:PHostEnt; begin Result := ''; iWsaRet := WSAStartup($101,Data); if iWsaRet<>0 then begin ShowMessage('Socket initialize error!'); Exit; end; buf := Allocmem(60); strcopy(buf,PChar(HostName)); if Trim(buf)='' then gethostname(buf,60); hostent := gethostbyname(buf); Freemem(buf,60); if hostent=nil then Exit; Result := inet_ntoa(pinAddr(hostent^.h_addr^)^); WSACleanup(); end; end.如果hostname为空,则取本机的IP 要获取远程机器的IP 就输入远程机器的名字
function TForm1.GetIPByName(AName: String): string; type TaPInAddr = array [0..10] of PInAddr; PaPInAddr = ^TaPInAddr; var phe: PHostEnt; pptr: PaPInAddr; Buffer: array [0..63] of char; I: Integer; GInitData: TWSADATA; begin WSAStartup($101, GInitData); Result := ''; StrPCopy(Buffer, AName); 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; end;
这是我通过Ip返回机器名的代码,你改一下,应该就可以了~!
function GetNameByIPAddr(IPAddr : String;var MacName:String): Boolean;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
Result := False;
if IpAddr = '' then exit;
try
WSAStartup(2, WSAData);
SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr));
HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt <> nil then
MacName := StrPas(Hostent^.h_name);
Result := True;
finally
WSACleanup;
end;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, winsock, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function GetHostIP(HostName: String): String;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function Tform1.GetHostIP(HostName: String): String;
var
buf:pChar;
iWsaRet:Integer;
Data:WSAData;
hostent:PHostEnt;
begin
Result := '';
iWsaRet := WSAStartup($101,Data);
if iWsaRet<>0 then
begin
ShowMessage('Socket initialize error!');
Exit;
end;
buf := Allocmem(60);
strcopy(buf,PChar(HostName));
if Trim(buf)='' then
gethostname(buf,60);
hostent := gethostbyname(buf);
Freemem(buf,60);
if hostent=nil then
Exit;
Result := inet_ntoa(pinAddr(hostent^.h_addr^)^);
WSACleanup();
end;
end.如果hostname为空,则取本机的IP
要获取远程机器的IP
就输入远程机器的名字
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: array [0..63] of char;
I: Integer;
GInitData: TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '';
StrPCopy(Buffer, AName);
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;
end;