delphi獲取本地IP地址的幾種方法

2021-06-18 19:09:28 字數 3934 閱讀 8959

1樓:衝浪**優化

1、通過登錄檔獲取或修改ip

想到windows會把系統網絡卡相關資訊存入登錄檔,肯定可通過登錄檔讀取具體ip資訊。大致思路是找hkey_local_machine\software\microsoft\windows nt\currentversion\networkcards ,由networkcards得分支表示各個網絡卡的資訊通過servicename到 hkey_local_machine\system\currentcontrolset\services\tcpip\parameters\inte***ces 下找到對應的網絡卡配置詳細資訊,通過對登錄檔的read與write實現讀取或修改ip地址等資訊。

2、通過解析批處理命令結果獲取

在cmd命令列輸入ipconfig可以顯示出當前電腦ip地址,可以考慮先執執行命令"ipconfig /all >ips.txt",然後解析ips.txt檔案得到本機的ip資訊,此方法較繁雜。

3、通過delphi控制元件獲取ip

找到fastnet 下的 tpowersock控制元件,利用 該控制元件localip屬性返回 本機(主要指區域網) ip地址 。 放一個控制元件在窗體上可得到本機ip,方便簡單。

4、通過winsock函式獲取ip

此方法在程式中常用,一般函式如下:

//多網絡卡 將ip地址寫入到列表,求本機ip的話返回ip列表中的第一個即可,該函式要引用 winsock

function getlocaliplist(var iplist:tstringlist):integer;

type

tapinaddr = array[0..10] of pinaddr;

papinaddr = ^tapinaddr;

varhostname: array [0..max_path] of char;

namelen: integer;

wsdata: twsadata;

lphostent: phostent;

i: integer;

pptr: papinaddr;

begin

result := 0;

if wsastartup(makeword(2,0), wsdata) <> 0 then exit;

trynamelen := sizeof(hostname);

fillchar(hostname, namelen, 0);

namelen := gethostname(hostname, namelen);

if namelen = socket_error then exit;

lphostent := gethostbyname(hostname);

if lphostent = nil then exit;

i := 0;

pptr := papinaddr(lphostent^.h_addr_list);

iplist.clear;

while pptr^[i] <> nil do

begin

iplist.add( inet_ntoa(pptr^[i]^));

inc(i);

end;

result := iplist.count;

finally

wsacleanup;

end;

end;

2樓:磊鑫廣告製作

用這個function localip : string;

type

tapinaddr = array [0..10] of pinaddr;

papinaddr = ^tapinaddr;

varphe : phostent;

pptr : papinaddr;

buffer : array [0..63] of char;

i : integer;

ginitdata : twsadata;

begin

wsastartup($101, ginitdata);

result := ' ';

gethostname(buffer, sizeof(buffer));

phe :=gethostbyname(buffer);

if phe = nil then exit;

pptr := papinaddr(phe^.h_addr_list);

i := 0;

while pptr^[i] <> nil do begin

if i=0

then result:=strpas(inet_ntoa(pptr^[i]^))

else result:=result+ ', '+strpas(inet_ntoa(pptr^[i]^));

inc(i);

end;

wsacleanup;

end;

3樓:匿名使用者

function localip : string;

type

tapinaddr = array [0..10] of pinaddr;

papinaddr = ^tapinaddr;

varphe : phostent;

pptr : papinaddr;

buffer : array [0..63] of char;

i : integer;

ginitdata : twsadata;

begin

wsastartup($101, ginitdata);

result := ' ';

gethostname(buffer, sizeof(buffer));

phe :=gethostbyname(buffer);

if phe = nil then exit;

pptr := papinaddr(phe^.h_addr_list);

i := 0;

while pptr^[i] <> nil do begin

if i=0

then result:=strpas(inet_ntoa(pptr^[i]^))

else result:=result+ ', '+strpas(inet_ntoa(pptr^[i]^));

inc(i);

end;

wsacleanup;

end;

delphi 如何獲取本地計算機名和ip地址

4樓:雲南新華電腦學校

uses windows;

function gethostname:string;

varcomputername: array[0..max_computername_length+1] of char;

size: cardinal;

begin

result:='';

size := max_computername_length+1;

getcomputername(computername, size);

result:=strpas(computername);

end;

delphi 獲取區域網所有主機名稱及ip方法

5樓:匿名使用者

嫌棄搜尋慢,那你就用多執行緒好了

6樓:匿名使用者

分都不給,不幫。。。

怎麼獲取ip位址,ip位址怎樣獲取ip位址

在搜尋 ip 便會直接得出電腦外網的ip位址。獲取ip開啟開始選單,在搜尋框內輸入 cmd 在彈出的黑框中輸入 ipconfig 回車即可看到自己的ip位址。獲取ip 電腦工作列中開啟 網路和共享中心 無線網路連線 然後點選 詳細資訊 就可以看到電腦詳細的ip位址了。獲取ip 輸入ip位址或 查詢地...

電腦的本地連線能夠獲取到路由器的IP位址,但不能進路由器,也不能上網

家用小路由器是目前最容易出故障的網路裝置,尤其是軟故障,專業的說法是某段程式跑飛了。解決方法是 1.確認電腦沒有問題。就是同一臺電腦換個地方就能上網,很多類似情形下,收拾一下電腦就好了 清理 防毒 重灌系統等 2.電腦沒有問題,則切斷路由器電源,再供電。就是重新啟動,一般不需要重新配置。3.如果不行...

怎麼看本地ip位址,如何檢視本機IP位址?

依次操作如下 首先,開啟 開始 選單 選擇 執行 輸入 cmd 按下 enter 回車鍵,這樣做可以開啟 命令提示符 在命令提示符中輸入 ipconfig 按下 enter 回車鍵 見上圖 出現以下內容 ethernet adapter 本地連線 connection specific dns su...