添加为防火墙例外

CString GetExeName(LPCTSTR lpExeFullName)
{
CString strName = lpExeFullName;
int nPos = strName.ReverseFind(_T('\\'));
if (nPos >= 0)
{
strName = strName.Right(strName.GetLength()-nPos-1);
}
return strName;
}


CString GetExeName()
{
const int iArrSize = MAX_PATH;
TCHAR szPath[iArrSize] = {0};
GetModuleFileName(AfxGetInstanceHandle(),szPath,iArrSize);
return GetExeName(szPath);
}



InitInstance()中增加

bool AddToWindowsFirewall()
{
TCHAR szFullPath[MAX_PATH] = {0};
GetModuleFileName(AfxGetInstanceHandle(), szFullPath, MAX_PATH);


CWindowsFireWall fireWall;
bool bAdd = fireWall.AddApp(szFullPath, GetExeName());


ATLTRACE2(_T("AddToWindowsFirewall bSucessful=%d\n"), bAdd);


return bAdd;

}




WindowsFireWall.h

#pragma once


#include <netfw.h>


class CWindowsFireWall
{
public:
CWindowsFireWall(void);
~CWindowsFireWall(void);


public:
bool Open(); //打开防火墙,成功则返回true
bool Close(); //关闭防火墙,成功则返回true
bool IsOpen(bool& pFwOn);


bool AddApp(LPCTSTR lpFwProcessImageFileName,LPCTSTR lpFwName);
bool AddPort(long lPortNumber,NET_FW_IP_PROTOCOL ipProtocol,LPCTSTR pName);


protected:
void Initialize();
void UnInitalize();

//Help functions
private:
INetFwProfile* GetCurrentProfile(INetFwPolicy* pFwPolicy);
INetFwMgr* CreateFwMgr();
INetFwPolicy* GetLocalPolicy(INetFwMgr* pFwMgr);
INetFwAuthorizedApplications* GetAuthorizedApplications();
INetFwAuthorizedApplication* GetAuthorizedApplicationItem(INetFwAuthorizedApplications* pFwApps,BSTR lpFwProcessImageFileName);
INetFwOpenPorts* GetGloballyOpenPorts();
INetFwOpenPort* GetGloballyOpenPortItem(INetFwOpenPorts* pFwOpenPorts,LONG lPortNumber, NET_FW_IP_PROTOCOL ipProtocol);


bool IsAppEnabled(INetFwAuthorizedApplication* pFwApp);
bool IsPortEnabled(INetFwOpenPort* pFwOpenPort,long lPortNumber, NET_FW_IP_PROTOCOL ipProtocol);


bool PutProcessImageFileName(INetFwAuthorizedApplication* pFwApp,BSTR bstrProcessImageFileName);
bool PutName(INetFwAuthorizedApplication* pFwApp,BSTR bstrName);
bool Add(INetFwAuthorizedApplications* pFwApps,INetFwAuthorizedApplication* pFwApp);
bool PutPort(INetFwOpenPort* pFwOpenPort, long lPortNumber);
bool PutProtocol(INetFwOpenPort* pFwOpenPort, NET_FW_IP_PROTOCOL ipProtocol);
bool PutName(INetFwOpenPort* pFwOpenPort, BSTR bstrName);
bool Add(INetFwOpenPorts* pFwOpenPorts,INetFwOpenPort* pFwOpenPort);


void Release(INetFwProfile* pFwProfile);
void Release(INetFwMgr* pFwMgr);
void Release(INetFwPolicy* pFwPolicy);
void Release(INetFwAuthorizedApplications* pFwApps);
void Release(INetFwAuthorizedApplication* pFwApp);
void Release(INetFwOpenPorts* pFwOpenPorts);
void Release(INetFwOpenPort* pFwOpenPort);


private:
INetFwProfile* m_pFwProfile;


INetFwMgr* m_pFwMgr;
INetFwPolicy* m_pFwPolicy;
};


WindowsFireWall.cpp




#include "StdAfx.h"
#include "WindowsFireWall.h"
CWindowsFireWall::CWindowsFireWall(void)
: m_pFwProfile(NULL)
, m_pFwMgr(NULL)
, m_pFwPolicy(NULL)
{
Initialize();
}


CWindowsFireWall::~CWindowsFireWall(void)
{
UnInitalize();
}


