• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi获取跃点、网卡序号,添加、删除路由节点

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  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;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
让UltraEdit-32成为Delphi7编译器的工具设置发布时间:2022-07-18
下一篇:
delphi两个取字串长度的函数strlen,length发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap