- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 C# 应用程序使用端口 777 进行异步通信,使用端口 3306 与 My Sql Server 进行通信。当端口被防火墙阻止时,就会出现问题。我试图创建一个程序来在 Windows 7 的防火墙列表中添加一个异常(exception)。
当我运行该程序时,出现如下错误:“灾难性故障(HRESULT 异常:0x8000FFFF (E_UNEXPECTED))”。
我不明白这些错误是什么意思,欢迎任何建议,谢谢。
protected internal void AddExceptionToFirewall(){
try {
INetFwMgr fireWall = null;
INetFwAuthorizedApplications apps = null;
INetFwAuthorizedApplication app = null;
Type progID = null;
INetFwOpenPorts ports = null;
INetFwOpenPort asyncPort = null;
INetFwOpenPort mysqlPort = null;
bool appFounded = false;
bool asyncPortFounded = false;
bool mysqlPortFounded = false;
progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");
// checking for Windows Firewall
fireWall = (INetFwMgr)Activator.CreateInstance(progID);
if (fireWall.LocalPolicy.CurrentProfile.FirewallEnabled) {
// obtain the list of authorized applications
apps = (INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications;
IEnumerator appEnumerate = apps.GetEnumerator();
while (appEnumerate.MoveNext()){
app = (INetFwAuthorizedApplication)appEnumerate.Current;
if (app.Name == Application.ProductName){
appFounded = true;
break;
}
}
// add this application to the list of authorized applications
if(appFounded==false){
app.Name = Application.ProductName;
StringBuilder strBuild = new StringBuilder();
strBuild.Append(Application.ExecutablePath.Replace("\\","\\\\"));
app.ProcessImageFileName = strBuild.ToString();
app.Enabled = true;
apps = (INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications;
apps.Add(app);
}
// obtain the list of authorized asynchronous socket ports (777)
ports = (INetFwOpenPorts)fireWall.LocalPolicy.CurrentProfile.GloballyOpenPorts;
IEnumerator portEnumerate = ports.GetEnumerator();
while (portEnumerate.MoveNext()) {
asyncPort = (INetFwOpenPort)portEnumerate.Current;
if (asyncPort.Port == 777) {
asyncPortFounded = true;
break;
}
}
// add a port 777 to globally open ports
if (asyncPortFounded==false)
ports.Add(asyncPort);
// obtain the list of authorized mysql socket ports(3306)
while (portEnumerate.MoveNext()) {
mysqlPort = (INetFwOpenPort)portEnumerate.Current;
if (mysqlPort.Port == 3306) {
mysqlPortFounded = true;
break;
}
}
// add a port 3306 to globally open ports
if (mysqlPortFounded == false)
ports.Add(mysqlPort);
}
}
catch (COMException cm) {
MessageBox.Show(cm.Message);
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
最佳答案
http://www.codeproject.com/Articles/14906/Open-Windows-Firewall-During-Installation
在使用下面的程序之前,请将引用 FirewallAPI.dll 添加到 Visual Studio 2010。请执行下列操作:在 Visual Studio 2010 的解决方案资源管理器中右键单击项目 - 选择添加引用 - 选择C:\Windows\System32\FirewallAPI.dll - 好的
使用以下 3 行代码调用程序防火墙。您可以将此代码放入程序的表单加载中:
private clsFirewall objFirewall = new clsFirewall();
objFirewall.CloseFirewall();
objFirewall.OpenFirewall();
/* 使用 C# 自动化 Windows 防火墙支持 Windows 7 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Threading;
using NetFwTypeLib;
using System.Windows.Forms;
namespace MyFirewall {
public class clsFirewall {
private int[] portsSocket = { 777, 3306 };
private string[] portsName = { "AsyncPort", "MySqlPort" };
private INetFwProfile fwProfile = null;
protected internal void OpenFirewall() {
INetFwAuthorizedApplications authApps = null;
INetFwAuthorizedApplication authApp = null;
INetFwOpenPorts openPorts = null;
INetFwOpenPort openPort = null;
try {
if (isAppFound(Application.ProductName + " Server") == false) {
SetProfile();
authApps = fwProfile.AuthorizedApplications;
authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication;
authApp.Name = Application.ProductName + " Server";
authApp.ProcessImageFileName = Application.ExecutablePath;
authApps.Add(authApp);
}
if (isPortFound(portsSocket[0]) == false) {
SetProfile();
openPorts = fwProfile.GloballyOpenPorts;
openPort = GetInstance("INetOpenPort") as INetFwOpenPort;
openPort.Port = portsSocket[0];
openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
openPort.Name = portsName[0];
openPorts.Add(openPort);
}
if (isPortFound(portsSocket[1]) == false) {
SetProfile();
openPorts = fwProfile.GloballyOpenPorts;
openPort = GetInstance("INetOpenPort") as INetFwOpenPort;
openPort.Port = portsSocket[1];
openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
openPort.Name = portsName[1];
openPorts.Add(openPort);
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
if (authApps != null) authApps = null;
if (authApp != null) authApp = null;
if (openPorts != null) openPorts = null;
if (openPort != null) openPort = null;
}
}
protected internal void CloseFirewall() {
INetFwAuthorizedApplications apps = null;
INetFwOpenPorts ports = null;
try {
if (isAppFound(Application.ProductName + " Server") == true) {
SetProfile();
apps = fwProfile.AuthorizedApplications;
apps.Remove(Application.ExecutablePath);
}
if (isPortFound(portsSocket[0]) == true) {
SetProfile();
ports = fwProfile.GloballyOpenPorts;
ports.Remove(portsSocket[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
}
if (isPortFound(portsSocket[1]) == true) {
SetProfile();
ports = fwProfile.GloballyOpenPorts;
ports.Remove(portsSocket[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
if (apps != null) apps = null;
if (ports != null) ports = null;
}
}
protected internal bool isAppFound(string appName) {
bool boolResult = false;
Type progID = null;
INetFwMgr firewall = null;
INetFwAuthorizedApplications apps = null;
INetFwAuthorizedApplication app = null;
try {
progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");
firewall = Activator.CreateInstance(progID) as INetFwMgr;
if (firewall.LocalPolicy.CurrentProfile.FirewallEnabled) {
apps = firewall.LocalPolicy.CurrentProfile.AuthorizedApplications;
IEnumerator appEnumerate = apps.GetEnumerator();
while ((appEnumerate.MoveNext())) {
app = appEnumerate.Current as INetFwAuthorizedApplication;
if (app.Name == appName) {
boolResult = true;
break;
}
}
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
if (progID != null) progID = null;
if (firewall != null) firewall = null;
if (apps != null) apps = null;
if (app != null) app = null;
}
return boolResult;
}
protected internal bool isPortFound(int portNumber) {
bool boolResult = false;
INetFwOpenPorts ports = null;
Type progID = null;
INetFwMgr firewall = null;
INetFwOpenPort currentPort = null;
try {
progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");
firewall = Activator.CreateInstance(progID) as INetFwMgr;
ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts;
IEnumerator portEnumerate = ports.GetEnumerator();
while ((portEnumerate.MoveNext())) {
currentPort = portEnumerate.Current as INetFwOpenPort;
if (currentPort.Port == portNumber) {
boolResult = true;
break;
}
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally
{
if (ports != null) ports = null;
if (progID != null) progID = null;
if (firewall != null) firewall = null;
if (currentPort != null) currentPort = null;
}
return boolResult;
}
protected internal void SetProfile() {
INetFwMgr fwMgr = null;
INetFwPolicy fwPolicy = null;
try {
fwMgr = GetInstance("INetFwMgr") as INetFwMgr;
fwPolicy = fwMgr.LocalPolicy;
fwProfile = fwPolicy.CurrentProfile;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
finally {
if (fwMgr != null) fwMgr = null;
if (fwPolicy != null) fwPolicy = null;
}
}
protected internal object GetInstance(string typeName) {
Type tpResult = null;
switch (typeName) {
case "INetFwMgr":
tpResult = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
return Activator.CreateInstance(tpResult);
case "INetAuthApp":
tpResult = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));
return Activator.CreateInstance(tpResult);
case "INetOpenPort":
tpResult = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}"));
return Activator.CreateInstance(tpResult);
default:
return null;
}
}
}
}
关于c# - 自动化 Windows 防火墙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8889587/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6年前关闭。 Improve t
我正在尝试部署启用了策略的 Azure 防火墙高级版,并且需要添加网络规则集合负载。 我在参数文件中有规则集合,需要循环遍历每个规则集合并将其添加到策略中。 "FirewallSettings": {
无论如何,我对 symfony 都不陌生,但我一直使用 FOSUserBundle,默认情况下,它会阻止用户使用 2 个不同的登录表单来验证两种不同的用户类型。 我有两个实体,一个是Admins,另一
如果防火墙内的客户端通过 TCP 连接到公共(public)服务器,一旦连接形成,防火墙就允许双向通信。这是我们在日常使用中通常看到的。我的问题是:这也适用于 UDP 吗? 由于UDP没有连接,我将稍
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我目前正在为一家公司编写 windows-firewallmanager。 问题是,我只能通过名称而不是 INetFwRule 对象或其他方式删除条目。 有些条目具有两次相同的名称,其中一个用于 TC
我有一个防火墙实现,我想记录在机器上访问过的所有网站。因此,当用户在浏览器(任何浏览器)中输入地址或单击链接时,能够记录访问的地址。 问题是我只想记录访问过的地址,而不是页面请求的其他资源(广告、if
刚好手工装完一台CentOS6.4,就顺便记录一下。 复制代码 代码如下: # sestatus SELinux status:  
我是 Windows 防火墙的新手。我看过这个对话,但无法理解它的含义。 上面的对话框说 “允许 python 在这些网络上通信:” .然后它提供了两个复选框,一个用于每个专用网络,一个用于公共(pu
在我看来,ClickOnce 应用程序无法与 Windows Firewall 顺利运行。 . 我已经成功使用 ClickOnce 部署两年了。现在企业已升级到Windows 7,这遇到了障碍。每个用
我的 C# 应用程序使用端口 777 进行异步通信,使用端口 3306 与 My Sql Server 进行通信。当端口被防火墙阻止时,就会出现问题。我试图创建一个程序来在 Windows 7 的防火
我正在开始研究智能防火墙,但在此之前,我需要清楚地了解非常基本的防火墙如何工作。由于我和我的团队最擅长 C 语言,我们将使用它。 有人可以举一个用 C 语言编写的非常基本的防火墙的例子吗? 最佳答案
我正在尝试以编程方式创建出站 Windows 防火墙规则。此外,我想以编程方式启用和禁用此规则。我怎样才能在 C# 中执行此操作?手动,我可以通过进入控制面板,单击 Windows 防火墙,然后单击高
有人用过android intent firewall吗? http://www.cis.syr.edu/~wedu/android/IntentFirewall/ 我用 genymotion 模拟器
我编写了一个简单的 Java 应用程序,它使用套接字与自身的多个实例进行交互。第一个实例自动承担服务器的角色,监听特定端口,所有后续实例都连接到它。 我遇到的问题是 Windows 防火墙弹出询问我是
我有一个通过 ClickOnce 安装和更新的应用程序。该应用程序通过FTP下载文件,因此需要添加为windows防火墙的异常(exception)。由于 ClickOnce 的工作方式,EXE 的路
我很难在 C++ 中找到与 Windows 防火墙相关的任何内容(关于如何将应用程序添加到除列表之外、关闭端口或禁用对某些应用程序的 Internet 访问)。我发现甚至 MS 网站都显示了 Visu
我有一些使用 docker-compose up 运行的项目。该项目在端口 4200、后端 3000 和 db 5342 上有前端。当我使用 docker-compose 运行它时,所有这些端口都是公
用python编写的程序,它选择可用于TCP和UDP通信的随机端口。如果我通过运行 sudo ufw enable 启用 Linux 防火墙(ufw) .我们可以通过给出允许任何端口 sudo ufw
使用命令提示符修改 Windows 防火墙 我可以允许域/公共(public)/私有(private)复选标记全部出现在同一行中吗(就像我可以手动单击一样)? 到目前为止,我有 3 个条目,每种类型有
我是一名优秀的程序员,十分优秀!