void CWindowsFireWall::Initialize()
{
HRESULT hComInit = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);


// Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
// initialized with a different mode. Since we don't care what the mode is,
// we'll just use the existing mode.
if (hComInit!=RPC_E_CHANGED_MODE && FAILED(hComInit))
{
return;
}


m_pFwMgr = CreateFwMgr();
m_pFwPolicy = GetLocalPolicy(m_pFwMgr);
m_pFwProfile = GetCurrentProfile(m_pFwPolicy);
}


void CWindowsFireWall::UnInitalize()
{
Release(m_pFwProfile);
m_pFwProfile = NULL;
Release(m_pFwPolicy);
m_pFwPolicy = NULL;
Release(m_pFwMgr);
m_pFwMgr = NULL;


CoUninitialize();
}


bool CWindowsFireWall::IsOpen(bool& pbFwOn)
{
pbFwOn = false;
if (NULL == m_pFwProfile)
{
return false;
}


VARIANT_BOOL fwEnabled = VARIANT_FALSE;
HRESULT hr = m_pFwProfile->get_FirewallEnabled(&fwEnabled);// Get the current state of the firewall.
if (FAILED(hr))
{
return false;
}


if (fwEnabled != VARIANT_FALSE)
{
pbFwOn = true;
}


return true;
}


bool CWindowsFireWall::Open()
{
if (NULL == m_pFwProfile)
{
return false;
}


bool bFwOn = false;
if (!IsOpen(bFwOn))
{
return false;
}


if (!bFwOn)
{
HRESULT hr = m_pFwProfile->put_FirewallEnabled(VARIANT_TRUE);;
if (FAILED(hr))
{
return false;
}
}


return true;
}




bool CWindowsFireWall::Close()
{
bool bFwOn = true;
if (!IsOpen(bFwOn))
{
return false;
}


if (bFwOn && m_pFwProfile!=NULL)
{
// Turnoff the firewall.
HRESULT hr = m_pFwProfile->put_FirewallEnabled(VARIANT_FALSE);
if (FAILED(hr))
{
return false;
}
}


return true;
}


bool CWindowsFireWall::AddApp(LPCTSTR lpFwProcessImageFileName,LPCTSTR lpFwName)
{
if (NULL == m_pFwProfile)
{
return false;
}


bool bAdd = false;


BSTR bstrProcessImageFileName = SysAllocString(lpFwProcessImageFileName);
INetFwAuthorizedApplications* pFwApps = GetAuthorizedApplications();
INetFwAuthorizedApplication* pFwAppTemp = GetAuthorizedApplicationItem(pFwApps,bstrProcessImageFileName);


bool bFwAppEnabled = IsAppEnabled(pFwAppTemp);
if (!bFwAppEnabled)
{
INetFwAuthorizedApplication* pFwApp = NULL;
BSTR bstrName = SysAllocString(lpFwName);


HRESULT hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication),NULL,CLSCTX_INPROC_SERVER,__uuidof(INetFwAuthorizedApplication),(void**)&pFwApp);
if (SUCCEEDED(hr) && pFwApp!=NULL)
{
if ( PutProcessImageFileName(pFwApp,bstrProcessImageFileName)
&& PutName(pFwApp,bstrName)
&& Add(pFwApps,pFwApp))
{
bFwAppEnabled = true;
}
}
SysFreeString(bstrName);
Release(pFwApp);
}
bAdd = bFwAppEnabled;


SysFreeString(bstrProcessImageFileName);
Release(pFwApps);
Release(pFwAppTemp);


return bAdd;
}


