Delphi2009中取得本机IP及名称

孙家三少3

孙家三少3

2016-02-19 14:42

下面图老师小编跟大家分享Delphi2009中取得本机IP及名称,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

  首先是本地IP的获取:

  我们选择最简单的办法,不使用WinSock而直接使用Indy Misc下的TIdIPWatch控件。

  然而获取本机名称的方法却一时没能在Indy的控件中找到。

  于是我们回到原始的Windows.GetComputerName方法来取得。

  代码如下:

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
unitUnit1;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
interface

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
uses

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Dialogs,IdCustomTCPServer,IdTCPServer,IdIPWatch,IdBaseComponent,

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 IdComponent,IdCustomTransparentProxy,IdSocks,StdCtrls;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
type

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 TForm1=class(TForm)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  IdIPWatch1:TIdIPWatch;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  Edit1:TEdit;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  Edit2:TEdit;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  procedureFormCreate(Sender:TObject);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 private

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  {Privatedeclarations}

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 public

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
  {Publicdeclarations}

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 end;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
var

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Form1:TForm1;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
implementation

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
{$R*.dfm}

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
procedureTForm1.FormCreate(Sender:TObject);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
var

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 hostname:array[0..MAX_COMPUTERNAME_LENGTH]ofchar;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 length:DWORD;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
begin

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 length:=SizeOf(hostname);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Windows.GetComputerName(hostname,length);

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Edit1.Text:=hostname;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
 Edit2.Text:=IdIPWatch1.LocalIP;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
end;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
end.

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

Delphi2009中取得本机IP及名称

编程语言 公网ip 网络编程
Delphi2009中取得本机IP及名称

如何获取本机IP?

公网ip
如何获取本机IP?

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

怎么查看本机ip地址

电脑 公网ip
怎么查看本机ip地址

java中通过网卡名称获取IP地址

编程语言 网络编程
java中通过网卡名称获取IP地址

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

如何查看本机ip?怎么看自己的ip?

电脑入门 公网ip
如何查看本机ip?怎么看自己的ip?

Java判断本机IP地址类型的方法

编程语言 公网ip 网络编程
Java判断本机IP地址类型的方法

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

jquery select选中的一个小问题

jquery select选中的一个小问题

jQuery技巧大放送 学习jquery的朋友可以看下

jQuery技巧大放送 学习jquery的朋友可以看下
下拉加载更多内容 ↓