CPP > Win32 > Firewal(방화벽)

Redjini WiKi
< cpp‎ | win32


INetFwProfile 인터페이스 구하기

셈플소스
BOOL Firewall::Init(){		
	CComPtr<INetFwMgr>		pFwMgr		= NULL;
	CComPtr<INetFwPolicy>	pFwPolicy	= NULL;
	HRESULT					hResult		= S_OK;

	//INetFwMgr 생성
	hResult = CoCreateInstance(CLSID_NetFwMgr, NULL, CLSCTX_ALL, IID_INetFwMgr, (void**)&pFwMgr);
	if( FAILED(hResult) ){
		BLOG_E("CoCreateInstance FAILED: 0x%08lx\n", hResult);
		return FALSE;
	} 

	//로컬 방화벽 정책 얻기(INetFwPolicy)
	hResult = pFwMgr->get_LocalPolicy(&pFwPolicy);
	if( FAILED(hResult) ){
		BLOG_E("get_LocalPolicy FAILED: 0x%08lx\n", hResult);
		return FALSE;
	}

	//INetFwProfile 얻기
	hResult = pFwPolicy->get_CurrentProfile(&m_pFwProfile);
	if( FAILED(hResult) ){
		BLOG_E("get_CurrentProfile FAILED: 0x%08lx\n", hResult);
		return FALSE;
	}

	return TRUE;
}
참고사이트

허용되는 앱 등록

함수
  • INetFwProfile::AuthorizedApplications
HRESULT get_AuthorizedApplications( [out] INetFwAuthorizedApplications **applications ); 
  • INetFwAuthorizedApplications::Item
HRESULT Item(
  [in]  BSTR                        imageFileName,
  [out] INetFwAuthorizedApplication **application,
  [in]  String                      String imageFileName,
  [out] INetFwAuthorizedApplication  application
);
  • INetFwAuthorizedApplication
HRESULT put_Enabled(
  [in]  VARIANT_BOOL enabled
);

HRESULT get_Enabled(
  [out] VARIANT_BOOL *enabled
);

HRESULT put_ProcessImageFileName(
  [in]  BSTR imageFileName
);

HRESULT get_ProcessImageFileName(
  [out] BSTR *imageFileName
);

HRESULT put_Name(
  [in]  BSTR name
);

HRESULT get_Name(
  [out] BSTR *name
);
);
등록 확인
BOOL Firewall::IsAppOn(LPCTSTR sName, LPCTSTR sPath){
	CComPtr<INetFwAuthorizedApplication>	pFwApp	= NULL;
	CComPtr<INetFwAuthorizedApplications>	pFwApps = NULL;
	VARIANT_BOOL	bOn		= FALSE;
	HRESULT			hResult	= S_OK;

	//등록된 어플리케이션 목록 구하기
	hResult = m_pFwProfile->get_AuthorizedApplications(&pFwApps);
	if( FAILED(hResult) ){
		return FALSE;
	}

	//어플리케이션 구하기
	hResult = pFwApps->Item(CComBSTR(sPath), &pFwApp);
	if( FAILED(hResult) ){
		return FALSE;
	}

	// Enable & Disable 얻기
	hResult = pFwApp->get_Enabled(&bOn);
	if( FAILED(hResult) ){
		return FALSE;
	}

	if(bOn == VARIANT_TRUE){
		return TRUE;
	}else{
		return FALSE;
	}
}
등록하기
BOOL Firewall::AddApp(LPCTSTR sName, LPCTSTR sPath){
	CComPtr<INetFwAuthorizedApplication>	pFwApp	= NULL;
	CComPtr<INetFwAuthorizedApplications>	pFwApps	= NULL;
	HRESULT			hResult	= S_OK;

	//등록된 어플리케이션 목록 구하기
	hResult = m_pFwProfile->get_AuthorizedApplications(&pFwApps);
	if( FAILED(hResult) ){
		return FALSE;
	}
	
	//등록할 어플리케이션 생성
	hResult = CoCreateInstance(CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_ALL, IID_INetFwAuthorizedApplication, (void**)&pFwApp);
	if( FAILED(hResult) ){
		return FALSE;
	}

	//등록된 어플리케이션 경로
	hResult = pFwApp->put_ProcessImageFileName(CComBSTR(sPath));
	if( FAILED(hResult) ){		
		return FALSE;
	}

	//등록된 어플리케이션 이름
	hResult = pFwApp->put_Name(CComBSTR(sName));
	if( FAILED(hResult) ){
		return FALSE;
	}

	//등록하기
	hResult = pFwApps->Add(pFwApp);
	if( FAILED(hResult) ){
		return FALSE;
	}

	return IsAppOn(sName, sPath);
}