bool CWindowsFireWall::AddPort(long lPortNumber,NET_FW_IP_PROTOCOL ipProtocol,LPCTSTR pName)
{
if (NULL == m_pFwProfile)
{
return false;
}
bool bAdd = false;


INetFwOpenPorts* pFwOpenPorts = GetGloballyOpenPorts();
INetFwOpenPort* pFwOpenPortTemp = GetGloballyOpenPortItem(pFwOpenPorts,lPortNumber,ipProtocol);


bool bFwPortEnabled = IsPortEnabled(pFwOpenPortTemp,lPortNumber,ipProtocol);
if (!bFwPortEnabled)
{
INetFwOpenPort* pFwOpenPort = NULL;
BSTR bstrName = SysAllocString(pName);


// Create an instance of an open port.
HRESULT hr = CoCreateInstance(__uuidof(NetFwOpenPort),NULL, CLSCTX_INPROC_SERVER,__uuidof(INetFwOpenPort),(void**)&pFwOpenPort);
if (SUCCEEDED(hr) && pFwOpenPort!=NULL)
{
if (PutPort(pFwOpenPort,lPortNumber) 
&& PutProtocol(pFwOpenPort,ipProtocol) 
&& PutName(pFwOpenPort,bstrName)
&& Add(pFwOpenPorts,pFwOpenPort))
{
bFwPortEnabled = true;
}
}
SysFreeString(bstrName);
Release(pFwOpenPort);
}
bAdd = bFwPortEnabled;
Release(pFwOpenPorts);
Release(pFwOpenPortTemp);


return bAdd;
}


INetFwProfile* CWindowsFireWall::GetCurrentProfile(INetFwPolicy* pFwPolicy)
{
INetFwProfile* pFwProfile = NULL;


if (pFwPolicy != NULL)
{
HRESULT hr = pFwPolicy->get_CurrentProfile(&pFwProfile);
if (FAILED(hr) && pFwProfile!=NULL)
{
pFwProfile->Release();
pFwProfile = NULL;
}
}


return pFwProfile;
}


INetFwMgr* CWindowsFireWall::CreateFwMgr()
{
INetFwMgr* pFwMgr = NULL;


HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwMgr), (void**)&pFwMgr);
if (FAILED(hr) && pFwMgr != NULL)
{
pFwMgr->Release();
pFwMgr = NULL;
}


return pFwMgr;
}


INetFwPolicy* CWindowsFireWall::GetLocalPolicy(INetFwMgr* pFwMgr)
{
INetFwPolicy* pFwPolicy = NULL;


if (pFwMgr != NULL)
{
HRESULT hr = pFwMgr->get_LocalPolicy(&pFwPolicy);
if (FAILED(hr) && pFwPolicy!=NULL)
{
pFwPolicy->Release();
pFwPolicy = NULL;
}
}


return pFwPolicy;
}


INetFwAuthorizedApplications* CWindowsFireWall::GetAuthorizedApplications()
{
INetFwAuthorizedApplications* pFwApps = NULL;
if (m_pFwProfile != NULL)
{
HRESULT hr = m_pFwProfile->get_AuthorizedApplications(&pFwApps);
if (FAILED(hr) && pFwApps!=NULL)
{
pFwApps->Release(); 
pFwApps = NULL;
}
}

return pFwApps;
}


INetFwAuthorizedApplication* CWindowsFireWall::GetAuthorizedApplicationItem(INetFwAuthorizedApplications* pFwApps,BSTR bstrProcessImageFileName)
{
INetFwAuthorizedApplication* pFwApp = NULL;
if (pFwApps!=NULL && bstrProcessImageFileName!=NULL)
{
HRESULT hr = pFwApps->Item(bstrProcessImageFileName, &pFwApp);
if (FAILED(hr) && pFwApp!=NULL)
{
pFwApp->Release();
pFwApp = NULL;
}
}


return pFwApp;
}


INetFwOpenPorts* CWindowsFireWall::GetGloballyOpenPorts()
{
INetFwOpenPorts* pFwOpenPorts = NULL;


if (m_pFwProfile != NULL)
{
HRESULT hr = m_pFwProfile->get_GloballyOpenPorts(&pFwOpenPorts);
if (FAILED(hr))
{
Release(pFwOpenPorts);
pFwOpenPorts = NULL;
}
}


return pFwOpenPorts;
}


INetFwOpenPort* CWindowsFireWall::GetGloballyOpenPortItem(INetFwOpenPorts* pFwOpenPorts,LONG lPortNumber, NET_FW_IP_PROTOCOL ipProtocol)
{
INetFwOpenPort* pFwOpenPort = NULL;


if (pFwOpenPorts != NULL)
{
HRESULT hr = pFwOpenPorts->Item(lPortNumber, ipProtocol, &pFwOpenPort);
if (FAILED(hr))
{
Release(pFwOpenPort);
pFwOpenPort = NULL;
}
}


return pFwOpenPort;
}


