0
votes

I have made the following function in Delphi 7. UDP_TABLE_OWNER_PID = 5;

  {For UDP}
     UDP_TABLE_OWNER_PID = 5;
    type

       UDP_TABLE_CLASS = Integer;

      PMibUdpRowOwnerPid = ^TMibUdpRowOwnerPid;
        TMibUdpRowOwnerPid  = record//packed record
            dwState     : DWORD;
        dwLocalAddr : DWORD;
  //dwLocalPort : DWORD;
            dwRemoteAddr: DWORD;
 // dwRemotePort: DWORD;
            dwOwningPid : DWORD;
           end;


     PMIB_UDPTABLE_OWNER_PID  = ^MIB_UDPTABLE_OWNER_PID;
     MIB_UDPTABLE_OWNER_PID =record// packed record
        dwNumEntries: DWord;
        table: array [0..ANY_SIZE - 1] OF TMibUdpRowOwnerPid;
         end;

  procedure TFmainViewTCP.ShowCurrentUDPConnections(StatusType:String);
    var
   Error        : DWORD;
   TableSize    : DWORD;
      i            : integer;
   IpAddress    : in_addr;
     RemoteIp     : string;
       LocalIp      : string;
      ver:Integer;
       ProcName:string;
     FExtendedUdpTable : PMIB_UDPTABLE_OWNER_PID;
     lItem:TListItem;  {for displaying the output}
       countRow:Integer;
     begin
      i:=0;
   TableSize := 0;
   countRow:=0;

    Error := GetExtendedUdpTable(nil, @TableSize, False,AF_INET,UDP_TABLE_OWNER_PID, 0);

    if Error <> ERROR_INSUFFICIENT_BUFFER then
     begin
          if Error=ERROR_INVALID_PARAMETER then
          begin
           ShowMessage(IntToStr(Error));//Error code is 87, shown here
          end;

           Exit;
        end;

The code gives error, i cannot figure out why. Help will be appreciated.

Thanks in advance

3
Is your code indented like that? If so then that is a big part of your problem. If not then please spend the time to make sure that the indentation in the question is correct. - David Heffernan
You forgot to comment out dwState field in your boilerplate. - Premature Optimization
Indentation is not really my concern now. please help. - CyprUS
@CyprUS Indentation is your concern. By producing such unreadable code you make it hard for us to read and parse your code. Since you are asking us to find your bugs you should take the extra effort of making your code suitably readable. - David Heffernan

3 Answers

1
votes

Here is something to get u started it was easier for me to code something from scratch then to fix your code...

type
  MIB_UDPROW_OWNER_PID = record
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwOwningPID: DWORD;
  end;

type
  MIB_UPDATE_TABLE = record
    dwNumEntries: DWORD;
    UDP_TABLE: array [0 .. 0] of MIB_UDPROW_OWNER_PID;
  end;

  PMIB_UPDATE_TABLE = ^MIB_UPDATE_TABLE;

function GetExtendedUdpTable(pUdpTable:Pointer;dwSize:PDWORD;bOrder:Boolean;uAlf:ULONG;TableClass:Integer;Reserved:ULONG):DWORD;stdcall;external 'iphlpapi.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  PID_ : PMIB_UPDATE_TABLE;
  dwSize:DWORD;
  i: Integer;
begin
  GetExtendedUdpTable(nil,@dwSize,false,2,1,0);
  GetMem(PID_,dwSize);
  GetExtendedUdpTable(PID_,@dwSize,false,2,1,0);
  for i := 0 to PID_^.dwNumEntries - 1 do
    ShowMessage(IntToStr(PID_^.UDP_TABLE[i].dwOwningPID));
end;
0
votes

Your declaration of MIB_UDPROW_OWNER_PID is incorrect. It does not have the dwState member. That's the cause of the error. Also, there's no dwRemoteAddr, instead you should have dwLocalPort.

0
votes

The error was that UDP_TABLE_OWNER_PID = 1; instead of 5 as i had set. Also, dwState as David had pointed out.