部署 NetFwTypeLib 来管理 Windows 防火墙
我的 Windows 服务需要在 Windows 防火墙中创建/删除某些规则。为此,我通过 COM 与
中的 NetFwTypeLib
进行交互。它在我的 64 位 Windows 7 计算机上运行得很好,但在另一台 64 位 Windows 7 计算机上测试会引发以下错误:
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.
我有一种感觉,如果我使用我的应用程序嵌入并安装程序集,我在使用不同版本的 Windows 以及 32 位和 64 位之间时会遇到问题。
如何解决缺少程序集部署问题?
编辑:这似乎是VS2010 问题 适用于除 4.0 之外的任何目标框架。有人能解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NetFwTypeLib
对象不驻留在 Windows 7 Ultimate 上的hnetcfg.dll
中。相反,它驻留在%system32%\FirewallAPI.dll
的FirewallAPI.dll
中(例如c:\windows\system32\FirewallAPI.dll
) 。NetFwTypeLib
object doesn't reside inhnetcfg.dll
on Windows 7 Ultimate. Rather, it resides inFirewallAPI.dll
at%system32%\FirewallAPI.dll
(egc:\windows\system32\FirewallAPI.dll
).多么奇怪的错误啊!我能想到的最好的办法是不要依赖
System32
版本的DLL
,将其复制到您的文件夹中并从那里调用它。据我所知,我认为 DLL 不应该与不同位的计算机发生冲突,但如果它们发生冲突,那么只需从 32 位计算机获取不同的 DLL,并分别下载x64
和x86
。祝你好运!编辑:此外,我在 VS2010 中的 3.5 或更低版本中编程时遇到了一些问题。尝试获取 Visual C# Express 2008 版本并尝试使用它(通常通过降级
.net
版本来修复很多错误)What a weird error! The best i can think of is don't rely on the
System32
version of theDLL
, 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 forx64
andx86
. 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)我在 Visual Studio 2012 中工作时遇到了同一个 dll 的问题。
对我来说,修复方法是手动将
interop.NetFwTypeLib.dll
移动到我正在工作的目录中。这似乎解决了我的问题。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.我刚刚遇到了类似的问题。在本例中,我将代码从一个程序移至我公司的通用 Web 实用程序中。
根据微软文档: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff737187(v=vs.120)
接口
NetFwTypeLib
use 也在命名空间中定义:Assembly:
那么你只需要以下功能:
现在它看起来是程序集可移植的,不需要对 dll 进行任何复制,虽然我还没有测试过,但编译器没有使任何有关它的噪音。
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://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff737187(v=vs.120)
The interfaces
NetFwTypeLib
uses are also defined in Namespace:Assembly:
Then you just need the following functions:
Now it appears to be assembly portable without needing to do any copying of the dll, although I haven't tested it yet, the compiler didn't make any noise about it.