none
INetFwPolicy2 interface causing error in the application on .net3.5 RRS feed

  • Question

  • Hi,

    I want to manage windows firewall using windows API on dotnet 3.5.

    This is what I did.

    Type netFwPolicy2Type = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");

    var manage =  (INetFwPolicy2)Activator.CreateInstance(netFwPolicy2Type);

    My application actually build succeeded in visual studio 2017. but when I run it, it gives me this error

    The program '[3240] firewallmanage.exe' has exited with code -532459699 (0xe0434f4d).

    And when I compile in .net 4.5, it works perfectly.  Please tell me what went wrong.

    Thank you.



    Friday, March 23, 2018 3:17 PM

Answers

  • The test I did (incomplete, but it returns the same results as in C++...) =>

    Guid CLSID_NetFwPolicy2 = new Guid("E2B3C97F-6AE1-41AC-817A-F6F92166D7DD");
    Type NetFwPolicy2Type = Type.GetTypeFromCLSID(CLSID_NetFwPolicy2, true);
    object NetFwPolicy2 = Activator.CreateInstance(NetFwPolicy2Type);
    INetFwPolicy2 pNetFwPolicy2 = (INetFwPolicy2)NetFwPolicy2;
    
    int CurrentProfilesBitMask = 0;
    bool bActualFirewallEnabled = false;
    HRESULT hr = pNetFwPolicy2.get_CurrentProfileTypes(out CurrentProfilesBitMask);
    
    ProfileMapElement[] ProfileMap = new ProfileMapElement[3];
    ProfileMap[0].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_DOMAIN;
    ProfileMap[0].sName = "Domain";
    ProfileMap[1].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_PRIVATE;
    ProfileMap[1].sName = "Private";
    ProfileMap[2].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_PUBLIC;
    ProfileMap[2].sName = "Public";
    
    for (int i = 0; i < 3; i++)
    {
        if ((Convert.ToInt32(CurrentProfilesBitMask) & (int)ProfileMap[i].Id) > 0)
        //if (CurrentProfilesBitMask & ProfileMap[i].Id)
        {
            hr = pNetFwPolicy2.get_FirewallEnabled(ProfileMap[i].Id, out bActualFirewallEnabled);
            if (hr == HRESULT.S_OK)
            {
                
            }
        }
    }
    
    INetFwRules pFwRules;
    hr = pNetFwPolicy2.get_Rules(out pFwRules);
    int nRuleCount = 0;
    hr = pFwRules.get_Count(out nRuleCount);
    
    IntPtr pEnumerator;
    hr = pFwRules.get__NewEnum(out pEnumerator);
    IEnumerator pEnum = Marshal.GetObjectForIUnknown(pEnumerator) as IEnumerator;
    int nIndex = 1;
    while (pEnum.MoveNext())
    {             
        nIndex++;               
    }

    Declarations :

    struct ProfileMapElement
    {
        public NET_FW_PROFILE_TYPE2 Id;
        public string sName;
    }   
    
    public enum HRESULT : int
    {
        S_OK = 0,
        S_FALSE = 1,
        E_NOINTERFACE = unchecked((int)0x80004002),
        E_NOTIMPL = unchecked((int)0x80004001),
        E_FAIL = unchecked((int)0x80004005)
    }
    
    public enum NET_FW_PROFILE_TYPE2 : int
    {
        NET_FW_PROFILE2_DOMAIN = 0x1,
        NET_FW_PROFILE2_PRIVATE = 0x2,
        NET_FW_PROFILE2_PUBLIC = 0x4,
        NET_FW_PROFILE2_ALL = 0x7fffffff
    }
    
    [ComImport]
    [Guid("9C4C6277-5027-441E-AFAE-CA1F542DA009")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwRules
    {
        #region <IDispatch>
        int GetTypeInfoCount();
        [return: MarshalAs(UnmanagedType.Interface)]
        IntPtr GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
        [PreserveSig]
        HRESULT GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames,
            [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
        [PreserveSig]
        HRESULT Invoke(int dispIdMember, [In] ref Guid riid, [In, MarshalAs(UnmanagedType.U4)] int lcid, [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
            [Out, In] DISPPARAMS pDispParams, [Out] out object pVarResult, [Out, In] EXCEPINFO pExcepInfo, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
        #endregion
    
        HRESULT get_Count(out int count);
        HRESULT Add(IntPtr rule);
        // INetFwRule : to be added instead of IntPtr
        //HRESULT Add(INetFwRule rule);
        HRESULT Remove(StringBuilder name);
        HRESULT Item(StringBuilder name, out IntPtr rule);
        // INetFwRule : to be added instead of IntPtr
        //HRESULT Item(StringBuilder name, out INetFwRule rule);
        //HRESULT get__NewEnum([MarshalAs(UnmanagedType.IUnknown)] out object newEnum);
        HRESULT get__NewEnum(out IntPtr newEnum);
    }
    
    [ComImport]
    [Guid("98325047-C671-4174-8D81-DEFCD3F03186")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwPolicy2
    {
        #region <IDispatch>
        int GetTypeInfoCount();
        [return: MarshalAs(UnmanagedType.Interface)]
        IntPtr GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
        [PreserveSig]
        HRESULT GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames,
            [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
        [PreserveSig]
        HRESULT Invoke(int dispIdMember, [In] ref Guid riid, [In, MarshalAs(UnmanagedType.U4)] int lcid, [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
            [Out, In] DISPPARAMS pDispParams, [Out] out object pVarResult, [Out, In] EXCEPINFO pExcepInfo, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
        #endregion
    
        HRESULT get_CurrentProfileTypes(out int profileTypesBitmask);
        HRESULT get_FirewallEnabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool enabled);   
        HRESULT put_FirewallEnabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool enabled);
        HRESULT get_ExcludedInterfaces(NET_FW_PROFILE_TYPE2 profileType, out IntPtr interfaces);
        HRESULT put_ExcludedInterfaces(NET_FW_PROFILE_TYPE2 profileType, IntPtr interfaces);
        HRESULT get_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool Block);
        HRESULT put_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool Block);
        HRESULT get_NotificationsDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool disabled);
        HRESULT put_NotificationsDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool disabled);
        HRESULT get_UnicastResponsesToMulticastBroadcastDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool disabled);
        HRESULT put_UnicastResponsesToMulticastBroadcastDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool disabled);
        HRESULT get_Rules(out INetFwRules rules);
    
        // ... to be completed with other methods from .h
    }  

    Monday, March 26, 2018 4:34 PM

