获取ip.txt

来自「用来获取本机器IP地址」· 文本 代码 · 共 49 行

TXT
49
字号

// Get host address 
char    Buffer[64]; 
HOSTENT FAR *pHost; 
CString sIPAddress; 

gethostname(Buffer, 64); 
pHost  = gethostbyname(Buffer); 
sIPAddress = inet_ntoa(*(struct in_addr*)pHost->h_addr_list[0]); 

// If have many ip address, then 
// look h_addr_list[1], etc, until encounter a NULL pointer 

          


取得本地计算机的名字: 
  Windows 95 API: GetComputerName 
  题外话:取得本地登录用户名:GetUserName 

取得本地ip地址(转贴): 
function LocalIP : string; 
type 
    TaPInAddr = array [0..10] of PInAddr; 
    PaPInAddr = ^TaPInAddr; 
var 
    phe  : 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 
      result:=StrPas(inet_ntoa(pptr^[I]^)); 
      Inc(I); 
    end; 
    WSACleanup; 
end;  
 
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?