内含得到本机IP地址
电脑名称
子网掩码
广播地址
=====================================
unit NetFunction;
interface
uses Windows, SysUtils, Winsock;
{
使用到WinSock2。
这是一个完整的Delphi单元,将它加入到你的工程中,你可以调用:
EnumInterfaces(var s string): Boolean;
来返回所有IP地址、网络掩码、广播地址和连接状态。
此函数列举出所有的TCP/IP连接,并返回一个由回车换行(CRLF)符分隔的字符串,包含以下信息:
IP, NetMask, BroadCast-Address, Up/Down status,
Broadcast support, Loopback
如果你将这个字符串赋给TMemo(它的Memo.Lines.Text属性),你可以看到更清晰的结果。
使用此函数,你需要Win98/ME/2K, 95 OSR 2 或者NT service pack #3,
因为程序会使用到WinSock 2(WS2_32.DLL)。}
function EnumInterfaces(var IPAddress,IPSubNetMask,BroadcastAddress,Online: string): Boolean;
{从Winsock 2.0导入函数WSAIOCtl -- 在Win98/ME/2K and 95 OSR2, NT srv pack #3下才有Winsock 2}
function WSAIoctl(s: TSocket; cmd: DWORD; lpInBuffer: PCHAR; dwInBufferLen: DWORD;
lpOutBuffer: PCHAR; dwOutBufferLen: DWORD;
lpdwOutBytesReturned: LPDWORD;
lpOverLapped: POINTER;
lpOverLappedRoutine: POINTER): Integer; stdcall; external 'WS2_32.DLL';
function OctToBin(i:Integer):String;
function BinToOct(k:String):Integer;
function GetComputerNameString:String;
function NetGetLocalIP:String;
{Constants taken from C header files}
const
SIO_GET_INTERFACE_LIST = $4004747F;
IFF_UP = $00000001;
IFF_BROADCAST = $00000002;
IFF_LOOPBACK = $00000004;
IFF_POINTTOPOINT = $00000008;
IFF_MULTICAST = $00000010;
type
sockaddr_gen = packed record
AddressIn: sockaddr_in;
filler: packed array[0..7] of char;
end;
type
INTERFACE_INFO = packed record
iiFlags: u_long; // Interface flags
iiAddress: sockaddr_gen; // Interface address
iiBroadcastAddress: sockaddr_gen; // Broadcast address
iiNetmask: sockaddr_gen; // Network mask
end;
implementation
{
1. 打开Winsock
2. 创建一个socket
3. 调用WSAIOCtl获取网络连接
4. 对每个连接,获取它的IP、掩码、广播地址、状态
5. 将信息填充到一个由CDLF分隔的字符串中
6. 结束}
function octtobin(i:integer):string;
var
j:integer;
s:string;
begin
j:=i;s:='';
while j>=2 do
begin
if (j mod 2)=1 then
begin
s:='1'+s;
j:=j div 2;
end
else
begin
s:='0'+s;
j:=j div 2;
end;
end;
s:=chr(ord('0')+j) + s;
if length(s)<8 then
for i:=1 to 8-length(s) do
s:='0'+s;
octtobin:=s;
end;
function BinToOct(k:string):integer;
var
i,j,t:integer;
s:char;
begin
t:=1;
j:=length(k);
j:=0+(ord(k[j])-ord('0'))*t;
for i:=length(k)-1 downto 1 do
begin
s:=k;t:=t*2;
j:=j + ((ord(s)-ord('0'))*t);
end;
bintooct:=j;
end;
function GetComputerNameString:String;
var
ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
Size: Dword;
begin
Size := MAX_COMPUTERNAME_LENGTH+1;
if GetComputerName(ComputerName,Size) then
Result := StrPas(Computername)
end;
function NetGetLocalIP:String;
var
HostEnt: PHostEnt;
Ip: string;
addr: pchar;
Buffer: array [0..63] of char;
GInitData: TWSADATA;
begin
Result :='';
try
WSAStartup(2, GInitData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt := GetHostByName(buffer);
if HostEnt = nil then Exit;
addr := HostEnt^.h_addr_list^;
ip := Format('%d.%d.%d.%d', [byte(addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result:=ip;
finally
WSACleanup;
end;
end;
function EnumInterfaces(var IPAddress,IPSubNetMask,BroadcastAddress,Online: string): Boolean;
var
s: TSocket;
wsaD: WSADATA;
NumInterfaces: Integer;
BytesReturned, SetFlags: u_long;
pAddrInet: SOCKADDR_IN;
pAddrString: PCHAR;
PtrA: pointer;
Buffer: array[0..20] of INTERFACE_INFO;
i,j: Integer;
a:string;
b:String;
c:array[0..3] of String;
d:array[0..3] of String;
e:array[0..3] of String;
begin
result := true; // Initialize
WSAStartup($0101, wsaD); // Start WinSock
s := Socket(AF_INET, SOCK_STREAM, 0); // Open a socket
if (s = INVALID_SOCKET) then exit;
try // Call WSAIoCtl
PtrA := @bytesReturned;
if (WSAIoCtl(s, SIO_GET_INTERFACE_LIST, nil, 0, @Buffer, 1024, PtrA, nil,
nil)
<> SOCKET_ERROR) then
begin // If ok, find out how
NumInterfaces := BytesReturned div SizeOf(INTERFACE_INFO);
for i:=0 to NumInterfaces-1 do
begin
pAddrInet := Buffer.iiAddress.addressIn; // IP ADDRESS
pAddrString := inet_ntoa(pAddrInet.sin_addr);
a:=pAddrString ;
if pAddrString=NetGetLocalIP then
IPAddress := pAddrString;
pAddrInet := Buffer.iiNetMask.addressIn; // SUBNET MASK
pAddrString := inet_ntoa(pAddrInet.sin_addr);
if (a =NetGetLocalIP) and (IPAddress<>'127.0.0.1') then
IPSubNetMask := pAddrString ;
pAddrInet := Buffer.iiBroadCastAddress.addressIn; // Broadcast addr
pAddrString := inet_ntoa(pAddrInet.sin_addr);
if (a =NetGetLocalIP) and (IPAddress<>'127.0.0.1') then
BroadcastAddress := '' ;
SetFlags := Buffer.iiFlags;
if (a =NetGetLocalIP) and (IPAddress<>'127.0.0.1') and ((SetFlags and IFF_UP) = IFF_UP) then
Online := 'UP'
else if (IPAddress =NetGetLocalIP) and (IPAddress<>'127.0.0.1') and ((SetFlags and IFF_UP) <> IFF_UP) then
OnLine := 'DOWN';
end;
end;
b:=IPAddress+'.';
if IPAddress<>'' then
begin
for i:=0 to 3 do
begin
c:=Copy(b,1,Pos('.',b)-1);
b:=Copy(b,Pos('.',b)+1,Length(b));
end;
end;
b:=IPSubNetMask+'.';
if IPSubNetMask<>'' then
begin
for i:=0 to 3 do
begin
d:=Copy(b,1,Pos('.',b)-1);
b:=Copy(b,Pos('.',b)+1,Length(b));
end;
end;
if (IPAddress<>'') and (IPSubNetMask<>'') then
begin
for i:=0 to 3 do
begin
c:=OctToBin(StrToInt(c));
d:=OctToBin(StrToInt(d));
e:=d;
end;
for i:=0 to 3 do
begin
b:=e;
for j:=0 to length(d) do
begin
if (d)[j]='1' then
b[j]:=(c)[j]
else
b[j]:='1';
end;
e:=Copy(b,1,8);
end;
for i:=0 to 2 do
e:=IntToStr(BinToOct(e))+'.';
e:=IntToStr(BinToOct(e[3]));
end;
for i:=0 to 3 do
BroadcastAddress:=BroadCastAddress+e;
except
end;
CloseSocket(s);
WSACleanUp;
result := True;
end;
end.