VB編寫的程序加入防火牆的例外中

 在工程中要先引入:測試

NetCon 1.0 Type Libraryspa

NetFwTypeLib對象

 

Vb代碼 
  1. Option Explicit  
  2. Const NET_FW_SCOPE_ALL = 0  
  3. Const NET_FW_SCOPE_LOCAL_SUBNET = 1  
  4. Const NET_FW_IP_VERSION_ANY = 2  
  5.   
  6. '獲取Windows防火牆的當前狀態  
  7. Public Function FirewallStatus() As Boolean  
  8.     Dim fwMgr As INetFwMgr  
  9.     Dim oProfile As INetFwProfile  
  10.     On Error GoTo errHandler  
  11.     '聲明Windows防火牆配置管理接口對象  
  12.     Set fwMgr = CreateObject("HNetCfg.FwMgr")  
  13.     '獲取本地防火牆當前的配置對象  
  14.     Set oProfile = fwMgr.LocalPolicy.CurrentProfile  
  15.     '獲取防火牆的狀態,Ture表示啓用,False表示禁用  
  16.     FirewallStatus = oProfile.FirewallEnabled  
  17.     Set oProfile = Nothing  
  18.     Set fwMgr = Nothing  
  19.     Exit Function  
  20. errHandler:  
  21.     FirewallStatus = False  
  22.     MsgBox ("Error: & Err.Description")  
  23.     Err.Clear  
  24. End Function  
  25.   
  26. '切換Windows防火牆的狀態  
  27. Public Sub SwitchFirewall()  
  28.     Dim fwMgr As INetFwMgr  
  29.     Dim oProfile As INetFwProfile  
  30.     On Error GoTo errHandler  
  31.     '聲明Windows防火牆配置管理接口對象  
  32.     Set fwMgr = CreateObject("HNetCfg.FwMgr")  
  33.     '獲取本地防火牆當前的配置對象  
  34.     Set oProfile = fwMgr.LocalPolicy.CurrentProfile  
  35.     '根據當前的防火牆狀態相應地調整啓用與禁用狀態  
  36.     oProfile.FirewallEnabled = Not (oProfile.FirewallEnabled)  
  37.     Set oProfile = Nothing  
  38.     Set fwMgr = Nothing  
  39.     Exit Sub  
  40. errHandler:  
  41.     MsgBox (Err.Description)  
  42.     Err.Clear  
  43. End Sub  
  44.   
  45. '將當前應用程序添加到Windows防火牆例外列表  
  46. Public Sub AddApplicationRule()  
  47.     Dim fwMgr As INetFwMgr  
  48.     Dim oProfile As INetFwProfile  
  49.     On Error GoTo errHandler  
  50.     '聲明Windows防火牆配置管理接口對象  
  51.     Set fwMgr = CreateObject("HNetCfg.FwMgr")  
  52.     '獲取本地防火牆當前的配置對象  
  53.     Set oProfile = fwMgr.LocalPolicy.CurrentProfile  
  54.     Dim oApplication As INetFwAuthorizedApplication  
  55.     '聲明認證程序對象  
  56.     Set oApplication = CreateObject("HNetCfg.FwAuthorizedApplication")  
  57.     '設置認證程序對象的相關屬性  
  58.     With oApplication  
  59.         '應用程序的完整路徑  
  60.         .ProcessImageFileName = App.Path & "\" & App.EXEName & ".exe"  
  61.         '應用程序的名稱,也就是在Windows防火牆例外列表中顯示的名稱  
  62.         .Name = "測試例子"  
  63.         '定義本規則做用的範圍  
  64.         .Scope = NET_FW_SCOPE_ALL  
  65.         '定義本規則用戶的IP協議版本  
  66.         .IpVersion = NET_FW_IP_VERSION_ANY  
  67.         '表示啓用當前規則  
  68.         .Enabled = True  
  69.     End With  
  70.     '將建立的認證程序對象添加到本地防火牆策略的認證程序集合  
  71.     oProfile.AuthorizedApplications.Add oApplication  
  72.     Set oApplication = Nothing  
  73.     Set oProfile = Nothing  
  74.     Set fwMgr = Nothing  
  75.     MsgBox ("添加成功!")  
  76.     Exit Sub  
  77. errHandler:  
  78.     MsgBox (Err.Description)  
  79.     Err.Clear  
  80. End Sub  
  81.   
  82. Private Sub Command1_Click()  
  83.     SwitchFirewall  
  84.     Label1.Caption = FirewallStatus  
  85. End Sub  
  86.   
  87. Private Sub Command3_Click()  
  88. AddApplicationRule  
  89. Label1.Caption = FirewallStatus  
  90. End Sub  
相關文章
相關標籤/搜索