0

Using the Delphi code at Port Forward using UPnP - close port I've managed to add and remove UPnP port forward entries. My experience with variants is nil, how do I iterate through the existing entries?

kbriggs
  • 1,267
  • 1
  • 12
  • 16

1 Answers1

0

I managed to figure it out from pieces of other code found:

uses ComObj, ActiveX;

procedure ShowUPnPEntries;
var
  Nat, Ports: Variant;
  Port: OleVariant;
  Enum: IEnumVariant;
  iValue: longword;
  Msg, Desc, ExtIP, IntClient, ProtoCol: string;
  Enabled: boolean;
  ExtPort, IntPort: integer;
begin
  Nat := CreateOleObject('HNetCfg.NATUPnP');
  Ports := Nat.StaticPortMappingCollection;
  Msg := '';
  if not VarIsClear(Ports) then
  begin
    Enum := IUnknown(Ports._NewEnum) as IEnumVariant;
    while ENum.Next(1, Port, iValue) = 0 do
    begin
      Desc := Port.Description;
      Enabled := Port.Enabled;
      ExtIP := Port.ExternalIPAddress;
      ExtPort := Port.ExternalPort;
      IntClient := Port.InternalClient;
      IntPort := Port.InternalPort;
      Protocol := Port.Protocol;
      if Msg <> '' then Msg := Msg + #13;
      Msg := Msg + Desc + ' ' + IntToStr(ExtPort) + '/' + IntToStr(IntPort) + ' ' +
        Protocol + ' ' + ExtIP + '/' + IntClient + ' ';
      if Enabled then Msg := Msg + 'Enabled' else Msg := Msg + 'Disabled';
    end;
  end;
  ShowMessage(Msg);
end;
kbriggs
  • 1,267
  • 1
  • 12
  • 16