SOL9 Sample: NetFwRulesAddRule

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * NetFwRulesAddRule.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2012/09/23

// Note: This program must be run as System Administrator.

#include <sol\Locale.h>
#include <sol\com\ApartmentThreadedModel.h>
#include <sol\netfw\NetFwPolicy2.h>
#include <sol\netfw\NetFwRules.h>
#include <sol\netfw\NetFwRule.h>


void _tmain(int argc, const TCHAR* argv[])
{
  Locale locale;


  try {
    ApartmentThreadedModel model;

    //Create a policy2 object of NetFwPolicy2
    SOL::NetFwPolicy2 policy2;

    //Get a rules object from policy2
    SOL::NetFwRules rules = policy2.getRules();
    
    //Create a rule object of NetFwRule
    SOL::NetFwRule rule;

  
    _bstr_t name = "SolService";
    _bstr_t description = "SolService rule for incoming network traffic";
    _bstr_t group = "@SolService,-100";
    _bstr_t application = "C:\\sample\\SolService.exe";
    _bstr_t service = "SolService";
    _bstr_t ports = "6400";

    //Put properties to the rule.
    rule.putName(name);
    rule.putDescription(description);
    rule.putApplicationName(application);
    rule.putServiceName(service);
    rule.putProtocol(NET_FW_IP_PROTOCOL_TCP);
    rule.putLocalPorts(ports);
    rule.putGrouping(group);
    rule.putProfiles(NET_FW_PROFILE2_PRIVATE);
    rule.putAction(NET_FW_ACTION_ALLOW);
    rule.putEnabled(VARIANT_TRUE);

    //Add the rule to the rules
    printf("Add the rule to the rules\n");
    rules.add(rule);

    printf("O<. Added\n");
    //rules.display();
    printf("Hit any key"); getchar();

    printf("Find a rule from the rules by name\n");

    SOL::NetFwRule solServiceRule = rules.item(name);

    printf("Display the solServiceRule\n");
    
    solServiceRule.display();

    printf("Hit any key"); getchar();

    printf("Remove a rule from the rules by name\n");

    rules.remove(name);
    printf("OK. Removed\n");

    printf("Hit any key"); getchar();

  } catch (Exception& ex) {
    ex.printf();  
  } catch (HRESULT hr) {
    _tprintf(_T("Exception: HRESULT=%x\n"), hr);
    _tprintf(_T("This program must be run as System Administrator\n"));
  }
}



Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.