problem with GetIpForwardTable Implementation in Delphi 7
I have put together this code for viewing Routing Table in My delphi App.
PMIB_IPFORWARDROW = ^TMibIpForwardRow;
TMibIpForwardRow=packed 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=^TMibIpForwardTable ;
TMibIpForwardTable=packed record
dwNumEntries:DWORD;
table:array [0..ANY_SIZE-1] of TMibIpForwardRow;
end;
The Declaration of the Function is :
getIpForwardTable: function (pIpForwardTable:PMIB_IPFORWARDTABLE; pdwSize:PULONG; bOrder:BOOL):DWORD; stdcall;
And the Function for Implementation is:
procedure GetRouteTable;
var
Error:DWORD;
pRouteTable:PMIB_IPFORWARDTABLE ;
dwSize:ULONG;
//dwSize:ULONG;
TableSize:Integer;
begin
try
dwSize:=0;
GetMem(pRouteTable,SizeOf(TMibIpForwardTable));
Error:=GetIpForwardTable(pRouteTable,@dwSize,FALSE);// Error at this line
if Error=NO_ERROR t开发者_JAVA百科hen
begin
ShowMessage(IntToStr(Error));
end
else
ShowMessage(IntToStr(Error));
except
on E:Exception do
ShowMessage(E.ClassName+':'+E.Message);
end;
end;
I am getting Error code=122. Please help in correcting this Error.
Thanks in Advance
edit
I have tried this combo too but it didnt work
Error:=GetIpForwardTable(nil,@dwSize,FALSE);
if Error=ERROR_INSUFFICIENT_BUFFER then
ShowMessage(IntToStr(Error))
else
begin
GetMem(pRouteTable,dwSize);
Error:=getIpForwardTable(pRouteTable,@dwSize,False);
if Error=NO_ERROR then
begin
ShowMessage(IntToStr(Error));
end
else
ShowMessage(IntToStr(Error));
Error code 122 is ERROR_INSUFFICIENT_BUFFER
. The documentation for GetIpForwardTable
states that this error code is returned when the following occurs.
The buffer pointed to by the pIpForwardTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the pdwSize parameter.
The solution is to read the value of dwSize
that is returned, and call again with a buffer of that size.
The other problem with your code is that you do not pass in a valid buffer. Notice that the variable pRouteTable
is never assigned to. In your edit, you do call GetMem
to allocate it, but in the wrong place. You need something like this:
Error := GetIpForwardTable(nil, @dwSize, False);
if Error<>ERROR_INSUFFICIENT_BUFFER then
RaiseLastOSError(Error);
GetMem(pRouteTable,dwSize);
Error := GetIpForwardTable(pRouteTable, @dwSize, False);
if Error<>ERROR_SUCCESS then
RaiseLastOSError(Error);
精彩评论