Search code examples
c#comwindows-firewall-api

Unable to cast COM object of type 'System.__ComObject' with STAThread attribute


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?

Solution

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