首先是本地IP的獲取:
我們選擇最簡單的辦法,不使用WinSock而直接使用Indy Misc下的TIdIPWatch控件。
然而獲取本機名稱的方法卻一時沒能在Indy的控件中找到。
於是我們回到原始的Windows.GetComputerName方法來取得。
代碼如下:
unitUnit1;
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;
end.