虽然现在一机多网卡已经很普遍(像Notebook带有线、无线、蓝芽等),但是找一个现成的能够一次过读出所有网卡IP地址的函数实在是难,无奈自己写了一个,好东西谁用谁知道。
[c-sharp] view plain copy
- //uses WinSock;
- type
- PAddrInfoW = ^ADDRINFOW;
- ADDRINFOW = record
- ai_flags : Integer; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
- ai_family : Integer; // PF_xxx
- ai_socktype : Integer; // SOCK_xxx
- ai_protocol : Integer; // 0 or IPPROTO_xxx for IPv4 and IPv6
- ai_addrlen : ULONG; // Length of ai_addr
- ai_canonname : PWideChar; // Canonical name for nodename
- ai_addr : PSOCKADDR; // Binary address
- ai_next : PAddrInfoW; // Next structure in linked list
- end;
- PPaddrinfoW = ^PAddrInfoW;
- TAddrInfoW = ADDRINFOW;
- function GetAddrInfoW(NodeName: PWideChar; ServiceName: PWideChar; Hints: PaddrinfoW; ppResult: PPaddrinfoW): Integer; stdcall; external ‘WS2_32.DLL‘ name ‘GetAddrInfoW‘;
- procedure FreeAddrInfoW(ai: PaddrinfoW); stdcall; stdcall; external ‘WS2_32.DLL‘ name ‘FreeAddrInfoW‘;
- procedure GetIPList(List: TStrings);
- var
- Buffer: array[0..63] of AnsiChar;
- Hostname: string;
- Hints: TAddrInfoW;
- LAddrInfo: pAddrInfoW;
- RetVal: Integer;
- GInitData: TWSADATA;
- begin
- WSAStartup($101, GInitData);
- GetHostName(Buffer, SizeOf(Buffer));
- Hostname := string(Buffer);
- ZeroMemory(@Hints, SizeOf(TAddrInfoW));
- Hints.ai_family := 2{AF_INET};
- Hints.ai_socktype := 1{SOCK_STREAM};
- LAddrInfo := nil;
- RetVal := GetAddrInfoW(PChar(Hostname), nil, @Hints, @LAddrInfo);
- try
- if (RetVal = 0) and (LAddrInfo <> nil) then
- repeat
- with TInAddr(LAddrInfo^.ai_addr^.sin_addr).S_un_b do
- List.Add(Format(‘%d.%d.%d.%d‘, [Byte(s_b1), Byte(s_b2), Byte(s_b3), Byte(s_b4)]));
- LAddrInfo := LAddrInfo^.ai_next;
- until LAddrInfo = nil;
- finally
- FreeAddrInfoW(LAddrInfo);
- WSACleanup;
- end;
- end;
PS:我用D2009,用低版本D的朋友麻烦自己改改了。
http://blog.csdn.net/nhconch/article/details/4897682
时间: 2024-10-03 22:47:59