用于测试网络适配器是否受防火墙保护的 C# API

发布于 2024-08-09 12:49:37 字数 95 浏览 3 评论 0原文

鉴于 - .Net 2.0 XP 机器,带 SP2 和 多个网络适配器

是否有 API 可用于检查网络适配器是否受防火墙保护?

华盛顿一个人

Given -
.Net 2.0
XP machine with SP2 and
multiple network adapters

Is there an API that can be used to check if the network adapter is firewalled?

OneGuyInDC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世晨晓 2024-08-16 12:49:37

尝试一下下面的 C# 代码。它适用于 Windows 7(和 Vista)和 XP。
这将获取当前配置文件的状态并启用/禁用 Windows 防火墙,例如:家庭/域/公共访问网络。

用法:

getFirewallStatus() 
  --> returns true/false for whether the windows firewall is enable/disabled.

setFirewallStatus(newStatus) 
  --> sets the firewall enabled/disabled to the true/false value passed in
      eg, to enable the firewall:
         setFirewallStatus(true)

getCurrPolicy()  
  --> used by the other two methods

isWinXP()
  --> returns whether windows version is WinXP/2000 or newer, ie: Vista/Win7
      used by the other methods to determine which code to use.

代码:

using NetFwTypeLib; 
// (don't forget to add it to your references, its under the COM tab)

public bool isWinXP()
{
   OperatingSystem os = Environment.OSVersion;
   int majorVersion = os.Version.Major;
   // see http://msdn.microsoft.com/en-us/library/ms724832(v=vs.85).aspx
   if (majorVersion < 6) // if O/S is not Vista or Windows7
   {
       return true;
   }
   else
   {
       return false;
   }
}
private static INetFwPolicy2 getCurrPolicy()
{
    INetFwPolicy2 fwPolicy2;
    Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
    fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
    return fwPolicy2;
}
public bool getFirewallStatus()
{
    bool result = false;
    switch (isWinXP())
    {
        case true:
            Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
            INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
            result = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
            break;
        case false:
            INetFwPolicy2 fwPolicy2 = getCurrPolicy();
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)fwPolicy2.CurrentProfileTypes;
            result = (fwPolicy2.get_FirewallEnabled(fwCurrentProfileTypes));
            break;
        default:
            result = false; // assume Win7 by default
            break;
    }
    return result;
}
public void setFirewallStatus(bool newStatus)
{
    switch (isWinXP())
    {
        case true:
            Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
            INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
            mgr.LocalPolicy.CurrentProfile.FirewallEnabled = newStatus;
            break;
        case false:
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
            INetFwPolicy2 currPolicy = getCurrPolicy();
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)currPolicy.CurrentProfileTypes;
            currPolicy.set_FirewallEnabled(fwCurrentProfileTypes, newStatus);
            break;
        default:
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes1;
            INetFwPolicy2 currPolicy1 = getCurrPolicy();
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes1 = (NET_FW_PROFILE_TYPE2_)currPolicy1.CurrentProfileTypes;
            currPolicy1.set_FirewallEnabled(fwCurrentProfileTypes1, newStatus);
            break;
    }
}

Give this c# code below a go. It works for Windows 7 (& Vista) and XP.
This will get the status of, and enable/disable the Windows firewall for the current profile, eg: Home/Domain/Public access networks.

Usage:

getFirewallStatus() 
  --> returns true/false for whether the windows firewall is enable/disabled.

setFirewallStatus(newStatus) 
  --> sets the firewall enabled/disabled to the true/false value passed in
      eg, to enable the firewall:
         setFirewallStatus(true)

getCurrPolicy()  
  --> used by the other two methods

isWinXP()
  --> returns whether windows version is WinXP/2000 or newer, ie: Vista/Win7
      used by the other methods to determine which code to use.

Code:

using NetFwTypeLib; 
// (don't forget to add it to your references, its under the COM tab)

public bool isWinXP()
{
   OperatingSystem os = Environment.OSVersion;
   int majorVersion = os.Version.Major;
   // see http://msdn.microsoft.com/en-us/library/ms724832(v=vs.85).aspx
   if (majorVersion < 6) // if O/S is not Vista or Windows7
   {
       return true;
   }
   else
   {
       return false;
   }
}
private static INetFwPolicy2 getCurrPolicy()
{
    INetFwPolicy2 fwPolicy2;
    Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
    fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
    return fwPolicy2;
}
public bool getFirewallStatus()
{
    bool result = false;
    switch (isWinXP())
    {
        case true:
            Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
            INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
            result = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
            break;
        case false:
            INetFwPolicy2 fwPolicy2 = getCurrPolicy();
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)fwPolicy2.CurrentProfileTypes;
            result = (fwPolicy2.get_FirewallEnabled(fwCurrentProfileTypes));
            break;
        default:
            result = false; // assume Win7 by default
            break;
    }
    return result;
}
public void setFirewallStatus(bool newStatus)
{
    switch (isWinXP())
    {
        case true:
            Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
            INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
            mgr.LocalPolicy.CurrentProfile.FirewallEnabled = newStatus;
            break;
        case false:
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes;
            INetFwPolicy2 currPolicy = getCurrPolicy();
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes = (NET_FW_PROFILE_TYPE2_)currPolicy.CurrentProfileTypes;
            currPolicy.set_FirewallEnabled(fwCurrentProfileTypes, newStatus);
            break;
        default:
            NET_FW_PROFILE_TYPE2_ fwCurrentProfileTypes1;
            INetFwPolicy2 currPolicy1 = getCurrPolicy();
            //read Current Profile Types (only to increase Performace)
            //avoids access on CurrentProfileTypes from each Property
            fwCurrentProfileTypes1 = (NET_FW_PROFILE_TYPE2_)currPolicy1.CurrentProfileTypes;
            currPolicy1.set_FirewallEnabled(fwCurrentProfileTypes1, newStatus);
            break;
    }
}
只是在用心讲痛 2024-08-16 12:49:37

由于以下原因,一般情况无法了解(例如,是否存在外部防火墙):

  1. 如果您未收到传入连接,则您的外部接口可能已关闭。
  2. 如果您无法建立传出连接,则您的外部接口可能已关闭。

但是有一个 API 用于查明给定网络接口上是否启用了 Windows 防火墙。您将需要使用 COM 互操作来获取 INetFwProfile(对于全局防火墙状态)和 INetSharingConfiguration(对于特定网络接口)接口,并检查 INetFwProfile.FirewallEnabled 和 INetSharingConfiguration.InternetFirewallEnabled。

请参阅 http://msdn.microsoft.com/en -us/library/aa364717%28VS.85%29.aspx 了解链接以及如何使用这些结果来确定有效的防火墙状态。 (它是用 VBScript 编写的,但应该可以翻译为 C#。)

It is not possible to know in general (e.g. if there is an external firewall) for the following reasons:

  1. If you aren't receiving incoming connections, your external interface may just be down.
  2. If you are unable to make outgoing connections, your external interface may just be down.

But there is an API for finding out if the Windows Firewall is enabled on a given network interface. You will need to use COM interop to get the INetFwProfile (for global firewall status) and INetSharingConfiguration (for a specific network interface) interfaces, and check INetFwProfile.FirewallEnabled and INetSharingConfiguration.InternetFirewallEnabled.

See http://msdn.microsoft.com/en-us/library/aa364717%28VS.85%29.aspx for links and for how to use these results to determine the effective firewall status. (It's written in terms of VBScript but should be translatable to C#.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文