扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
![]() 图1 主界面 |
function TfrmMain.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; |
function TfrmMain.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; |
unit Unit1; interface uses Windows, Messages, Sys Utils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, WinSock; type TfrmMain = class(TForm) btnStart: TButton; edtIP: TEdit; edtName: TEdit; Label1: TLabel; Label2: TLabel; function GetHostName:String; procedure btnStartClick(Sender: TObject); function NameToIP(Name:string):String; private { Private declarations } public { Public declarations } end; var frmMain: TfrmMain; implementation {$R *.dfm} function TfrmMain.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 TfrmMain.btnStartClick(Sender: TObject); begin edtName.Text:=GetHostName; edtIP.Text:=NameToIP(edtName.Text); end; function TfrmMain.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; end. |
![]() 图2 程序运行的初始画面 ![]() 图3 程序运行结果 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。