2
votes

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?
1
It is the simple explanation, the INetFwRule3 interface was added in Windows 8 and Server 2012. No real idea what "Windows Server 2011" could be but it is certainly off by 1. You'll need to limit yourself to INetFwRule2 or less. Fwiw, the HKCR\CLSID key is the home of class guids, not interface guids.Hans Passant
I will try with your recommendation, and do you know the limitation of OS for INetFwRule2 interface?Joseph
Just RTFM, look at the bottom of the MSDN article for the interface.Hans Passant
Your explanation work for me, grateful for your time and knowledge, please when you have a response time write it as answer.Joseph
You don't really need me to write it, just write it yourself and accept it as the answer.Hans Passant

1 Answers

2
votes

I can write this answer thanks to @Hans comments.

After read the documentation on MSDN of:

I found the minimum supported client and server for each interface.

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.
    }

You can downgrade to INetFwRule on your application if you don't need extra feature of INetFwRule2 or INetFwRule3.