Deploying NetFwTypeLib to manage the Windows Firewall

Petrus Theron picture Petrus Theron · Nov 28, 2010 · Viewed 15.3k times · Source

My Windows service needs to create/remove certain rules from the Windows firewall. For this I interface with NetFwTypeLib in <windows>\system32\hnetcfg.dll via COM. It works great on my 64-bit Windows 7 machine, but testing on another 64-bit Windows 7 machine throws the following error:

Service cannot be started. System.IO.FileNotFoundException:
Could not load file or assembly 'Interop.NetFwTypeLib, 
   Version=1.0.0.0, Culture=neutral,
   PublicKeyToken=null' or one of its dependencies.
   The system cannot find the file specified.

I have a feeling that if I embed and install the assembly with my application, I would have problems with different versions of Windows and between 32-bit and 64-bit.

How do I solve this missing assembly deployment issue?


Edit: This seems to be a VS2010 issue for any target framework except 4.0. Does anyone have a fix for this?

Answer

Markus picture Markus · Jun 20, 2014

NetFwTypeLib object doesn't reside in hnetcfg.dll library on Windows 7 Ultimate. Rather, it resides in FirewallAPI.dll at %system32%\FirewallAPI.dll (eg c:\windows\system32\FirewallAPI.dll).

using NetFwTypeLib; // Add reference %SystemRoot%\System32\FirewallAPI.dll