[英]Programmatically add an application to all profile Windows Firewall (Vista+)
I have searched around and there are similar questions on SO, however, no one talks about how to add exception to "All Profile" (windows 7, AKA "Any Profile" on Vista/Windows Server 2008). 我已经搜索过,并且在SO上有类似的问题,但是,没有人谈论如何在“All Profile”(Windows 7,AKA“任何配置文件”在Vista / Windows Server 2008上)添加例外。 Examples on internet talk about add to current profile only.
互联网上的例子谈论仅添加到当前配置文件。
The reason for this is I have a problem with one of my virtual machine: windows 2008 x86, current firewall profile is Domain, and my application is added to Exception list of Domain. 原因是我的虚拟机有一个问题:windows 2008 x86,当前的防火墙配置文件是Domain,我的应用程序被添加到Domain的Exception列表中。 (Firewall setting is as default: block any inbound calls that are not in exception list.) However, inbound calls are still blocked unless : 1. turn off firewall on that this virtual machine.
(防火墙设置为默认设置:阻止任何不在例外列表中的入站呼叫。)但是,入站呼叫仍会被阻止,除非:1。关闭此虚拟机上的防火墙。 2. manually change rule profile of my application to "any"
2.手动将我的应用程序的规则配置文件更改为“任何”
It is very confusing as I thought only active profile should be "active" and should be functional, no matter other profiles are blocking my application inbound calls. 这是非常令人困惑的,因为我认为只有活动的配置文件应该是“活动的”并且应该是有效的,无论其他配置文件阻止我的应用程序入站呼叫。
I am using XPSP2 INetFwMgr interface to add exceptions which is lacking of "any" profile support. 我正在使用XPSP2 INetFwMgr接口来添加缺少“任何”配置文件支持的异常。
I am using c# but any language with example will be appreciated. 我正在使用c#,但任何语言都会受到赞赏。
You may try something like this: 你可以尝试这样的事情:
using System;
using NetFwTypeLib;
namespace FirewallManager
{
class Program
{
static void Main(string[] args)
{
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
firewallRule.Description = "Allow notepad";
firewallRule.ApplicationName = @"C:\Windows\notepad.exe";
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.Name = "Notepad";
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
}
}
}
For sake of completeness, add reference to c:\\Windows\\System32\\FirewallAPI.dll 为了完整起见,请添加对c:\\ Windows \\ System32 \\ FirewallAPI.dll的引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.