部署 NetFwTypeLib 来管理 Windows 防火墙

发布于 2024-10-05 08:01:49 字数 806 浏览 6 评论 0原文

我的 Windows 服务需要在 Windows 防火墙中创建/删除某些规则。为此,我通过 COM 与 \system32\hnetcfg.dll 中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光是把杀猪刀 2024-10-12 08:01:49

NetFwTypeLib 对象不驻留在 Windows 7 Ultimate 上的 hnetcfg.dll 中。相反,它驻留在 %system32%\FirewallAPI.dllFirewallAPI.dll 中(例如 c:\windows\system32\FirewallAPI.dll) 。

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

NetFwTypeLib object doesn't reside in hnetcfg.dll 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
人生戏 2024-10-12 08:01:49

多么奇怪的错误啊!我能想到的最好的办法是不要依赖 System32 版本的 DLL,将其复制到您的文件夹中并从那里调用它。据我所知,我认为 DLL 不应该与不同位的计算机发生冲突,但如果它们发生冲突,那么只需从 32 位计算机获取不同的 DLL,并分别下载 x64x86。祝你好运!

编辑:此外,我在 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 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)

对风讲故事 2024-10-12 08:01:49

我在 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.

相权↑美人 2024-10-12 08:01:49

我刚刚遇到了类似的问题。在本例中,我将代码从一个程序移至我公司的通用 Web 实用程序中。

根据微软文档: https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/ff737187(v=vs.120)

接口NetFwTypeLib use 也在命名空间中定义:

Microsoft.TeamFoundation.Common

Assembly:

Microsoft.TeamFoundation.Common (in Microsoft.TeamFoundation.Common.dll)

那么你只需要以下功能:

INetFwMgr iNetMgr = null;

try {
  Type tNetMgr = Type.GetTypeFromProgID("HNetCfg.FwMgr");
  iNetMgr = (INetFwMgr)Activator.CreateInstance(tNetMgr);
}

现在它看起来是程序集可移植的,不需要对 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:

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 tested it yet, the compiler didn't make any noise about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文