首先是本地IP的获取:
我们选择最简单的办法,不使用WinSock而直接使用Indy Misc下的TIdIPWatch控件。
然而获取本机名称的方法却一时没能在Indy的控件中找到。
于是我们回到原始的Windows.GetComputerName方法来取得。
代码如下:
- unit Unit1;
- 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;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- var
- hostname: array[0..MAX_COMPUTERNAME_LENGTH] of char;
- length: DWORD;
- begin
- length := SizeOf(hostname);
- Windows.GetComputerName(hostname, length);
- Edit1.Text := hostname;
- Edit2.Text := IdIPWatch1.LocalIP;
- end;
- end.