首先是本地IP的获取:
我们选择最简单的办法,不使用WinSock而直接使用Indy Misc下的TIdIPWatch控件。
然而获取本机名称的方法却一时没能在Indy的控件中找到。
于是我们回到原始的Windows.GetComputerName方法来取得。
代码如下:
unitUnit1;(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,IdCustomTCPServer,IdTCPServer,IdIPWatch,IdBaseComponent,
IdComponent,IdCustomTransparentProxy,IdSocks,StdCtrls;
type
TForm1=class(TForm)
IdIPWatch1:TIdIPWatch;
Edit1:TEdit;
Edit2:TEdit;
procedureFormCreate(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
{$R*.dfm}
procedureTForm1.FormCreate(Sender:TObject);
var
hostname:array[0..MAX_COMPUTERNAME_LENGTH]ofchar;
length:DWORD;
begin
length:=SizeOf(hostname);
Windows.GetComputerName(hostname,length);
Edit1.Text:=hostname;
Edit2.Text:=IdIPWatch1.LocalIP;
end;(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
end.