1 uses 2 Windows, WinSock, IpHlpApi, IpRtrMib; 3 4 //获取网卡序号 5 function GetGPIndex(const AIP: AnsiString): Cardinal; 6 var 7 ipinfo, info: PIpAdapterInfo; 8 ips: TIpAddrString; 9 len, nerr: Cardinal; 10 str: AnsiString; 11 ok: Boolean; 12 begin 13 Result := 0; 14 15 len := 0; 16 nerr := GetAdaptersInfo(nil, len); 17 GetMem(ipinfo, len); 18 nerr := GetAdaptersInfo(ipinfo, len); 19 ok := False; 20 if nerr = 0 then 21 begin 22 info := ipinfo; 23 while(info <> nil) do 24 begin 25 ips := info^.IpAddressList; 26 str := StrPas(ips.IpAddress.S); 27 str := Trim(Str); 28 while(str <> '')do 29 begin 30 if SameText(str, AIP) then 31 begin 32 Result := info^.Index; 33 ok := True; 34 Break; 35 end 36 else begin 37 if Assigned(ips.Next) then 38 begin 39 ips := ips.Next^; 40 str := StrPas(ips.IpAddress.S); 41 str := Trim(Str); 42 end 43 else str := ''; 44 end; 45 end; 46 if ok then 47 Break; 48 info := info^.Next; 49 end; 50 end; 51 FreeMem(ipinfo, len); 52 end; 53 54 // 获取跃点 55 function GetHops(const AIP: AnsiString): Cardinal; 56 var 57 pIpForwardTable: PMibIpForwardTable; 58 dwSize, dwStatus,NewGateway: Cardinal; 59 bOrder, needfree: Boolean; 60 i: Integer; 61 begin 62 Result := 30; 63 pIpForwardTable := nil; 64 dwSize := 0; 65 dwStatus := 0; 66 NewGateway := $DDCCBBAA; 67 needfree := False; 68 dwStatus := GetIpForwardTable(pIpForwardTable, dwSize, False); 69 if (dwStatus = ERROR_INSUFFICIENT_BUFFER) then 70 begin 71 GetMem(pIpForwardTable, dwSize); 72 needfree := True; 73 dwStatus := GetIpForwardTable(pIpForwardTable, dwSize, False); 74 end; 75 if dwStatus = ERROR_SUCCESS then 76 begin 77 NewGateway := inet_addr(PAnsiChar(AIP)); 78 for i := 0 to pIpForwardTable^.dwNumEntries - 1 do 79 if pIpForwardTable^.table[i].dwForwardDest = NewGateway then 80 begin 81 Result := pIpForwardTable^.table[i].dwForwardMetric1; 82 Break; 83 end; 84 end; 85 if needfree then 86 FreeMem(pIpForwardTable); 87 end; 88 89 //初始化MibIP AIP: 路由节点 AMask: 节点掩码 ANextHop: 下一节点 ANetwork:网卡序号 AHop: 跃点 90 function InitMibIp(const AIP, AMask, ANextHop: AnsiString; ANetwork, AHop: Cardinal): TMibIpForwardRow; 91 begin 92 FillChar(Result, SizeOf(Result), 0); 93 Result.dwForwardDest := inet_addr(PAnsiChar(AIP)); 94 Result.dwForwardMask := inet_addr(PAnsiChar(AMask)); 95 Result.dwForwardNextHop := inet_addr(PAnsiChar(ANextHop)); 96 Result.dwForwardPolicy := 0; 97 Result.dwForwardIfIndex := ANetwork; 98 Result.dwForwardType := 4; //the next hop is not the final dest 99 Result.dwForwardProto := 3; //PROTO_IP_NETMGMT 100 Result.dwForwardAge := 0; 101 Result.dwForwardNextHopAS := 0; 102 Result.dwForwardMetric1 := AHop; 103 Result.dwForwardMetric2 := $FFFFFFFF; 104 Result.dwForwardMetric3 := $FFFFFFFF; 105 Result.dwForwardMetric4 := $FFFFFFFF; 106 Result.dwForwardMetric5 := $FFFFFFFF; 107 end; 108 109 //添加路由 110 function AddRoute(const AIP, AMask, ANextHop: AnsiString; ANetwork, AHop: Cardinal): Boolean; 111 begin 112 Result := CreateIpForwardEntry(InitMibIp(AIP, AMask, ANextHop, ANetwork, AHop) = NO_ERROR; 113 end; 114 115 //删除路由 116 function DellRoute(const AIP, AMask, ANextHop: AnsiString; ANetwork, AHop: Cardinal): Boolean; 117 begin 118 Result := DeleteIpForwardEntry(InitMibIp(AIP, AMask, ANextHop, ANetwork, AHop) = NO_ERROR; 119 end;