I want to Port Forward a port to an open server connection in Delphi (I'm using TServerSocket
).
I'm using this procedure that I found in another StackOverflow question:
procedure AddUPnPEntry(Port: Integer; const Name: ShortString; LAN_IP: string);
var
Nat: Variant;
Ports: Variant;
begin
if not (LAN_IP = '127.0.0.1') then
begin
Nat := CreateOleObject('HNetCfg.NATUPnP');
Ports := Nat.StaticPortMappingCollection;
if not VarIsClear(Ports) then
begin
CoInitialize(nil);
try
ShowMessage(IntToStr(Ports.count));
Ports.Add(Port,'TCP',Port,LAN_IP,True,name);
finally
CoUninitialize;
end;
end;
end;
end;
I've already tried reading MSDN's documentation, using different code snippets (similar to this).
I want to understand why this code doesn't work. All I get is an Access Violation error message.
Just some extra info:
My router does support UPnP. I'm using Windows 7 and Delphi 7.