none
How to set INetFwRule2.RemotePorts to IPHTTPS value RRS feed

  • Question

  • Hi all. I'm triying to build backup/restore functionality of Windows Firewall policy but facing with strange issue. When I trying to set INetFwRule2.RemotePorts to "IPHTTPS" value (RemotePorts is string in the NetFwTypeLib wrapper) I'm getting "The data is invalid" ComException. The protocol is set to 6 (TCP) as documentation required and all other values are correct as they were extracted from working firewall rule just before. To make sure that all NetFwRule2.RemotePorts dependencies keeped I'm setting it after all another properties of INetFwRule2 was set. I'm not sure about creation the instance of INetFwRule2 interface as it created using "HNetCfg.FwRule" progid. But I could'n find special progid for INetFwRule2 and "HNetCfg.FwRule2" does not exist. Also when I'm using INetFwRule instead of INetFwRule2 it gives me the same error.

    To reproduce this error just use the simple static method below:

    public static void CreateTestRule()
    {
        INetFwPolicy2 policy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2", false));
        INetFwRule2 rule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
        rule.Name = "Test IPHTTPS";
        rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
        rule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
        rule.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
        rule.Enabled = true;
        rule.RemotePorts = "IPHTTPS";
        policy.Rules.Add(rule);
        Marshal.FinalReleaseComObject(policy);
    }
    


    Thanks in advance.

     

    Saturday, October 22, 2011 4:13 AM

Answers

All replies

  • Hi Shoar,

    Please modify the following code line.
    rule.RemotePorts = "IPHTTPS"; // change "IPHTTPS" to "443".
    443 is the port number for IPHTTPS.
    IPHTTPS is not a special keyword like 'RPC', 'Teredo', etc.
    You can visit http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml to get more details about port number.


    Sincerely,
    Robin
    If it solved your problem,Please click "Mark As Answer" on that post and "Mark as Helpful". Lucky everyday!
    • Marked as answer by Shoar Monday, October 24, 2011 3:31 PM
    Monday, October 24, 2011 4:03 AM
  • Thanks for your reply. Yes, this will work but it is strange that if I set "IPHTTPS" as remote port of firewall rule manually, then RemotePorts property of INetFwRule will have "ivnalid" 'IPHTTPS' value.

    string remotePortsValue=rule.RemotePorts;//remotePortsValue will be 'IPHTTPS'

    rule.RemotePorts=remotePortsValue;//Error :(

    Monday, October 24, 2011 3:31 PM