SOL9 2.0 Class: WindowsFirewall

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * WindowsFirewall.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2008/08/05

#pragma once

#include <sol\Object.h>

#include <netfw.h>

namespace SOL {

/**
 * WindowsFirewall setting class
 */

class WindowsFirewall: public Object {

private:
    INetFwMgr* fwMgr;

    INetFwProfile* fwProfile;
    INetFwPolicy* fwPolicy;

public:
    /**
     * Constructor                        
     */

    WindowsFirewall()
     :fwProfile(NULL),
     fwMgr(NULL),
     fwPolicy(NULL)
    {
                
        HRESULT hr = CoInitializeEx(0,COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

        if (hr != RPC_E_CHANGED_MODE && FAILED(hr)){
            printf("WindowsFirewall#WindowsFirewall,1,CoInitializeEx failed: 0x%08lx\n", hr);
            throw hr;
        }

        try {
            // Firewall settings manager.
            if (FAILED(hr = CoCreateInstance(__uuidof(NetFwMgr),
                    NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwMgr),
                    (void**)&fwMgr))) {
                printf("WindowsFirewall#WindowsFirewall,2,CoCreateInstance failed: 0x%08lx\n", hr);
                throw hr;
            }

            if (FAILED(hr =fwMgr->get_LocalPolicy(&fwPolicy))) {
                printf("WindowsFirewall#WindowsFirewall,3,get_LocalPolicy failed: 0x%08lx\n", hr);
                throw hr;
            }
            if (FAILED(hr = fwPolicy->get_CurrentProfile(&fwProfile))) {
                printf("WindowsFirewall#WindowsFirewall,4,get_CurrentProfile failed: 0x%08lx\n", hr);

                throw hr;
            }
        } catch (HRESULT hr) {
            printf("WindowsFirewall#WindowsFirewall,5,Caught Exception 0x%08lx\n", hr);
            clear();

            throw hr;
        }
    }    
  

private:
    void clear() {
        if (fwProfile) {
            fwProfile->Release();
            fwProfile = NULL;
        }
        if (fwPolicy) {
            fwPolicy->Release();
            fwPolicy = NULL;
        }
        if (fwMgr) {
            fwMgr->Release();
            fwMgr = NULL;
        }

        CoUninitialize();
    }


public:
    /**
     * Destructor
     */
    ~WindowsFirewall() {
        clear();
    }


public:
    HRESULT isFirewallEnabled(BOOL& enabled)
    {
        HRESULT hr = S_OK;

        enabled = FALSE;

        VARIANT_BOOL fwEnabled = VARIANT_FALSE;

        try {    
            if (FAILED (hr = fwProfile->get_FirewallEnabled(&fwEnabled))) {
                printf("WindowsFirewall#isFirewallEnabled,1,get_FirewallEnabled failed: 0x%08lx\n", hr);
                throw hr;
            }
            if (fwEnabled == VARIANT_TRUE){
                enabled = TRUE;    
            }
         } catch (HRESULT hr) {

        }
        return hr;
     }


public:
    HRESULT isExceptionNotAllowed(BOOL& notAllowed)
    {
        //printf("WindowsFirewall#isFirewallOn,1,Start");

        HRESULT hr = S_OK;

        VARIANT_BOOL fwNotAllowed = VARIANT_FALSE;

        notAllowed = FALSE;

        try {    
            if (FAILED (hr = fwProfile->get_ExceptionsNotAllowed(&fwNotAllowed))) {
                printf("WindowsFirewall#isExceptionNotAllowed,1,get_ExceptionsNotAllowed failed: 0x%08lx\n", hr);
                throw hr;
            }
            if (fwNotAllowed == VARIANT_TRUE){
                notAllowed = TRUE;
            }
        } catch (HRESULT hr) {

        } 
        return hr;
    }


public:
    HRESULT isPortEnabled(int port, bool tcp, BOOL& enabled)
    {
            enabled = FALSE;

        NET_FW_IP_PROTOCOL protocol = NET_FW_IP_PROTOCOL_UDP;
        if(tcp) {
            protocol = NET_FW_IP_PROTOCOL_TCP;
        }

        INetFwOpenPorts* fwOpenPorts = NULL;
        INetFwOpenPort* fwOpenPort = NULL;
        HRESULT hr = S_OK;
        
        try {
            if (FAILED(hr= fwProfile->get_GloballyOpenPorts(&fwOpenPorts))) {
                printf("WindowsFirewall#isPortEnabled,1,Failed in get_GloballyOpenPorts\n");
                throw hr;

            }        
            long count = 0;
            if (FAILED(hr = fwOpenPorts->get_Count(&count))) {
                printf("WindowsFirewall#isPortEnabled,2,Failed in get_Count\n");
                throw hr;
            }
            printf("Count=%d\n", count);
            //if (count>0) {
            if (FAILED(hr = fwOpenPorts->Item(port, protocol, &fwOpenPort))) {
                printf("WindowsFirewall#isPortEnabled,3,Failed in Item\n");
                throw hr;
            }

            VARIANT_BOOL fwEnabled = VARIANT_FALSE;  
            if (FAILED(hr = fwOpenPort->get_Enabled(&fwEnabled))) {
                printf("WindowsFirewall#isPortEnabled,4,get_Enabled\n");
                throw hr;
            }
            if (fwEnabled == VARIANT_TRUE) {
                enabled = TRUE;
            }
            //}
        } catch (HRESULT hr) {
            ;//
        }
        
        if (fwOpenPort) 
            fwOpenPort->Release();
            
        if (fwOpenPorts)
            fwOpenPorts->Release();
    
        return hr;
    }

public:

    HRESULT setPortEnabled(int port, bool tcp, bool enable)
    {
        BOOL rc = FALSE;

        HRESULT hr = S_OK;


        NET_FW_IP_PROTOCOL protocol = NET_FW_IP_PROTOCOL_UDP;
        if(tcp) {
            protocol = NET_FW_IP_PROTOCOL_TCP;
        }

        INetFwOpenPort* fwOpenPort = NULL;
        INetFwOpenPorts* fwOpenPorts = NULL;

        try {
            if (FAILED(hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts))) {
                printf("WindowsFirewall#setPortEnabled,1,Failed in get_GloballyOpenPorts\n");

                throw hr;
            }
            long count = 0;
            if (FAILED(hr = fwOpenPorts->get_Count(&count))) {
                printf("WindowsFirewall#setPortEnabled,2,Failed in get_Count\n");
                throw hr;
            }
            printf("Count=%d\n", count);

            if (FAILED(hr = fwOpenPorts->Item(port, protocol, &fwOpenPort))) {
                printf("WindowsFirewall#setPortEnabled,3,Failed in Item\n");
                throw hr;
            }

            VARIANT_BOOL fwEnabled = VARIANT_FALSE;
            if (enable) {
                fwEnabled = VARIANT_TRUE;
            }
            if (FAILED (hr = fwOpenPort->put_Enabled(fwEnabled))) {
                printf("WindowsFirewall#sePortEnabled,4,put_Enabled\n");

                throw hr;
                }

        } catch (HRESULT hr) {
            ;    
        }
    
        if (fwOpenPort)
            fwOpenPort->Release();

        if (fwOpenPorts) 
            fwOpenPorts->Release();

        return hr;
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.