Форум: "Сети";
Текущий архив: 2007.05.27;
Скачать: [xml.tar.bz2];
Вниз
Как задать gateway сетевому интерфейсу? Найти похожие ветки
← →
kay © (2006-10-17 01:51) [0]нужно реализовать смену gateway в программе
← →
kay © (2006-10-17 02:12) [1]нашёл:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecomm5/html/wce50conchangingthedefaultgateway.asp
← →
kay © (2006-10-17 02:53) [2]хотя нед. совсем запутался с преобразованием из c++ в дельфи. может есть готовый вариант?
← →
kay © (2006-10-17 04:05) [3]Получилось шлюз задать. Но не совсем так как нужно. Он то задаётся, а вот в натсройках IP сетевого адаптера всё равно висит. И причем именно в настроках.
Вот код процедуры:procedure setgateway;
var
pIpForwardTable:PMIB_IPFORWARDTABLE;
pRow:PMIB_IPFORWARDROW;
dwSize:ULONG;
bOrder:boolean;
dwStatus:DWORD;
NewGateway,tmp:DWORD;
i:integer;
begin
bOrder:=false;
NewGateWay:=$0100A8C8;
pRow:=nil;
dwSize:=0;
if GetIpForwardTable(nil, dwSize, bOrder) = ERROR_INSUFFICIENT_BUFFER then
begin
// Áåðåì íóæíîå êîë-âî
GetMem(pIpForwardTable, dwSize);
try
// âûïîëíåíèå ôóíêöèè
dwStatus:=GetIpForwardTable(pIpForwardTable, dwSize, bOrder);
if dwStatus=ERROR_SUCCESS then
begin
// Search for the row in the table we want. The default gateway has a destination
// of 0.0.0.0. Notice that we continue looking through the table, but copy only
// one row. This is so that if there happen to be multiple default gateways, we can
// be sure to delete them all.
for i:=0 to pIpForwardTable.dwNumEntries-1 do
begin
if pIpForwardTable.table[i].dwForwardDest=0 then
begin
// We have found the default gateway.
if (pRow=nil) then
begin
// Allocate some memory to store the row in. This is easier than filling
// in the row structure ourselves, and we can be sure to change only the
// gateway address.
GetMem(pRow, sizeof(MIB_IPFORWARDROW));
// Copy the row.
CopyMemory(pRow, @(pIpForwardTable.table[i]), sizeof(MIB_IPFORWARDROW));
end;
// Delete the old default gateway entry.
dwStatus:=DeleteIpForwardEntry((pIpForwardTable.table[i]));
if (dwStatus = ERROR_SUCCESS) then
break;
end;
end;
// Set the nexthop field to our new gateway. All the other properties of the route will
// be the same as they were previously.
dwStatus:=0;
if pRow<>nil then
begin
pRow.dwForwardNextHop := NewGateway;
// Create a new route entry for the default gateway.
dwStatus:=SetIpForwardEntry((pRow)^);
end;
if (dwStatus = NO_ERROR) then
ShowMessage("Gateway changed successfully")
else
ShowMessage("Gateway change failed");
end;
finally
FreeMem(pIpForwardTable);
FreeMem(pRow);
end;
end;
end;
Заголовки:
PMIB_IPFORWARDROW = ^MIB_IPFORWARDROW;
MIB_IPFORWARDROW = record
dwForwardDest:DWORD;
dwForwardMask:DWORD;
dwForwardPolicy:DWORD;
dwForwardNextHop:DWORD;
dwForwardIfIndex:DWORD;
dwForwardType:DWORD;
dwForwardProto:DWORD;
dwForwardAge:DWORD;
dwForwardNextHopAS:DWORD;
dwForwardMetric1:DWORD;
dwForwardMetric2:DWORD;
dwForwardMetric3:DWORD;
dwForwardMetric4:DWORD;
dwForwardMetric5:DWORD;
end;
PMIB_IPFORWARDTABLE = ^MIB_IPFORWARDTABLE;
MIB_IPFORWARDTABLE = record
dwNumEntries:DWORD;
table: array[0..ANY_SIZE - 1] of MIB_IPFORWARDROW;
end;
......................................................................
function GetIpForwardTable(pIpForwardTable: PMIB_IPFORWARDTABLE;
var pdwSize: ULONG; bOrder: BOOL): DWORD; stdcall; external IPHelper;
function DeleteIpForwardEntry(const pRoute: MIB_IPFORWARDROW): DWORD;
stdcall; external IPHelper;
function SetIpForwardEntry(const pRoute: MIB_IPFORWARDROW): DWORD;
stdcall; external IPHelper;
← →
kay © (2006-10-19 14:06) [4]up
← →
user51 (2006-11-21 16:11) [5]Все легко и просто
ff:="route.exe -f -p add 0.0.0.0 mask 0.0.0.0 "+gateway+" metric 1";
winexec(pchar(ff),0);
и зачем изобретать велосипед с километровым кодом
Страницы: 1 вся ветка
Форум: "Сети";
Текущий архив: 2007.05.27;
Скачать: [xml.tar.bz2];
Память: 0.47 MB
Время: 0.041 c