问题描述
我有 C# 应用程序,它使用 PORT 777 进行异步通信,使用 PORT 3306 与 My Sql Server 进行通信.当端口被防火墙阻止时,就会出现问题.我尝试创建一个程序,在 Windows 7 的防火墙列表中添加例外.
当我运行程序时,出现如下错误:灾难性故障(HRESULT 异常:0x8000FFFF (E_UNEXPECTED))".
我不明白这些错误是什么意思,欢迎提出任何建议,谢谢.
受保护的内部 void AddExceptionToFirewall(){尝试 {INetFwMgr 防火墙 = null;INetFwAuthorizedApplications 应用程序 = null;INetFwAuthorizedApplication app = null;类型 progID = null;INetFwOpenPorts 端口 = null;INetFwOpenPort asyncPort = null;INetFwOpenPort mysqlPort = null;bool appFounded = false;bool asyncPortFounded = false;布尔 mysqlPortFounded = false;progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");//检查 Windows 防火墙fireWall = (INetFwMgr)Activator.CreateInstance(progID);如果(fireWall.LocalPolicy.CurrentProfile.FirewallEnabled){//获取授权应用列表应用程序 = (INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications;IEnumerator appEnumerate = apps.GetEnumerator();而(appEnumerate.MoveNext()){app = (INetFwAuthorizedApplication)appEnumerate.Current;if (app.Name == Application.ProductName){appFounded =真;休息;}}//将此应用程序添加到授权应用程序列表中如果(应用创建==假){app.Name = Application.ProductName;StringBuilder strBuild = new StringBuilder();strBuild.Append(Application.ExecutablePath.Replace("\","\\"));app.ProcessImageFileName = strBuild.ToString();应用程序启用=真;应用程序 = (INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications;应用程序.添加(应用程序);}//获取授权的异步套接字端口列表(777)端口 = (INetFwOpenPorts)fireWall.LocalPolicy.CurrentProfile.GloballyOpenPorts;IEnumerator portEnumerate = ports.GetEnumerator();而 (portEnumerate.MoveNext()) {asyncPort = (INetFwOpenPort)portEnumerate.Current;if (asyncPort.Port == 777) {asyncPortFounded = true;休息;}}//添加一个 777 端口到全局开放的端口if (asyncPortFounded==false)端口.添加(异步端口);//获取授权的mysql socket端口列表(3306)而 (portEnumerate.MoveNext()) {mysqlPort = (INetFwOpenPort)portEnumerate.Current;如果(mysqlPort.Port == 3306){mysqlPortFounded = true;休息;}}//添加一个 3306 端口到全局开放的端口如果(mysqlPortFounded == false)端口.添加(mysqlPort);}}捕捉(COMException cm){MessageBox.Show(cm.Message);}捕捉(异常前){MessageBox.Show(ex.Message);}}
http:///www.codeproject.com/Articles/14906/Open-Windows-Firewall-During-Installation
在使用以下程序之前,请在 Visual Studio 2010 中添加参考 FirewallAPI.dll.请执行下列操作:从 Visual Studio 2010 的 Solution Explorer 右键单击项目 - 选择 Add Reference - 选择 C:WindowsSystem32FirewallAPI.dll - 好的
使用下面的 3 行代码调用程序防火墙.您可以将此代码放入程序的表单加载中:
<块引用>private clsFirewall objFirewall = new clsFirewall();objFirewall.CloseFirewall();objFirewall.OpenFirewall();
/* 使用 C# 支持 Windows 7 的自动化 Windows 防火墙 */
使用系统;使用 System.Collections;使用 System.Collections.Generic;使用 System.Data;使用 System.Diagnostics;使用 System.Threading;使用 NetFwTypeLib;使用 System.Windows.Forms;命名空间我的防火墙 {公共类 clsFirewall {私有 int[] 端口套接字 = { 777, 3306 };私有字符串[] portsName = { "AsyncPort", "MySqlPort" };私有 INetFwProfile fwProfile = null;受保护的内部无效 OpenFirewall() {INetFwAuthorizedApplications authApps = null;INetFwAuthorizedApplication authApp = null;INetFwOpenPorts openPorts = null;INetFwOpenPort openPort = null;尝试 {if (isAppFound(Application.ProductName + "Server") == false) {设置配置文件();authApps = fwProfile.AuthorizedApplications;authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication;authApp.Name = Application.ProductName + "服务器";authApp.ProcessImageFileName = Application.ExecutablePath;authApps.Add(authApp);}if (isPortFound(portsSocket[0]) == false) {设置配置文件();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 = 端口名称[0];openPorts.Add(openPort);}if (isPortFound(portsSocket[1]) == false) {设置配置文件();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 = 端口名称[1];openPorts.Add(openPort);}}捕捉(异常前){MessageBox.Show(ex.Message);}最后 {if (authApps != null) authApps = null;if (authApp != null) authApp = null;if (openPorts != null) openPorts = null;if (openPort != null) openPort = null;}}受保护的内部无效 CloseFirewall() {INetFwAuthorizedApplications 应用程序 = null;INetFwOpenPorts 端口 = null;尝试 {if (isAppFound(Application.ProductName + "Server") == true) {设置配置文件();应用程序 = fwProfile.AuthorizedApplications;apps.Remove(Application.ExecutablePath);}if (isPortFound(portsSocket[0]) == true) {设置配置文件();端口 = fwProfile.GloballyOpenPorts;ports.Remove(portsSocket[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);}if (isPortFound(portsSocket[1]) == true) {设置配置文件();端口 = fwProfile.GloballyOpenPorts;ports.Remove(portsSocket[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);}}捕捉(异常前){MessageBox.Show(ex.Message);}最后 {如果(应用程序!= null)应用程序= null;如果(端口!= null)端口= null;}}受保护的内部 bool isAppFound(string appName) {布尔布尔结果=假;类型 progID = null;INetFwMgr 防火墙 = null;INetFwAuthorizedApplications 应用程序 = null;INetFwAuthorizedApplication app = null;尝试 {progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");防火墙 = Activator.CreateInstance(progID) 作为 INetFwMgr;如果(firewall.LocalPolicy.CurrentProfile.FirewallEnabled){应用程序 = 防火墙.LocalPolicy.CurrentProfile.AuthorizedApplications;IEnumerator appEnumerate = apps.GetEnumerator();而 ((appEnumerate.MoveNext())) {app = appEnumerate.Current as INetFwAuthorizedApplication;if (app.Name == appName) {布尔结果 = 真;休息;}}}}捕捉(异常前){MessageBox.Show(ex.Message);}最后 {if (progID != null) progID = null;如果(防火墙!= null)防火墙= null;如果(应用程序!= null)应用程序= null;如果(应用程序!= null)应用程序= null;}返回布尔结果;}受保护的内部 bool isPortFound(int portNumber) {布尔布尔结果=假;INetFwOpenPorts 端口 = null;类型 progID = null;INetFwMgr 防火墙 = null;INetFwOpenPort currentPort = null;尝试 {progID = Type.GetTypeFromProgID("HNetCfg.FwMgr");防火墙 = Activator.CreateInstance(progID) 作为 INetFwMgr;端口 = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts;IEnumerator portEnumerate = ports.GetEnumerator();而((portEnumerate.MoveNext())){currentPort = portEnumerate.Current 作为 INetFwOpenPort;if (currentPort.Port == portNumber) {布尔结果 = 真;休息;}}}捕捉(异常前){MessageBox.Show(ex.Message);}最后{如果(端口!= null)端口= null;if (progID != null) progID = null;如果(防火墙!= null)防火墙= null;if (currentPort != null) currentPort = null;}返回布尔结果;}受保护的内部无效 SetProfile() {INetFwMgr fwMgr = null;INetFwPolicy fwPolicy = null;尝试 {fwMgr = GetInstance("INetFwMgr") as INetFwMgr;fwPolicy = fwMgr.LocalPolicy;fwProfile = fwPolicy.CurrentProfile;}捕捉(异常前){MessageBox.Show(ex.Message);}最后 {如果 (fwMgr != null) fwMgr = null;if (fwPolicy != null) fwPolicy = null;}}受保护的内部对象 GetInstance(string typeName) {类型 tpResult = null;开关(类型名称){案例INetFwMgr":tpResult = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));返回 Activator.CreateInstance(tpResult);案例INetAuthApp":tpResult = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));返回 Activator.CreateInstance(tpResult);案例INetOpenPort":tpResult = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}"));返回 Activator.CreateInstance(tpResult);默认:返回空值;}}}}
I have C# Application that uses PORT 777 for asynchronous communication and PORT 3306 for communication with My Sql Server. Problems arise when the ports is blocked by a firewall. I tried to create a program to add an exception in the firewall list of Windows 7.
When i run the program, i get bug as follow: "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))".
I do not understand what these errors mean, any suggestion is welcome, Thanks.
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
Before using the program below, please add reference FirewallAPI.dll to Visual Studio 2010. Do the following: Right-click the Project from the Solution Explorer of Visual Studio 2010 - Select the Add Reference - Select C:WindowsSystem32FirewallAPI.dll - Ok
Calling program firewall with 3 line code below. You can put this code in the form load of your program:
private clsFirewall objFirewall = new clsFirewall(); objFirewall.CloseFirewall(); objFirewall.OpenFirewall();
/* Automating Windows Firewall with C# Support 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;
}
}
}
}
这篇关于自动化 Windows 防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!