我编写了一个Windows服务,它使用防火墙COM库Interop.NetFwTypeLib
来管理TCP传输的规则。两台机器上部署不报的问题,但是我最近在另一台计算机上安装并收到异常:无法使用STAThread属性强制类型为'System .__ ComObject'的COM对象
Unable to cast COM object of type 'System.__ComObject' to interface type 'NetFwTypeLib.INetFwRule3'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B21563FF-D696-4222-AB46-4E89B73AB34A}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))
阅读本帖后:
- Exception from HRESULT: 0x80004002 (E_NOINTERFACE)
- Why cannot I cast my COM object to the interface it implements in C#
- InvalidCastException , Exception from HRESULT: 0x80004002 (E_NOINTERFACE)
我设置了STAThreadAttribute
这个代码来测试,如果这个我解决问题的Main方法,但没有任何解决办法:
class Program
{
[STAThread]
static void Main(string[] args)
{
try{
var type = Type.GetTypeFromProgID("HNetCfg.FWRule");
var instance = (INetFwRule3) Activator.CreateInstance(type);
Console.WriteLine("OK");
}
catch (Exception exception){
Console.WriteLine(exception);
}
}
}
我很惊讶,我去运行此脚本来找到注册表中的CLSID,并没有返回
**OS**
Windows Server 2012 R2 Standard
**FirewallAPI.dll file on Windows/system32**
File version: 6.3.9600.17415
Product version: 6.3.9600.17415
Size: 736 kb
Date modified: 4/28/2015 8:51 PM
信息从计算机到哪里:两台电脑,在那里工作和不工作:
reg query HKCR\CLSID | find /i "{B21563FF-D696-4222-AB46-4E89B73AB34A}"
这些是从计算机到哪里了服务的工作信息上的任何结果服务失败:
**OS**
Windows Server 2011 Service Pack 1
**FirewallAPI.dll file on Windows/system32**
File version: 6.1.7600.16385
Product version: 6.3.7600.16385
Size: 730 kb
Date modified: 7/13/2009 8:51 PM
问题:
- 它可以是FirewallAPI.dll的版本是什么原因导致这个问题有什么区别?
- 如果是这样就足以更新DLL,虽然它似乎有点危险的注册表上可能的不一致?
这是一个简单的解释,在Windows 8和Server 2012中添加了INetFwRule3接口。没有真正的想法是什么“Windows Server 2011”可以,但它肯定是1。您将需要限制自己INetFwRule2或减。 Fwiw,HKCR \ CLSID键是类GUID的家,而不是接口GUID。 –
我会尝试你的建议,你知道INetFwRule2接口的操作系统的限制吗? – Joseph
只要RTFM,看看接口的MSDN文章的底部。 –