獲取本機的位址

2021-04-02 21:09:16 字數 852 閱讀 5187

剛學網路程式設計,試著做了乙個獲取本機的名及位址的方法,開始怎麼也獲取的不對,原來錯誤在這裡

i:=longint(pointer(p^.h_addr_list^)^);  這句開始少了 pointer 了,因為 h_addr_list 本來就是乙個指標 ^pchar,所以取得了這個指標的值後再去獲取這個指標所指向的內容

不管**好不好吧,反正能獲取 ip 了

procedure tform1.button1click(sender: tobject);

vars:pchar;

name:string;

ip:string;

p:phostent;

i:longint;

begin

//getmem(s,254);

//winsock.gethostname(s,strlen(s));

setlength(name,254);

winsock.gethostname(pchar(name),254);

//name:=sysutils.strpas(s);

setlength(name,strlen(pchar(name)));

showmessage(name);

p:=winsock.gethostbyname(pchar(name));

i:=longint(pointer(p^.h_addr_list^)^);

s:=winsock.inet_ntoa(tinaddr(i));

memo1.lines.add(strpas(p^.h_name));

memo1.lines.add(string(s));

// freemem(s);

end;

獲取本機IP位址

cstring strlocalip char szhostname 64 memset szhostname,0x00,sizeof szhostname gethostname szhostname,sizeof szhostname phostent phost gethostbyname s...

獲取本機IP位址

第一步 載入winsock2靜態鏈結庫檔案。include pragma comment lib,ws2 32.lib bool loadwinsock 第二步 獲取本地的主機名稱。hostent phostent null char szhostname 256 memset szhostname...

獲取本機IP MAC位址

1 include 2 include 3 include iphlpapi.h 包含對ip幫助函式的定義 4 pragma comment lib,iphlpapi.lib 5 pragma comment lib,ws2 32.lib 全域性資料 6 u char g uclocalmac 6 ...