none
Firewall Management with C# application RRS feed

  • Question

  • Hello ,

    I want to make an application in .net C# that should be capable of doing firewall operations like (current firewall status,firewall turn on/off,adding new rules, deleting existing rules, modifying existing rule). Does .NET has any API that I can use instead of using "netsh advfirewall" command.

    Note: It should be capable of running on both 3.5 and 4.5 framework

    Thanks,

    Rajat Kinkhabwala

    Monday, April 2, 2018 3:35 PM

Answers

  • Ah yes, I had changed all StringBuilder into string and I added INetFwRule interface but I did not update the code in the thread...

    This works fine to get the description =>

    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;
    
    INetFwRules pFwRules;
    HRESULT hr = pNetFwPolicy2.get_Rules(out pFwRules);
    
    INetFwRule pNetFwRule;
    hr = pFwRules.Item("Test", out pNetFwRule);
    string sRuleDesc;
    hr = pNetFwRule.get_Description(out sRuleDesc);

    INetFwRule (I did not test the Variant parameters) =>

    [ComImport]
    [Guid("AF230D27-BABA-4E42-ACED-F524F22CFCE2")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwRule
    {
        #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_Name(StringBuilder name);
        HRESULT get_Name(out string name);
        HRESULT put_Name(string name);
        HRESULT get_Description(out string desc);
        HRESULT put_Description(string desc);
        HRESULT get_ApplicationName(out string imageFileName);
        HRESULT put_ApplicationName(string imageFileName);
        HRESULT get_ServiceName(out string serviceName);
        HRESULT put_ServiceName(string serviceName);
        HRESULT get_Protocol(out NET_FW_IP_PROTOCOL protocol);
        HRESULT put_Protocol(NET_FW_IP_PROTOCOL protocol);
        HRESULT get_LocalPorts(out string portNumbers);
        HRESULT put_LocalPorts(string portNumbers);
        HRESULT get_RemotePorts(out string portNumbers);
        HRESULT put_RemotePorts(string portNumbers);
        HRESULT get_LocalAddresses(out string localAddrs);
        HRESULT put_LocalAddresses(string localAddrs);
        HRESULT get_RemoteAddresses(out string remoteAddrs);
        HRESULT put_RemoteAddresses(string remoteAddrs);
        HRESULT get_IcmpTypesAndCodes(out string icmpTypesAndCodes);
        HRESULT put_IcmpTypesAndCodes(string icmpTypesAndCodes);
        HRESULT get_Direction(out NET_FW_RULE_DIRECTION remoteAddrs);
        HRESULT put_Direction(NET_FW_RULE_DIRECTION remoteAddrs);
    
        // VARIANT
        HRESULT get_Interfaces(out IntPtr interfaces);
        HRESULT put_Interfaces(IntPtr interfaces);
    
        HRESULT get_InterfaceTypes(out string interfaceTypes);
        HRESULT put_InterfaceTypes(string interfaceTypes);
    
        // VARIANT_BOOL
        HRESULT get_Enabled(out bool enabled);
        HRESULT put_Enabled(bool enabled);
    
        HRESULT get_Grouping(out string context);
        HRESULT put_Grouping(string context);
    
        //HRESULT get_Grouping(StringBuilder context);
        //HRESULT put_Grouping(StringBuilder context);
        HRESULT get_Profiles(out NET_FW_PROFILE_TYPE2 profileTypesBitmask);
        HRESULT put_Profiles(NET_FW_PROFILE_TYPE2 profileTypesBitmask);
    
        // VARIANT_BOOL
        HRESULT get_EdgeTraversal(out bool enabled);
        HRESULT put_EdgeTraversal(bool enabled);
    
        HRESULT get_Action(out NET_FW_ACTION action);
        HRESULT put_Action(NET_FW_ACTION action);
    }


    Tuesday, April 3, 2018 7:30 PM

All replies

  • With INetFwRule

    • Edited by Castorix31 Monday, April 2, 2018 4:28 PM
    Monday, April 2, 2018 4:27 PM
  • Did you test INetFwRules from your other thread ?


    Monday, April 2, 2018 4:33 PM
  • Yes ,

    I did test INetFwRules but it didn't work. The code provided was in C++ but I'm building application in C# when I compile my application in 3.5 it gives me error application in break mode

    Is there any other api I can use ?

    Thanks,

    Rajat

    Monday, April 2, 2018 4:37 PM
  • I did test INetFwRules but it didn't work. The code provided was in C++ but I'm building application in C# when I compile my application in 3.5 it gives me error application in break mode

    The sample code I had posted was C#, not C++

    (and it can be compiled in any version of .NET as it is P/Invoke.)

    I tested the enumeration of Rules with VS 2015, .NET 3.5 on Windows 10 and it works.
    • Edited by Castorix31 Monday, April 2, 2018 4:57 PM
    Monday, April 2, 2018 4:56 PM
  • Hey Castorix,

    Actually my goal is to run Dll of my application in java. I'm also able to make it run through standalone .Exe but When I'm calling the method from Java through JNA. It's giving me error. UnsatisfiedLinkError (Error looking up method name x). I'm able to load dll successfully to Java But not able to get desired output. Can you tell me what could be the cause of this issue ?

    Thanks,

    Rajat Kinkhabwala

    Monday, April 2, 2018 5:46 PM
  • Hi Rajat Kinkhabwala,

    Thank you for posting here.

    According to your description and feedback, you want to call this C++ Firewall DLL from #. And you want to call this C# application in java via jna?

    We are not experts of jave, If you want to know more about jave, you could post a new thread in StackOverFlow.

    https://stackoverflow.com/questions/tagged/java

    Have you try the code provided by Castorix31?

    >>Actually my goal is to run Dll of my application in java.

    What is the DLL? The C# application or the C++ dll API?

    If you want to call C++ DLL using java via jna, you could try to refer to the link below.

    https://shansvex.wordpress.com/2013/05/23/using-a-dll-in-java-with-jna-java-native-access/

    If you want to call C# method in java, you could refer to the thread in StackOverFlow.

    https://stackoverflow.com/questions/18474398/how-to-use-c-sharp-function-in-java-using-jna-lib

    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.

    Tuesday, April 3, 2018 2:46 AM
    Moderator
  • Hey Wendy,

    Thanks for reply. Yes I'm working on that code. But I'm stuck in finding ComImport Guid for INetFwRules ? Where can I get the CLSID for INetFwRules?

    How can I create instance for INetFwRules?

    Thanks,

    Rajat kinkhabwala



    Tuesday, April 3, 2018 3:24 PM
  • Yes I'm working on that code. But I'm stuck in finding ComImport Guid for INetFwRules ? Where can I get the CLSID for INetFwRules?

    But it is in the code (?) :

    [ComImport]
    [Guid("9C4C6277-5027-441E-AFAE-CA1F542DA009")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwRules

    (I just copied it from Netfw.h)

    Tuesday, April 3, 2018 4:04 PM
  • Hey Wendy,

    This is the test I'm doing, Trying to get the description from my test rule.

    public void GetDescription()

            {

                INetFwRules FwRules;

                Guid CLSID_NetFwRule = new Guid("2C5BC43E-3369-4C33-AB0C-BE9469677AF4");

                FwPolicy2.get_Rules(out FwRules);

                Type INetFwRuleType = Type.GetTypeFromCLSID(CLSID_NetFwRule, true);

                INetFwRule FwRule = (INetFwRule)Activator.CreateInstance(INetFwRuleType);

                StringBuilder sb = new StringBuilder();

                sb.Append("test");

                FwRules.Item(sb,out FwRule); // get exception here 

                string Description = "";

                FwRule.get_Description(out Description);

                Console.WriteLine(Description);

            }

    It works for some of the time but sometime give me this exception.

    System.ArgumentException: 'Value does not fall within the expected range.'  

    This is the Interface

    HRESULT Item(StringBuilder name, out INetFwRule rule);


    My question is why it only works for some of the time and how can i fix it.

    Thank you very much.


    Rajat kinkhabwala

     




    Tuesday, April 3, 2018 5:20 PM
  • the issue was the string builder. So i changed 

    HRESULT Item(StringBuilder name, out INetFwRule rule);

    to 

    HRESULT Item(string name, out INetFwRule rule);

    it works every time.

    But i still don't what cause the problem.

    thanks.

    Tuesday, April 3, 2018 7:12 PM
  • Ah yes, I had changed all StringBuilder into string and I added INetFwRule interface but I did not update the code in the thread...

    This works fine to get the description =>

    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;
    
    INetFwRules pFwRules;
    HRESULT hr = pNetFwPolicy2.get_Rules(out pFwRules);
    
    INetFwRule pNetFwRule;
    hr = pFwRules.Item("Test", out pNetFwRule);
    string sRuleDesc;
    hr = pNetFwRule.get_Description(out sRuleDesc);

    INetFwRule (I did not test the Variant parameters) =>

    [ComImport]
    [Guid("AF230D27-BABA-4E42-ACED-F524F22CFCE2")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface INetFwRule
    {
        #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_Name(StringBuilder name);
        HRESULT get_Name(out string name);
        HRESULT put_Name(string name);
        HRESULT get_Description(out string desc);
        HRESULT put_Description(string desc);
        HRESULT get_ApplicationName(out string imageFileName);
        HRESULT put_ApplicationName(string imageFileName);
        HRESULT get_ServiceName(out string serviceName);
        HRESULT put_ServiceName(string serviceName);
        HRESULT get_Protocol(out NET_FW_IP_PROTOCOL protocol);
        HRESULT put_Protocol(NET_FW_IP_PROTOCOL protocol);
        HRESULT get_LocalPorts(out string portNumbers);
        HRESULT put_LocalPorts(string portNumbers);
        HRESULT get_RemotePorts(out string portNumbers);
        HRESULT put_RemotePorts(string portNumbers);
        HRESULT get_LocalAddresses(out string localAddrs);
        HRESULT put_LocalAddresses(string localAddrs);
        HRESULT get_RemoteAddresses(out string remoteAddrs);
        HRESULT put_RemoteAddresses(string remoteAddrs);
        HRESULT get_IcmpTypesAndCodes(out string icmpTypesAndCodes);
        HRESULT put_IcmpTypesAndCodes(string icmpTypesAndCodes);
        HRESULT get_Direction(out NET_FW_RULE_DIRECTION remoteAddrs);
        HRESULT put_Direction(NET_FW_RULE_DIRECTION remoteAddrs);
    
        // VARIANT
        HRESULT get_Interfaces(out IntPtr interfaces);
        HRESULT put_Interfaces(IntPtr interfaces);
    
        HRESULT get_InterfaceTypes(out string interfaceTypes);
        HRESULT put_InterfaceTypes(string interfaceTypes);
    
        // VARIANT_BOOL
        HRESULT get_Enabled(out bool enabled);
        HRESULT put_Enabled(bool enabled);
    
        HRESULT get_Grouping(out string context);
        HRESULT put_Grouping(string context);
    
        //HRESULT get_Grouping(StringBuilder context);
        //HRESULT put_Grouping(StringBuilder context);
        HRESULT get_Profiles(out NET_FW_PROFILE_TYPE2 profileTypesBitmask);
        HRESULT put_Profiles(NET_FW_PROFILE_TYPE2 profileTypesBitmask);
    
        // VARIANT_BOOL
        HRESULT get_EdgeTraversal(out bool enabled);
        HRESULT put_EdgeTraversal(bool enabled);
    
        HRESULT get_Action(out NET_FW_ACTION action);
        HRESULT put_Action(NET_FW_ACTION action);
    }


    Tuesday, April 3, 2018 7:30 PM