xp windows防火牆

來源:互聯網
上載者:User
// FwConsole.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include "windows.h"#include <Netfw.h>int main(){HRESULT hr = E_FAIL;INetFwMgr *pFwMgr = NULL;INetFwPolicy *pFwPolicy = NULL;CoInitialize(NULL);hr = CoCreateInstance(CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, IID_INetFwMgr, (void**)&pFwMgr);if (SUCCEEDED(hr) && pFwMgr){if (SUCCEEDED(pFwMgr->get_LocalPolicy(&pFwPolicy)) && pFwPolicy){INetFwProfile *pFwProfile = NULL;if (SUCCEEDED(pFwPolicy->get_CurrentProfile(&pFwProfile)) && pFwProfile){//判斷防火牆是否允許例外VARIANT_BOOL bExcepAlowed;if (SUCCEEDED(pFwProfile->get_ExceptionsNotAllowed(&bExcepAlowed)) && bExcepAlowed){printf("not allowed Exception");}else{printf("allowed Exception");}//防火牆是否已開啟VARIANT_BOOL bOpend;if (SUCCEEDED(pFwProfile->get_FirewallEnabled(&bOpend)) && bOpend){printf("\nOpened");}else{printf("\nclosed");// Turn the firewall on.pFwProfile->put_FirewallEnabled(VARIANT_TRUE);printf("\nThe firewall is now on.\n");}//防火牆阻止程式時,是否通知VARIANT_BOOL bNotify;if (SUCCEEDED(pFwProfile->get_NotificationsDisabled(&bNotify)) && bNotify){printf("\nnot Notify");}else{printf("\nNotify");}//查看連接埠是否添加進防火牆INetFwOpenPorts *pFwOpenPortS = NULL;INetFwOpenPort *pFwOpenPort = NULL;pFwProfile->get_GloballyOpenPorts(&pFwOpenPortS);hr = CoCreateInstance(__uuidof(NetFwOpenPort), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwOpenPort), (void**)&pFwOpenPort);if (SUCCEEDED(hr) && pFwOpenPortS){hr = pFwOpenPortS->Item(9999, NET_FW_IP_PROTOCOL_TCP/*include TCP AND UDP*/, &pFwOpenPort);if (SUCCEEDED(hr) && pFwOpenPort){VARIANT_BOOL bEnable;hr = pFwOpenPort->get_Enabled(&bEnable);if (SUCCEEDED(hr) && bEnable){printf("\n9999 is Enabled.");}else{printf("\n9999 is not  Enabled.");}}else{// Create an instance of an open port. hr = CoCreateInstance(__uuidof(NetFwOpenPort), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwOpenPort), (void**)&pFwOpenPort);if (SUCCEEDED(hr)){pFwOpenPort->put_Port(9999);pFwOpenPort->put_Protocol(NET_FW_IP_PROTOCOL_TCP);pFwOpenPort->put_Enabled(VARIANT_TRUE);pFwOpenPort->put_Name(_T("FireWallTest"));if (S_OK == pFwOpenPortS->Add(pFwOpenPort)){printf("\nadd 9999 port.");}else{printf("\nadd 9999 port failed.");}}}}//INetFwAuthorizedApplications *pFwAuthAppS = NULL;INetFwAuthorizedApplication *pFwAuthApp = NULL;//CoCreateInstance(__uuidof(INetFwAuthorizedApplications), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplications), (void**)&pFwAuthAppS);hr = pFwProfile->get_AuthorizedApplications(&pFwAuthAppS);if (SUCCEEDED(hr) && pFwAuthAppS){hr = CoCreateInstance(CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_ALL, IID_INetFwAuthorizedApplication, (void**)&pFwAuthApp);if (SUCCEEDED(hr)){//添加APP規則BSTR bstr122= SysAllocString(_T("D:\\FwTest.exe"));BSTR bstr121= SysAllocString(_T("FwTest"));pFwAuthApp->put_Name(bstr121);pFwAuthApp->put_ProcessImageFileName(bstr122);hr = pFwAuthAppS->Add(pFwAuthApp);if (SUCCEEDED(hr)){printf("\nadd FwTest.exe app");}else{printf("\nadd FwTest.exe failed");}SysFreeString(bstr122);SysFreeString(bstr121);}}}}}CoUninitialize();system("pause");}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.