방화벽 Enable/Disable

함수
  • INetFwPolicy2::FirewallEnabled
HRESULT put_FirewallEnabled(
  [in]  NET_FW_PROFILE_TYPE2 profileType,
  [in]  VARIANT_BOOL         enabled
);

HRESULT get_FirewallEnabled(
  [in]  NET_FW_PROFILE_TYPE2 profileType,
  [out] VARIANT_BOOL         *enabled
);
오류코드
  • E_ACCESSDENIED
    • 권한 문제로 인해 작업이 중단되었습니다.
  • E_INVALIDARG
    • 매개 변수가 유효하지 않아 메서드가 실패했습니다.
  • E_OUTOFMEMORY
    • 메서드가 필요한 메모리를 할당 할 수 없습니다.
  • E_POINTER
    • 포인터가 유효하지 않기 때문에 메서드가 실패했습니다.
방화벽 On
BOOL Firewall::On(UINT nType){
	CComPtr<INetFwPolicy2>	pFwPolicy2	= NULL;
	HRESULT					hResult		= S_OK;

	if(IsOn()==FALSE){
		hResult = CoCreateInstance(CLSID_NetFwPolicy2, NULL, CLSCTX_ALL, IID_INetFwPolicy2, (void**)&pFwPolicy2);
		if( FAILED(hResult) ){
			BLOG_E("CoCreateInstance failed: 0x%08lx\n", hResult);
			return FALSE;
		} 

		if(nType & NET_FW_PROFILE2_DOMAIN ) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN , VARIANT_TRUE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN ) FAILED: 0x%08lx\n", hResult);
		}

		if(nType & NET_FW_PROFILE2_PRIVATE) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, VARIANT_TRUE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE) FAILED: 0x%08lx\n", hResult);
		}

		if(nType & NET_FW_PROFILE2_PUBLIC ) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC , VARIANT_TRUE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC ) FAILED: 0x%08lx\n", hResult);
		}
	}
	return TRUE;
}
방화벽 Off
BOOL Firewall::Off(UINT nType){
	CComPtr<INetFwPolicy2>	pFwPolicy2	= NULL;
	HRESULT					hResult		= S_OK;
	
	if(IsOn()==TRUE ){
		hResult = CoCreateInstance(CLSID_NetFwPolicy2, NULL, CLSCTX_ALL, IID_INetFwPolicy2, (void**)&pFwPolicy2);
		if( FAILED(hResult) ){
			BLOG_E("CoCreateInstance failed: 0x%08lx\n", hResult);
			return FALSE;
		} 

		if(nType & NET_FW_PROFILE2_DOMAIN ) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN , VARIANT_FALSE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN ) FAILED: 0x%08lx\n", hResult);
		}

		if(nType & NET_FW_PROFILE2_PRIVATE) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, VARIANT_FALSE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE) FAILED: 0x%08lx\n", hResult);
		}

		if(nType & NET_FW_PROFILE2_PUBLIC ) hResult = pFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC , VARIANT_FALSE);
		if( FAILED(hResult) ){
			BLOG_E("put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC ) FAILED: 0x%08lx\n", hResult);
		}
	}
	return TRUE;
}
참고사이트