bool CWindowsFireWall::IsAppEnabled(INetFwAuthorizedApplication* pFwApp)
{
bool bFwAppEnabled = false;
if (pFwApp != NULL)
{
VARIANT_BOOL fwEnabled = VARIANT_FALSE;
HRESULT hr = pFwApp->get_Enabled(&fwEnabled);
if (SUCCEEDED(hr))
{
bFwAppEnabled = (fwEnabled==VARIANT_TRUE);
}


}


return bFwAppEnabled;
}


bool CWindowsFireWall::IsPortEnabled(INetFwOpenPort* pFwOpenPort,long lPortNumber, NET_FW_IP_PROTOCOL ipProtocol)
{
bool bFwPortEnabled = false;


if (pFwOpenPort != NULL)
{
VARIANT_BOOL fwEnabled = VARIANT_FALSE;
HRESULT hr = pFwOpenPort->get_Enabled(&fwEnabled);
if (SUCCEEDED(hr) && VARIANT_TRUE==fwEnabled)
{
bFwPortEnabled = true;
}
}


return bFwPortEnabled;
}


bool CWindowsFireWall::PutProcessImageFileName(INetFwAuthorizedApplication* pFwApp,BSTR bstrProcessImageFileName)
{
if (pFwApp != NULL)
{
HRESULT hr = pFwApp->put_ProcessImageFileName(bstrProcessImageFileName);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


bool CWindowsFireWall::PutName(INetFwAuthorizedApplication* pFwApp,BSTR bstrName)
{
if (pFwApp != NULL)
{
HRESULT hr = pFwApp->put_Name(bstrName);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


bool CWindowsFireWall::Add(INetFwAuthorizedApplications* pFwApps,INetFwAuthorizedApplication* pFwApp)
{
if (pFwApps!=NULL && pFwApp!=NULL)
{
HRESULT hr = pFwApps->Add(pFwApp);
if (SUCCEEDED(hr))
{
return true;
}
}

return false;
}


bool CWindowsFireWall::PutPort(INetFwOpenPort* pFwOpenPort, long lPortNumber)
{
if (pFwOpenPort != NULL)
{
HRESULT hr = pFwOpenPort->put_Port(lPortNumber);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


bool CWindowsFireWall::PutProtocol(INetFwOpenPort* pFwOpenPort, NET_FW_IP_PROTOCOL ipProtocol)
{
if (pFwOpenPort != NULL)
{
HRESULT hr = pFwOpenPort->put_Protocol(ipProtocol);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


bool CWindowsFireWall::PutName(INetFwOpenPort* pFwOpenPort, BSTR bstrName)
{
if (pFwOpenPort!=NULL && bstrName!=NULL)
{
HRESULT hr = pFwOpenPort->put_Name(bstrName);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


bool CWindowsFireWall::Add(INetFwOpenPorts* pFwOpenPorts,INetFwOpenPort* pFwOpenPort)
{
if (pFwOpenPorts!=NULL && pFwOpenPort!=NULL)
{
HRESULT hr = pFwOpenPorts->Add(pFwOpenPort);
if (SUCCEEDED(hr))
{
return true;
}
}


return false;
}


void CWindowsFireWall::Release(INetFwProfile* pFwProfile)
{
if (pFwProfile != NULL)
{
pFwProfile->Release();
}
}


void CWindowsFireWall::Release(INetFwPolicy* pFwPolicy)
{
if (pFwPolicy != NULL)
{
pFwPolicy->Release();
}
}


void CWindowsFireWall::Release(INetFwMgr* pFwMgr)
{
if (pFwMgr != NULL)
{
pFwMgr->Release();
}
}


void CWindowsFireWall::Release(INetFwAuthorizedApplications* pFwApps)
{
if (pFwApps != NULL)
{
pFwApps->Release();
}
}


void CWindowsFireWall::Release(INetFwAuthorizedApplication* pFwApp)
{
if (pFwApp != NULL)
{
pFwApp->Release();
}
}


void CWindowsFireWall::Release(INetFwOpenPorts* pFwOpenPorts)
{
if (pFwOpenPorts != NULL)
{
pFwOpenPorts->Release();
}
}


void CWindowsFireWall::Release(INetFwOpenPort* pFwOpenPort)
{
if (pFwOpenPort != NULL)
{
pFwOpenPort->Release();
}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值