Windows防火墙启用,禁用,添加例外端口和应用程序

以下代码参考自MSDN,不过经过一点点修改,自己也逐个方法调试了,完全可行。网上的很多博客都是直接拷贝MSDN的代码,我的机器

是Windows XP  Professional SP3,需要安装对应的Windows SDK,并在VC的包含目录和静态库目录中

添加分别SDK的include目录和库目录,直接上代码,每个方法都写得很清楚

[cpp]  view plain copy
  1. #include <Windows.h>  
  2. #include <rpcsal.h> // MSDN的代码中这里没有包含这个头文件,导致编译不过  
  3. #include <crtdbg.h>  
  4. #include <objbase.h>  
  5. #include <oleauto.h>  
  6. #include <stdio.h>  
  7. #include <netfw.h>  
  8.   
  9. #pragma comment( lib, "ole32.lib" )  
  10. #pragma comment( lib, "oleaut32.lib" )  
  11.   
  12. HRESULT WindowsFirewallInitialize(OUT INetFwProfile** fwProfile)  
  13. {  
  14.     HRESULT hr = S_OK;  
  15.     INetFwMgr *fwMgr = NULL;  
  16.     INetFwPolicy *fwPolicy = NULL;  
  17.   
  18.     _ASSERT(fwProfile != NULL);  
  19.   
  20.     *fwProfile = NULL;  
  21.   
  22.     hr = CoCreateInstance(  
  23.         __uuidof(NetFwMgr),  
  24.         NULL,  
  25.         CLSCTX_INPROC_SERVER,  
  26.         __uuidof(INetFwMgr),  
  27.         (void **)&fwMgr);  
  28.   
  29.     if (FAILED(hr))  
  30.     {  
  31.         printf("CoCreateInstance failed: 0x%08lx\n", hr);  
  32.         goto error;  
  33.     }  
  34.   
  35.     hr = fwMgr->get_LocalPolicy(&fwPolicy);  
  36.     if (FAILED(hr))  
  37.     {  
  38.         printf("get_localPolicy failed: 0x%08lx\n", hr);  
  39.         goto error;  
  40.     }  
  41.   
  42.     hr = fwPolicy->get_CurrentProfile(fwProfile);  
  43.     if (FAILED(hr))  
  44.     {  
  45.         printf("get_CurrentProfile failed: 0x%08lx\n", hr);  
  46.         goto error;  
  47.     }  
  48.       
  49. error:  
  50.     if (fwPolicy != NULL)  
  51.     {  
  52.         fwPolicy->Release();  
  53.     }  
  54.   
  55.     if (fwMgr != NULL)  
  56.     {  
  57.         fwMgr->Release();  
  58.     }  
  59.   
  60.     return hr;  
  61. }  
  62.   
  63. void WindowsFirewallCleanup(IN INetFwProfile *fwProfile)  
  64. {  
  65.     if (fwProfile != NULL)  
  66.     {  
  67.         fwProfile->Release();  
  68.     }  
  69. }  
  70.   
  71. HRESULT WindowsFirewallIsOn(IN INetFwProfile *fwProfile, OUT BOOL *fwOn)  
  72. {  
  73.     HRESULT hr = S_OK;  
  74.     VARIANT_BOOL fwEnabled;  
  75.   
  76.     _ASSERT(fwProfile != NULL);  
  77.     _ASSERT(fwOn != NULL);  
  78.   
  79.     *fwOn = FALSE;  
  80.   
  81.     hr = fwProfile->get_FirewallEnabled(&fwEnabled);  
  82.     if (FAILED(hr))  
  83.     {  
  84.         printf("get_FirewallEnabled failed: 0x%08lx\n", hr);  
  85.         goto error;  
  86.     }  
  87.   
  88.     if (fwEnabled != VARIANT_FALSE)  
  89.     {  
  90.         *fwOn = TRUE;  
  91.         printf("The firewall is on.\n");  
  92.     }  
  93.     else  
  94.     {  
  95.         printf("The firewall is off.\n");  
  96.     }  
  97.   
  98. error:  
  99.   
  100.     return hr;  
  101. }  
  102.   
  103. HRESULT WindowsFirewallTurnOn(IN INetFwProfile *fwProfile)  
  104. {  
  105.     HRESULT hr = S_OK;  
  106.     BOOL fwOn;  
  107.   
  108.     _ASSERT(fwProfile != NULL);  
  109.   
  110.     hr = WindowsFirewallIsOn(fwProfile, &fwOn);  
  111.     if (FAILED(hr))  
  112.     {  
  113.         printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr);  
  114.         goto error;  
  115.     }  
  116.   
  117.     if (!fwOn)  
  118.     {  
  119.         hr = fwProfile->put_FirewallEnabled(VARIANT_TRUE);  
  120.         if (FAILED(hr))  
  121.         {  
  122.             printf("put_FirewallEnabled failed: 0x%08lx\n", hr);  
  123.             goto error;  
  124.         }  
  125.   
  126.         printf("The firewall is now on.\n");  
  127.     }  
  128.   
  129. error:  
  130.       
  131.     return hr;  
  132. }  
  133.   
  134. HRESULT WindowsFirewallTurnOff(IN INetFwProfile *fwProfile)  
  135. {  
  136.     HRESULT hr = S_OK;  
  137.     BOOL fwOn;  
  138.   
  139.     _ASSERT(fwProfile != NULL);  
  140.   
  141.     hr = WindowsFirewallIsOn(fwProfile, &fwOn);  
  142.     if (FAILED(hr))  
  143.     {  
  144.         printf("WindowsFirewallIsOn failed: 0x%08lx\n", hr);  
  145.         goto error;  
  146.     }  
  147.   
  148.     if (fwOn)  
  149.     {  
  150.         hr = fwProfile->put_FirewallEnabled(VARIANT_FALSE);  
  151.         if (FAILED(hr))  
  152.         {  
  153.             printf("put_FirewallEnabled failed: 0x%08lx\n", hr);  
  154.             goto error;  
  155.         }  
  156.   
  157.         printf("The firewall is now off.\n");  
  158.     }  
  159.   
  160. error:  
  161.   
  162.     return hr;  
  163. }  
  164.   
  165. HRESULT WindowsFirewallAppIsEnabled(  
  166.             IN INetFwProfile * fwProfile,  
  167.             IN const wchar_t *fwProcessImageFileName,  
  168.             OUT BOOL *fwAppEnabled)  
  169. {  
  170.     HRESULT hr = S_OK;  
  171.     BSTR fwBstrProcessImageFileName = NULL;  
  172.     VARIANT_BOOL fwEnabled;  
  173.     INetFwAuthorizedApplication *fwApp = NULL;  
  174.     INetFwAuthorizedApplications *fwApps = NULL;  
  175.   
  176.     _ASSERT(fwProfile != NULL);  
  177.     _ASSERT(fwProcessImageFileName != NULL);  
  178.     _ASSERT(fwAppEnabled != NULL);  
  179.   
  180.     *fwAppEnabled = false;  
  181.   
  182.     // 获取授权的程序集  
  183.     hr = fwProfile->get_AuthorizedApplications(&fwApps);  
  184.     if (FAILED(hr))  
  185.     {  
  186.         printf("get_AuthorizedApplications failed: 0x%08lx\n", hr);  
  187.         goto error;  
  188.     }  
  189.   
  190.     fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);  
  191.     if (fwBstrProcessImageFileName == NULL)  
  192.     {  
  193.         hr = E_OUTOFMEMORY;  
  194.         printf("SysAllocString failed: 0x%08lx\n", hr);  
  195.         goto error;  
  196.     }  
  197.   
  198.     hr = fwApps->Item(fwBstrProcessImageFileName, &fwApp);  
  199.     if (SUCCEEDED(hr))  
  200.     {  
  201.         hr = fwApp->get_Enabled(&fwEnabled);  
  202.         if (FAILED(hr))  
  203.         {  
  204.             printf("get_Enabled failed: 0x%08lx\n", hr);  
  205.             goto error;  
  206.         }  
  207.   
  208.         if (fwEnabled != VARIANT_FALSE)  
  209.         {  
  210.             *fwAppEnabled = TRUE;  
  211.   
  212.             printf(  
  213.                 "Authorized application %lS is enabled in the firewall.\n",  
  214.                 fwProcessImageFileName  
  215.                 );  
  216.         }  
  217.         else  
  218.         {  
  219.             printf(  
  220.                 "Authorized application %lS is disabled in the firewall.\n",  
  221.                 fwProcessImageFileName  
  222.                 );  
  223.         }  
  224.     }  
  225.     else  
  226.     {  
  227.         hr = S_OK;  
  228.   
  229.         printf(  
  230.             "Authorized application %lS is disabled in the firewall.\n",  
  231.             fwProcessImageFileName  
  232.             );  
  233.     }  
  234.   
  235. error:  
  236.   
  237.     SysFreeString(fwBstrProcessImageFileName);  
  238.   
  239.     if (fwApp != NULL)  
  240.     {  
  241.         fwApp->Release();  
  242.     }  
  243.   
  244.     if (fwApps != NULL)  
  245.     {  
  246.         fwApps->Release();  
  247.     }  
  248.   
  249.     return hr;  
  250. }  
  251.   
  252. HRESULT WindowsFirewallAddApp(  
  253.             IN INetFwProfile *fwProfile,  
  254.             IN const wchar_t *fwProcessImageFileName,  
  255.             IN const wchar_t *fwName)  
  256. {  
  257.     HRESULT hr = S_OK;  
  258.     BOOL fwAppEnabled;  
  259.     BSTR fwBstrName = NULL;  
  260.     BSTR fwBstrProcessImageFileName = NULL;  
  261.     INetFwAuthorizedApplication *fwApp = NULL;  
  262.     INetFwAuthorizedApplications *fwApps = NULL;  
  263.   
  264.     _ASSERT(fwProfile != NULL);  
  265.     _ASSERT(fwProcessImageFileName != NULL);  
  266.     _ASSERT(fwName != NULL);  
  267.   
  268.     hr = WindowsFirewallAppIsEnabled(  
  269.         fwProfile,  
  270.         fwProcessImageFileName,  
  271.         &fwAppEnabled);  
  272.     if (FAILED(hr))  
  273.     {  
  274.         printf("WindowsFirewallAppIsEnabled failed: 0x%08lx\n", hr);  
  275.         goto error;  
  276.     }  
  277.   
  278.     if (!fwAppEnabled)  
  279.     {  
  280.         hr = fwProfile->get_AuthorizedApplications(&fwApps);  
  281.         if (FAILED(hr))  
  282.         {  
  283.             printf("get_AuthorizedApplications failed: 0x%08lx\n", hr);  
  284.             goto error;  
  285.         }  
  286.   
  287.         hr = CoCreateInstance(  
  288.             __uuidof(NetFwAuthorizedApplication),  
  289.             NULL,  
  290.             CLSCTX_INPROC_SERVER,  
  291.             __uuidof(INetFwAuthorizedApplication),  
  292.             (void**)&fwApp);  
  293.   
  294.         if (FAILED(hr))  
  295.         {  
  296.             printf("CoCreateInstance failed: 0x%08lx\n", hr);  
  297.             goto error;  
  298.         }  
  299.   
  300.         fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);  
  301.         if (fwBstrProcessImageFileName == NULL)  
  302.         {  
  303.             hr = E_OUTOFMEMORY;  
  304.             printf("SysAllocString failed: 0x%08lx\n", hr);  
  305.             goto error;  
  306.         }  
  307.   
  308.         hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName);  
  309.         if (FAILED(hr))  
  310.         {  
  311.             printf("put_ProcessImageFileName failed: 0x%08lx\n", hr);  
  312.             goto error;  
  313.         }  
  314.   
  315.         fwBstrName = SysAllocString(fwName);  
  316.         if (SysStringLen(fwBstrName) == 0)  
  317.         {  
  318.             hr = E_OUTOFMEMORY;  
  319.             printf("SysAllocString failed: 0x%08lx\n", hr);  
  320.             goto error;  
  321.         }  
  322.   
  323.         hr = fwApp->put_Name(fwBstrName);  
  324.         if (FAILED(hr))  
  325.         {  
  326.             printf("put_Name failed: 0x%08lx\n", hr);  
  327.             goto error;  
  328.         }  
  329.   
  330.         hr = fwApps->Add(fwApp);  
  331.         if (FAILED(hr))  
  332.         {  
  333.             printf("Add failed: 0x%08lx\n", hr);  
  334.             goto error;  
  335.         }  
  336.   
  337.         printf(  
  338.             "Authorized application %lS is now enabled in the firewall.\n",  
  339.             fwProcessImageFileName  
  340.             );  
  341.     }  
  342.   
  343. error:  
  344.   
  345.     SysFreeString(fwBstrName);  
  346.     SysFreeString(fwBstrProcessImageFileName);  
  347.   
  348.     if (fwApp != NULL)  
  349.     {  
  350.         fwApp->Release();  
  351.     }  
  352.   
  353.     if (fwApps != NULL)  
  354.     {  
  355.         fwApps->Release();  
  356.     }  
  357.   
  358.     return hr;  
  359. }  
  360.   
  361. HRESULT WindowsFirewallPortIsEnabled(  
  362.             IN INetFwProfile *fwProfile,  
  363.             IN LONG portNumber,  
  364.             IN NET_FW_IP_PROTOCOL ipProtocol,  
  365.             OUT BOOL *fwPortEnabled)  
  366. {  
  367.     HRESULT hr = S_OK;  
  368.     VARIANT_BOOL fwEnabled;  
  369.     INetFwOpenPort *fwOpenPort = NULL;  
  370.     INetFwOpenPorts *fwOpenPorts = NULL;  
  371.       
  372.     _ASSERT(fwProfile != NULL);  
  373.     _ASSERT(fwPortEnabled != NULL);  
  374.   
  375.     *fwPortEnabled = FALSE;  
  376.   
  377.     hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);  
  378.     if (FAILED(hr))  
  379.     {  
  380.         printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr);  
  381.         goto error;  
  382.     }  
  383.   
  384.     // 获取端口的设备上下文  
  385.     hr = fwOpenPorts->Item(portNumber, ipProtocol, &fwOpenPort);  
  386.     if (SUCCEEDED(hr))  
  387.     {  
  388.         hr = fwOpenPort->get_Enabled(&fwEnabled);  
  389.         if (FAILED(hr))  
  390.         {  
  391.             printf("get_Enabled failed: 0x%08lx\n", hr);  
  392.             goto error;  
  393.         }  
  394.   
  395.         if (fwEnabled != VARIANT_FALSE)  
  396.         {  
  397.             *fwPortEnabled = TRUE;  
  398.             printf("Port %ld is open in the firewall.\n", portNumber);  
  399.         }  
  400.         else  
  401.         {  
  402.             printf("Port %ld is not open in the firewall.\n", portNumber);  
  403.         }  
  404.     }  
  405.     else  
  406.     {  
  407.         hr = S_OK;  
  408.         printf("Port %ld is not open in the firewall.\n", portNumber);  
  409.     }  
  410.   
  411. error:  
  412.   
  413.     if (fwOpenPort != NULL)  
  414.     {  
  415.         fwOpenPort->Release();  
  416.     }  
  417.   
  418.     if (fwOpenPorts != NULL)  
  419.     {  
  420.         fwOpenPorts->Release();  
  421.     }  
  422.   
  423.     return hr;  
  424. }  
  425.   
  426. HRESULT WindowsFirewallPortAdd(  
  427.             IN INetFwProfile* fwProfile,  
  428.             IN LONG portNumber,  
  429.             IN NET_FW_IP_PROTOCOL ipProtocol,  
  430.             IN const wchar_t *name)  
  431. {  
  432.     HRESULT hr = S_OK;  
  433.     BOOL fwPortEnabled;  
  434.     BSTR fwBstrName = NULL;  
  435.     INetFwOpenPort *fwOpenPort = NULL;  
  436.     INetFwOpenPorts *fwOpenPorts = NULL;  
  437.   
  438.     _ASSERT(fwProfile != NULL);  
  439.     _ASSERT(name != NULL);  
  440.   
  441.     hr = WindowsFirewallPortIsEnabled(  
  442.         fwProfile,  
  443.         portNumber,  
  444.         ipProtocol,  
  445.         &fwPortEnabled);  
  446.     if (FAILED(hr))  
  447.     {  
  448.         printf("WindowsFirewallPortIsEnabled failed: 0x%08lx\n", hr);  
  449.         goto error;  
  450.     }  
  451.   
  452.     if (!fwPortEnabled)  
  453.     {  
  454.         hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);  
  455.         if (FAILED(hr))  
  456.         {  
  457.             printf("get_GloballyOpenPorts failed: 0x%08lx\n", hr);  
  458.             goto error;  
  459.         }  
  460.   
  461.         hr = CoCreateInstance(  
  462.             __uuidof(NetFwOpenPort),  
  463.             NULL,  
  464.             CLSCTX_INPROC_SERVER,  
  465.             __uuidof(INetFwOpenPort),  
  466.             (void**)&fwOpenPort);  
  467.         if (FAILED(hr))  
  468.         {  
  469.             printf("CoCreateInstance failed: 0x%08lx\n", hr);  
  470.             goto error;  
  471.         }  
  472.   
  473.         hr = fwOpenPort->put_Port(portNumber);  
  474.         if (FAILED(hr))  
  475.         {  
  476.             printf("put_Port failed: 0x%08lx\n", hr);  
  477.             goto error;  
  478.         }  
  479.   
  480.         hr = fwOpenPort->put_Protocol(ipProtocol);  
  481.         if (FAILED(hr))  
  482.         {  
  483.             printf("put_Protocol failed: 0x%08lx\n", hr);  
  484.             goto error;  
  485.         }  
  486.   
  487.         fwBstrName = SysAllocString(name);  
  488.         if (SysStringLen(fwBstrName) == 0)  
  489.         {  
  490.             hr = E_OUTOFMEMORY;  
  491.             printf("SysAllocString failed: 0x%08lx\n", hr);  
  492.             goto error;  
  493.         }  
  494.   
  495.         hr = fwOpenPort->put_Name(fwBstrName);  
  496.         if (FAILED(hr))  
  497.         {  
  498.             printf("put_Name failed: 0x%08lx\n", hr);  
  499.             goto error;  
  500.         }  
  501.   
  502.         hr = fwOpenPorts->Add(fwOpenPort);  
  503.         if (FAILED(hr))  
  504.         {  
  505.             printf("Add failed: 0x%08lx\n", hr);  
  506.             goto error;  
  507.         }  
  508.   
  509.         printf("Port %ld is now open in the firewall.\n", portNumber);  
  510.     }  
  511.   
  512. error:  
  513.   
  514.     SysFreeString(fwBstrName);  
  515.   
  516.     if (fwOpenPort != NULL)  
  517.     {  
  518.         fwOpenPort->Release();  
  519.     }  
  520.   
  521.     if (fwOpenPorts != NULL)  
  522.     {  
  523.         fwOpenPorts->Release();  
  524.     }  
  525.   
  526.     return hr;  
  527. }  
  528.   
  529. int _tmain(int argc, _TCHAR* argv[])  
  530. {  
  531.     HRESULT hr = S_OK;  
  532.     HRESULT comInit = E_FAIL;  
  533.     INetFwProfile *fwProfile = NULL;  
  534.   
  535.     comInit = CoInitializeEx(  
  536.         0,  
  537.         COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);  
  538.   
  539.     if (comInit != RPC_E_CHANGED_MODE)  
  540.     {  
  541.         hr = comInit;  
  542.         if (FAILED(hr))  
  543.         {  
  544.             printf("CoInitializeEx failed: 0x%08lx\n", hr);  
  545.             goto error;  
  546.         }  
  547.     }  
  548.   
  549.     hr = WindowsFirewallInitialize(&fwProfile);  
  550.     if (FAILED(hr))  
  551.     {  
  552.         printf("WindowsFirewallInitialize failed: 0x%08lx\n", hr);  
  553.         goto error;  
  554.     }  
  555.   
  556.     hr = WindowsFirewallTurnOff(fwProfile);  
  557.     if (FAILED(hr))  
  558.     {  
  559.         printf("WindowsFirewallTurnOff failed: 0x%08lx\n", hr);  
  560.         goto error;  
  561.     }  
  562.   
  563.     hr = WindowsFirewallTurnOn(fwProfile);  
  564.     if (FAILED(hr))  
  565.     {  
  566.         printf("WindowsFirewallTurnOn failed: 0x%08lx\n", hr);  
  567.         goto error;  
  568.     }  
  569.   
  570.     hr = WindowsFirewallAddApp(fwProfile,   
  571.         L"%ProgramFiles%\\Messenger\\msmsgs.exe",  
  572.         L"Windows Messenger");  
  573.     if (FAILED(hr))  
  574.     {  
  575.         printf("WindowsFirewallAddApp failed: 0x%08lx\n", hr);  
  576.         goto error;  
  577.     }  
  578.   
  579.     hr = WindowsFirewallPortAdd(fwProfile, 80,   
  580.         NET_FW_IP_PROTOCOL_TCP, L"WWW");  
  581.   
  582.     if (FAILED(hr))  
  583.     {  
  584.         printf("WindowsFirewallPortAdd failed: 0x%08lx\n", hr);  
  585.         goto error;  
  586.     }  
  587.   
  588. error:  
  589.   
  590.     WindowsFirewallCleanup(fwProfile);  
  591.   
  592.     if (SUCCEEDED(comInit))  
  593.     {  
  594.         CoUninitialize();  
  595.     }  
  596.   
  597.     return 0;  
  598. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值