All replies

  • Hi Rajat Kinkhabwala,

    Thank you for posting here.

    For your question, how do you add the reference? What is your package? Do you add FirewallManager from NuGet? If yes, I reproduce the scenario.

    It works well in .net framework 4.5. When I run it in .net framework 3.5, in the build message, it shows why. "FirewallManager, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL" could not be resolved because it was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v3.5".

    Hence it needs .net framework 4.5.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 8:33 AM
    Moderator
  • You can use directly the COM interface

    [ComImport] [Guid("98325047-C671-4174-8D81-DEFCD3F03186")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface INetFwPolicy2

    + methods declarations

    I did a quick test with .NET 3.5  on Windows 10 with the 2 first methods get_CurrentProfileTypes and get_FirewallEnabled and it seems to work...

    Monday, March 26, 2018 11:14 AM
  • Hi Castorix,

    I tried the solution you asked me to do but I still not able to achieve the output. If you could share demo program you written it would be very helpful. Could you share it ? 

    Thanks,
    Rajat Kinkhabwala 

    Monday, March 26, 2018 3:13 PM
  • The test I did (incomplete, but it returns the same results as in C++...) =>

    Guid CLSID_NetFwPolicy2 = new Guid("E2B3C97F-6AE1-41AC-817A-F6F92166D7DD");
    Type NetFwPolicy2Type = Type.GetTypeFromCLSID(CLSID_NetFwPolicy2, true);
    object NetFwPolicy2 = Activator.CreateInstance(NetFwPolicy2Type);
    INetFwPolicy2 pNetFwPolicy2 = (INetFwPolicy2)NetFwPolicy2;
    
    int CurrentProfilesBitMask = 0;
    bool bActualFirewallEnabled = false;
    HRESULT hr = pNetFwPolicy2.get_CurrentProfileTypes(out CurrentProfilesBitMask);
    
    ProfileMapElement[] ProfileMap = new ProfileMapElement[3];
    ProfileMap[0].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_DOMAIN;
    ProfileMap[0].sName = "Domain";
    ProfileMap[1].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_PRIVATE;
    ProfileMap[1].sName = "Private";
    ProfileMap[2].Id = NET_FW_PROFILE_TYPE2.NET_FW_PROFILE2_PUBLIC;
    ProfileMap[2].sName = "Public";
    
    for (int i = 0; i < 3; i++)
    {
        if ((Convert.ToInt32(CurrentProfilesBitMask) & (int)ProfileMap[i].Id) > 0)
        //if (CurrentProfilesBitMask & ProfileMap[i].Id)
        {
            hr = pNetFwPolicy2.get_FirewallEnabled(ProfileMap[i].Id, out bActualFirewallEnabled);
            if (hr == HRESULT.S_OK)
            {
                
            }
        }
    }
    
    INetFwRules pFwRules;
    hr = pNetFwPolicy2.get_Rules(out pFwRules);
    int nRuleCount = 0;
    hr = pFwRules.get_Count(out nRuleCount);
    
    IntPtr pEnumerator;
    hr = pFwRules.get__NewEnum(out pEnumerator);
    IEnumerator pEnum = Marshal.GetObjectForIUnknown(pEnumerator) as IEnumerator;
    int nIndex = 1;
    while (pEnum.MoveNext())
    {             
        nIndex++;               
    }

    Declarations :

    struct ProfileMapElement
    {
        public NET_FW_PROFILE_TYPE2 Id;
        public string sName;
    }   
    
    public enum HRESULT : int
    {
        S_OK = 0,
        S_FALSE = 1,
        E_NOINTERFACE = unchecked((int)0x80004002),
        E_NOTIMPL = unchecked((int)0x80004001),
        E_FAIL = unchecked((int)0x80004005)
    }
    
    public enum NET_FW_PROFILE_TYPE2 : int
    {
        NET_FW_PROFILE2_DOMAIN = 0x1,
        NET_FW_PROFILE2_PRIVATE = 0x2,
        NET_FW_PROFILE2_PUBLIC = 0x4,
        NET_FW_PROFILE2_ALL = 0x7fffffff
    }
    
    [ComImport]
    [Guid("9C4C6277-5027-441E-AFAE-CA1F542DA009")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwRules
    {
        #region <IDispatch>
        int GetTypeInfoCount();
        [return: MarshalAs(UnmanagedType.Interface)]
        IntPtr GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
        [PreserveSig]
        HRESULT GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames,
            [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
        [PreserveSig]
        HRESULT Invoke(int dispIdMember, [In] ref Guid riid, [In, MarshalAs(UnmanagedType.U4)] int lcid, [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
            [Out, In] DISPPARAMS pDispParams, [Out] out object pVarResult, [Out, In] EXCEPINFO pExcepInfo, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
        #endregion
    
        HRESULT get_Count(out int count);
        HRESULT Add(IntPtr rule);
        // INetFwRule : to be added instead of IntPtr
        //HRESULT Add(INetFwRule rule);
        HRESULT Remove(StringBuilder name);
        HRESULT Item(StringBuilder name, out IntPtr rule);
        // INetFwRule : to be added instead of IntPtr
        //HRESULT Item(StringBuilder name, out INetFwRule rule);
        //HRESULT get__NewEnum([MarshalAs(UnmanagedType.IUnknown)] out object newEnum);
        HRESULT get__NewEnum(out IntPtr newEnum);
    }
    
    [ComImport]
    [Guid("98325047-C671-4174-8D81-DEFCD3F03186")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwPolicy2
    {
        #region <IDispatch>
        int GetTypeInfoCount();
        [return: MarshalAs(UnmanagedType.Interface)]
        IntPtr GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
        [PreserveSig]
        HRESULT GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames,
            [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
        [PreserveSig]
        HRESULT Invoke(int dispIdMember, [In] ref Guid riid, [In, MarshalAs(UnmanagedType.U4)] int lcid, [In, MarshalAs(UnmanagedType.U4)] int dwFlags,
            [Out, In] DISPPARAMS pDispParams, [Out] out object pVarResult, [Out, In] EXCEPINFO pExcepInfo, [Out, MarshalAs(UnmanagedType.LPArray)] IntPtr[] pArgErr);
        #endregion
    
        HRESULT get_CurrentProfileTypes(out int profileTypesBitmask);
        HRESULT get_FirewallEnabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool enabled);   
        HRESULT put_FirewallEnabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool enabled);
        HRESULT get_ExcludedInterfaces(NET_FW_PROFILE_TYPE2 profileType, out IntPtr interfaces);
        HRESULT put_ExcludedInterfaces(NET_FW_PROFILE_TYPE2 profileType, IntPtr interfaces);
        HRESULT get_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool Block);
        HRESULT put_BlockAllInboundTraffic(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool Block);
        HRESULT get_NotificationsDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool disabled);
        HRESULT put_NotificationsDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool disabled);
        HRESULT get_UnicastResponsesToMulticastBroadcastDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] out bool disabled);
        HRESULT put_UnicastResponsesToMulticastBroadcastDisabled(NET_FW_PROFILE_TYPE2 profileType, [MarshalAs(UnmanagedType.VariantBool)] bool disabled);
        HRESULT get_Rules(out INetFwRules rules);
    
        // ... to be completed with other methods from .h
    }  

    Monday, March 26, 2018 4:34 PM