📄 winfirewall.cc
字号:
#ifdef _HAVE_PLATFORM_SDK#include <comdef.h>#include "netfw.h"#include "winfirewall.h"#define RELEASE(lpUnk) do \ { if ((lpUnk) != NULL) { (lpUnk)->Release(); (lpUnk) = NULL; } } while (0)namespace utils_base {//////////////////////////////////////////////////////////////////////// WinFirewall//////////////////////////////////////////////////////////////////////WinFirewall::WinFirewall() : mgr_(NULL), policy_(NULL), profile_(NULL) {}WinFirewall::~WinFirewall() { Shutdown();}boolWinFirewall::Initialize() { if (mgr_) return true; HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr), 0, CLSCTX_INPROC_SERVER, __uuidof(INetFwMgr), reinterpret_cast<void **>(&mgr_)); if (SUCCEEDED(hr) && (mgr_ != NULL)) hr = mgr_->get_LocalPolicy(&policy_); if (SUCCEEDED(hr) && (policy_ != NULL)) hr = policy_->get_CurrentProfile(&profile_); return SUCCEEDED(hr) && (profile_ != NULL);}voidWinFirewall::Shutdown() { RELEASE(profile_); RELEASE(policy_); RELEASE(mgr_);}boolWinFirewall::Enabled() { if (!profile_) return false; VARIANT_BOOL fwEnabled = VARIANT_FALSE; profile_->get_FirewallEnabled(&fwEnabled); return (fwEnabled != VARIANT_FALSE);}boolWinFirewall::Authorized(const char * filename, bool * known) { if (known) { *known = false; } if (!profile_) return false; VARIANT_BOOL fwEnabled = VARIANT_FALSE; _bstr_t bfilename = filename; INetFwAuthorizedApplications * apps = NULL; HRESULT hr = profile_->get_AuthorizedApplications(&apps); if (SUCCEEDED(hr) && (apps != NULL)) { INetFwAuthorizedApplication * app = NULL; hr = apps->Item(bfilename, &app); if (SUCCEEDED(hr) && (app != NULL)) { hr = app->get_Enabled(&fwEnabled); app->Release(); if (known) { *known = true; } } else if (hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) { // Unexpected error } apps->Release(); } return (fwEnabled != VARIANT_FALSE);}boolWinFirewall::AddApplication(const char * filename, const char * friendly_name, bool authorized) { INetFwAuthorizedApplications * apps = NULL; HRESULT hr = profile_->get_AuthorizedApplications(&apps); if (SUCCEEDED(hr) && (apps != NULL)) { INetFwAuthorizedApplication * app = NULL; hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication), 0, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), reinterpret_cast<void **>(&app)); if (SUCCEEDED(hr) && (app != NULL)) { _bstr_t bstr = filename; hr = app->put_ProcessImageFileName(bstr); bstr = friendly_name; if (SUCCEEDED(hr)) hr = app->put_Name(bstr); if (SUCCEEDED(hr)) hr = app->put_Enabled(authorized ? VARIANT_TRUE : VARIANT_FALSE); if (SUCCEEDED(hr)) hr = apps->Add(app); app->Release(); } apps->Release(); } return SUCCEEDED(hr);}} // namespace talk_base#endif // _HAVE_PLATFORM_SDK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -