【发布时间】:2021-11-19 04:43:19
【问题描述】:
使用Port Forward using UPnP - close port 的Delphi 代码,我设法添加和删除了UPnP 端口转发条目。我对变体的经验为零,如何遍历现有条目?
【问题讨论】:
标签: delphi
使用Port Forward using UPnP - close port 的Delphi 代码,我设法添加和删除了UPnP 端口转发条目。我对变体的经验为零,如何遍历现有条目?
【问题讨论】:
标签: delphi
我设法从找到的其他代码中弄明白了:
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;
【讨论】: