网卡绑定的TCP/IP协议的IP地址是交换机、陆游器或代理服务器里设置的,是为了防止盗用
IP地址。如果你设置的IP地址及MAC在交换机或代理服务器中设置的不一致,你的数据可能就
不能通过。
获取IP地址和掩码可以用IP Help Api中的 GetIpAddrTable函数:请看MSDN 2000以上版本
// Returns the IP address table. The caller must free the memory.
function GetIpAddrTableWithAlloc: PMibIpAddrTable;
var
Size: ULONG;
begin
Size := 0;
GetIpAddrTable(nil, Size, True);
Result := AllocMem(Size);
if GetIpAddrTable(Result, Size, True) <> NO_ERROR then
begin
FreeMem(Result);
Result := nil;
end;
end;
绑定IP和MAC地址可以用参看下面的程序段:
// Returns the interface index of the first ...
function FirstNetworkAdapter(IpAddrTable: PMibIpAddrTable): Integer;
var
I: Integer;
IfInfo: TMibIfRow;
begin
// TODO this is a "stupid" implementation, can be done much easier by using
// enumerating the interface table directly
Result := -1;
for I := 0 to IpAddrTable^.dwNumEntries - 1 do
begin
{$R-}IfInfo.dwIndex := IpAddrTable^.table.dwIndex;{$R+}
if GetIfEntry(@IfInfo) = NO_ERROR then
begin
if IfInfo.dwType in [MIB_IF_TYPE_ETHERNET, MIB_IF_TYPE_TOKENRING] then
begin
Result := IfInfo.dwIndex;
Break;
end;
end;
end;
end;
// Adds an entry to the ARP table. InetAddr is the IP address to add, EtherAddr
// is the ethernet address to associate with the InetAddr and IntfAddr is the
// index of the adapter to add this ARP entry to. If IntfAddr is an empty string
// the function uses the first network adapter (tokenring or ethernet) it can
// find.
procedure SetArpEntry(const InetAddr, EtherAddr, IntfAddr: string);
var
Entry: TMibIpNetRow;
IpAddrTable: PMibIpAddrTable;
begin
FillChar(Entry, SizeOf(Entry), 0);
Entry.dwAddr := StringToIpAddr(InetAddr);
Assert(Entry.dwAddr <> DWORD(INADDR_NONE));
Entry.dwPhysAddrLen := 6;
StringToPhysAddr(EtherAddr, TPhysAddrByteArray(Entry.bPhysAddr));
Entry.dwType := MIB_IPNET_TYPE_STATIC;
if IntfAddr <> '' then
Entry.dwIndex := StrToInt(IntfAddr)
else
begin
IpAddrTable := GetIpAddrTableWithAlloc;
Assert(IpAddrTable <> nil);
Entry.dwIndex := FirstNetworkAdapter(IpAddrTable);
FreeMem(IpAddrTable);
end;
WriteLn(SysErrorMessage(SetIpNetEntry(Entry)));
end;