2

I am having trouble using the NATUPnPLib. I have a piece of code that works perfectly fine while the project is running on .Net 4. But, unfortunately when I try the same piece of code in .Net3.5, I get some assembly reference errors while building the project.

UPnPNATClass upnpnat = new UPnPNATClass();

IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

And i get the following build error:

The type or namespace name 'UPnPNATClass' could not be found (are you missing a using directive or an assembly reference?

Although i have added the reference to the NatUPnP Library from Add Reference --> COM

I have scoured the internet to find a solution but i was unable to find any help. I hope someone here might be able to help out.

P.S: I followed this Article about NAT Router and UPnP

Thanx & Regards.

Neysor
  • 3,893
  • 11
  • 34
  • 66
Master.Aurora
  • 908
  • 2
  • 11
  • 26

2 Answers2

1

Ran into this problem myself and I have found that when you add the reference in vs2010 with 3.5 framework settings, the interface map for the NETCONLib is transposed with the NATUPNPLib interface map, making the objects unavailable. Adding the reference in the 4.0 framework exposes the correct interface map for the to libraries so this would appear to be a fault in the tlib importer when working in 3.5.

1

There is no reason you should have a problem with this in 3.5, it supports this COM component well. Check list:

  • Make sure you added the reference and not just tried to use the interop library that your 4.0 project generated. That interop library can't be used since it has the newer metadata format
  • Make sure you didn't forget to add using NATUPNPLib; at the top of your source code file
  • As an alternative, remove the COM references and add it back with Project + Add Reference, Browse tab, select c:\windows\system32\hnetcfg.dll

My money is on the second bullet, easy to forget and easy to mis-spell.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you very much for such an answer. But i have performed all these steps very carefully and still no luck. – Master.Aurora Mar 24 '12 at 08:28
  • I cannot phantom what the problem could be if you followed these bullets carefully. Of course I get no repro at all. Have to throw the towel on this one. – Hans Passant Mar 24 '12 at 08:51
  • Let me rephrase it to be more precise here. The metadata seen for the NATUPNPLib in .Net 4 using the Object Browser is different from what i see for the library in .Net 3.5. Now the problem is that the appproach i follow to forward the ports can't possibly be done using the set of functions & interface that are available in the .Net 3.5 version of the library. So that is where the problems comes in for me. P.S: Unfortunately i also haven't been able to successfully able to do the task using the functions/ interfaces available in the .net 3.5 version – Master.Aurora Mar 24 '12 at 10:32
  • Precision is the core problem here. Something different? What? – Hans Passant Mar 24 '12 at 10:48
  • This is the screen from the object browser view of the NATUPNPLib in a **.Net 4** project [IMG]http://i.imgur.com/WiYNc.png[/IMG] Whereas this is the screen from the object browser view of the NATUPNPLib in a **.Net 3.5** project [IMG]http://i.imgur.com/DyHLv.png[/IMG] – Master.Aurora Mar 24 '12 at 12:30
  • Okay, that clears it up. The 3rd bullet in my answer should have solved the problem. http://stackoverflow.com/questions/2842032/weird-compatibility-problem-with-net-3-5-and-4-0-assemblies-natupnplib/2842101#2842101 – Hans Passant Mar 24 '12 at 13:39