2

I wrote a Windows Service that use Firewall COM library Interop.NetFwTypeLib to manage rules for TCP transmission. Deployments on two machine don't report problems but I install it recently on another computer and receive the exception:

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))

After read this posts:

I set STAThreadAttribute to the Main method of this code to test if with this I resolve the problem, but without any solution:

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);
        }
    }
}

I was surprised went I run this script to find the CLSID on registry, and don't return any result on both computers, where works and does not work.:

reg query HKCR\CLSID | find /i "{B21563FF-D696-4222-AB46-4E89B73AB34A}"

These are the information from the computer where the service works:

**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

Information from the computer where the service fails:

**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

QUESTIONS:

  • It can be the difference of version of FirewallAPI.dll what causes the problem?
  • If so would suffice to update dll, although it seems a bit dangerous by possible inconsistents on the registry?
4

1 回答 1

2

感谢@Hans 的评论,我可以写下这个答案。

阅读 MSDN 上的文档后:

我找到了每个接口支持的最低客户端和服务器。

var osVersion = Environment.OSVersion.Version;

if(osVersion.Major < 6)
    throw new Exception("INetFwRule is not available for current OS version. Minimun OS version required is Windows Vista or Windows Server 2008.");

if (osVersion.Major == 6)
{
    switch (osVersion.Minor)
    {
        case 0:
            //INetFwRule is available. Windows Server 2008 or Windows Vista
            break;
        case 1:
            //INetFwRule2 is available. Windows 7 or Windows Server 2008 R2 
            break;
        default:
            //INetFwRule3 is available. Windows 8.1, Windows Server 2012 R2, Windows 8 or Windows Server 2012.
            break;
        }
    }
    else
    {
        //INetFwRule3 is available. Windows Server 2016 Technical Preview or Windows 10.
    }

INetFwRule如果您不需要INetFwRule2or的额外功能,您可以降级到您的应用程序INetFwRule3

于 2016-06-29T17:53:38.347 回答