'Deploying NetFwTypeLib to manage the Windows Firewall
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?
Solution 1:[1]
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
Solution 2:[2]
What a weird error! The best i can think of is don't rely on the System32
version of the DLL
, copy it into your folder and call it from there. From my knowledge, i don't think the DLL should conflict with the different bit computers, but if they do then just obtain a different DLL from a 32 bit computer and have separate downloads for x64
and x86
. Good luck!
EDIT: Also, i have had some trouble with programming in 3.5 or lower in VS2010
. Try to get a version of visual c# express 2008 and try with that (usually fixes a lot of errors with downgrading .net
versions)
Solution 3:[3]
I had an issue with this same dll when working in Visual Studio 2012.
For me the fix was to manually move the interop.NetFwTypeLib.dll
into the directory I was working from. This seemed to fix the issue for me. Hope it helps
Solution 4:[4]
I just ran into a similar issue. In this case I was moving code from one program into my company's common web utilities.
According to the Microsoft documentation: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff737187(v=vs.120)
The interfaces NetFwTypeLib uses are also defined in Namespace: Microsoft.TeamFoundation.Common Assembly: Microsoft.TeamFoundation.Common (in Microsoft.TeamFoundation.Common.dll)
Then you just need the following functions:
INetFwMgr iNetMgr = null;
try{
Type tNetMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr")
iNetMgr = (INetFwMgr)Activator.CreateInstance(tNetMgr);
}
Now it appears to be assembly portable without needing to do any copying of the dll. Although I haven't yet tested it yet the compiler didn't make any noise about it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Markus |
Solution 2 | JMax |
Solution 3 | wjhguitarman |
Solution 4 | mWellington |