主题:获得本机名称的错误
unit ip1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Label1: TLabel;
EdtIP: TEdit;
Label2: TLabel;
EdtName: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
function GetHostName:String;
procedure BitBtn1Click(Sender: TObject);
function NameToIP(Name:string):String;
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetHostName:String;
var
ComputerName:array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
Size:Cardinal;
begin
result:='';
Size:=MAX_COMPUTERNAME_LENGTH+1;
GetComputerName(ComputerName,Size);
Result:=StrPas(ComputerName);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
edtName.Text:=GetHostName;
edtIP.Text:=NameToIP(edtName.Text);
end;
function Tform1.NameToIP(Name:string):String;
var
WSAData:TWSAData;
HostEnt:PHostEnt;
begin
result:='';
WSAStartup(2,WSAData);
HostEnt:=GetHostByName(Pchar(Name));
if HostEnt <> nil then
begin
with HostEnt^ do
result:=Format('%d.%d.%d.%d',[Byte(h_addr^[0]),byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]);
end;
WSACleanup;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
FOrm1.Close;
end;
end.
为什么获得的IP正确,而获得的主机名是十六进制乱码啊??
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinSock, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Label1: TLabel;
EdtIP: TEdit;
Label2: TLabel;
EdtName: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
function GetHostName:String;
procedure BitBtn1Click(Sender: TObject);
function NameToIP(Name:string):String;
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetHostName:String;
var
ComputerName:array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
Size:Cardinal;
begin
result:='';
Size:=MAX_COMPUTERNAME_LENGTH+1;
GetComputerName(ComputerName,Size);
Result:=StrPas(ComputerName);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
edtName.Text:=GetHostName;
edtIP.Text:=NameToIP(edtName.Text);
end;
function Tform1.NameToIP(Name:string):String;
var
WSAData:TWSAData;
HostEnt:PHostEnt;
begin
result:='';
WSAStartup(2,WSAData);
HostEnt:=GetHostByName(Pchar(Name));
if HostEnt <> nil then
begin
with HostEnt^ do
result:=Format('%d.%d.%d.%d',[Byte(h_addr^[0]),byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]);
end;
WSACleanup;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
FOrm1.Close;
end;
end.
为什么获得的IP正确,而获得的主机名是十六进制乱码啊??