本文介绍了自动化的Windows防火墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个使用端口777与我的SQL Server通信异步通信和3306端口C#应用程序。当端口被防火墙阻止,就会出现问题。我试图创建一个程序在Windows 7的防火墙列表中添加一个例外 当我运行程序时,我得到的错误如下:灾难性故障(例外来自HRESULT:。0x8000FFFF(E_UNEXPECTED)) 我不明白这些错误的意思是,任何的建议是值得欢迎的,谢谢 受保护的内部空隙AddExceptionToFirewall(){尝试{ INetFwMgr防火墙= NULL; INetFwAuthorizedApplications应用= NULL; INetFwAuthorizedApplication应用= NULL; 型PROGID = NULL; INetFwOpenPorts端口= NULL; INetFwOpenPort asyncPort = NULL; INetFwOpenPort MYSQLPORT = NULL; 布尔appFounded = FALSE; 布尔asyncPortFounded = FALSE; 布尔mysqlPortFounded = FALSE; PROGID = Type.GetTypeFromProgID(HNetCfg.FwMgr); //检查Windows防火墙防火墙=(INetFwMgr)Activator.CreateInstance(PROGID); 如果(fireWall.LocalPolicy.CurrentProfile.FirewallEnabled){ //获得授权的应用程序列表 =应用程序(INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications; IEnumerator的appEnumerate = apps.GetEnumerator(); ,而(appEnumerate.MoveNext()){ =应用程序(INetFwAuthorizedApplication)appEnumerate.Current; 如果(app.Name == Application.ProductName){ appFounded = TRUE; 中断; } } //这个应用程序添加到批准的应用程序列表中,如果(appFounded == FALSE){ app.Name =应用。产品名称; StringBuilder的strBuild =新的StringBuilder(); strBuild.Append(Application.ExecutablePath.Replace(\\,\\\\)); app.ProcessImageFileName = strBuild.ToString(); app.Enabled = TRUE; =应用程序(INetFwAuthorizedApplications)fireWall.LocalPolicy.CurrentProfile.AuthorizedApplications; apps.Add(应用程序); } //获得授权的异步socket端口(777)端口=(INetFwOpenPorts)fireWall.LocalPolicy.CurrentProfile.GloballyOpenPorts清单; IEnumerator的portEnumerate = ports.GetEnumerator(); ,而(portEnumerate.MoveNext()){ asyncPort =(INetFwOpenPort)portEnumerate.Current; 如果(asyncPort.Port == 777){ asyncPortFounded = TRUE; 中断; } } //添加端口777到全球开放的端口如果(asyncPortFounded ==假) ports.Add(asyncPort); //获得MySQL授权插座端口(3306)名单而(portEnumerate.MoveNext()){ MYSQLPORT =(INetFwOpenPort)portEnumerate.Current ; 如果(mysqlPort.Port == 3306){ mysqlPortFounded = TRUE; 中断; } } //一个3306端口添加到全球开放的端口如果(mysqlPortFounded ==假) ports.Add(MYSQLPORT); } } 赶上(收到COMException厘米){ MessageBox.Show(cm.Message); } 赶上(例外前){ MessageBox.Show(ex.Message); } } 解决方案 http://www.codeproject.com/Articles/14906/Open-Windows-Firewall-During - 安装 使用下列程序之前,请加引用 FirewallAPI.dll 到Visual Studio 2010 执行以下操作: 右键单击该项目在解决方案管理器的 Visual Studio 2010中 - 选择添加引用 - 选择 C:\Windows\System32\FirewallAPI.dll - 确定 调用程序防火墙下面3行代码。你可以把这段代码在你的程序的形式负载: 私人clsFirewall objFirewall =新clsFirewall (); objFirewall.CloseFirewall(); objFirewall.OpenFirewall(); / *自动化Windows防火墙C#支持Windows 7 * / 使用系统; System.Collections中使用; 使用System.Collections.Generic; 使用System.Data这;使用System.Diagnostics程序; 使用的System.Threading; 使用NetFwTypeLib;使用System.Windows.Forms的; 命名空间MyFirewall { 公共类clsFirewall { 私人INT [] = portsSocket {777,3306}; 私人的String [] = portsName {AsyncPort,MYSQLPORT}; 私人INetFwProfile fwProfile = NULL; 受保护的内部空隙OpenFirewall(){ INetFwAuthorizedApplications authApps = NULL; INetFwAuthorizedApplication authApp = NULL; INetFwOpenPorts openPorts = NULL; INetFwOpenPort openPort = NULL; 尝试{如果(isAppFound(Application.ProductName +服务器)== FALSE){ SetProfile(); authApps = fwProfile.AuthorizedApplications; authApp =的GetInstance(INetAuthApp)作为INetFwAuthorizedApplication; authApp.Name = Application.ProductName +服务器; authApp.ProcessImageFileName = Application.ExecutablePath; authApps.Add(authApp); } 如果(isPortFound(portsSocket [0])==假){ SetProfile(); openPorts = fwProfile.GloballyOpenPorts; openPort =的GetInstance(INetOpenPort)作为INetFwOpenPort; openPort.Port = portsSocket [0]; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = portsName [0]; openPorts.Add(openPort); } 如果(isPortFound(portsSocket [1])==假){ SetProfile(); openPorts = fwProfile.GloballyOpenPorts; openPort =的GetInstance(INetOpenPort)作为INetFwOpenPort; openPort.Port = portsSocket [1]; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = portsName [1]; openPorts.Add(openPort); } } 赶上(例外前){ MessageBox.Show(ex.Message); } 终于{如果(authApps!= NULL)authApps = NULL; 如果(authApp!= NULL)authApp = NULL; 如果(openPorts!= NULL)openPorts = NULL; 如果(openPort!= NULL)openPort = NULL; } } 受保护的内部空隙CloseFirewall(){ INetFwAuthorizedApplications应用= NULL; INetFwOpenPorts端口= NULL; 尝试{如果(isAppFound(Application.ProductName +服务器)==真){ SetProfile(); =应用fwProfile.AuthorizedApplications; apps.Remove(Application.ExecutablePath); } 如果(isPortFound(portsSocket [0])== TRUE){ SetProfile(); 端口= fwProfile.GloballyOpenPorts; ports.Remove(portsSocket [0],NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); } 如果(isPortFound(portsSocket [1])== TRUE){ SetProfile(); 端口= fwProfile.GloballyOpenPorts; ports.Remove(portsSocket [1],NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); } } 赶上(例外前){ MessageBox.Show(ex.Message); } 终于{如果(应用程序!= NULL)应用程序= NULL; 如果(端口!= NULL)端口= NULL; } } 受保护的内部布尔isAppFound(字符串的appName){布尔boolResult = FALSE; 型PROGID = NULL; INetFwMgr防火墙= NULL; INetFwAuthorizedApplications应用= NULL; INetFwAuthorizedApplication应用= NULL; 尝试{ PROGID = Type.GetTypeFromProgID(HNetCfg.FwMgr); 防火墙= Activator.CreateInstance(PROGID)为INetFwMgr; 如果(firewall.LocalPolicy.CurrentProfile.FirewallEnabled){ =应用firewall.LocalPolicy.CurrentProfile.AuthorizedApplications; IEnumerator的appEnumerate = apps.GetEnumerator(); ,而((appEnumerate.MoveNext())){ =应用作为appEnumerate.Current INetFwAuthorizedApplication; 如果(app.Name ==的appName){ boolResult = TRUE; 中断; } } } } 赶上(例外前){ MessageBox.Show(ex.Message); } 终于{如果(PROGID!= NULL)程序id = NULL; 如果(防火墙!= NULL)防火墙= NULL; 如果(!应用= NULL)应用程式= NULL; 如果(APP!= NULL)应用= NULL; } 返回boolResult; } 受保护的内部布尔isPortFound(INT端口号){布尔boolResult = FALSE; 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; 如果(currentPort.Port ==端口号){ boolResult = TRUE; 中断; } } } 赶上(例外前){ MessageBox.Show(ex.Message); } 终于 {如果(端口!= NULL)端口= NULL; 如果(PROGID!= NULL)程序id = NULL; 如果(防火墙!= NULL)防火墙= NULL; 如果(currentPort!= NULL)currentPort = NULL; } 返回boolResult; } 受保护的内部空隙SetProfile(){ INetFwMgr fwMgr = NULL; INetFwPolicy fwPolicy = NULL; 尝试{ fwMgr =的GetInstance(INetFwMgr)作为INetFwMgr; fwPolicy = fwMgr.LocalPolicy; fwProfile = fwPolicy.CurrentProfile; } 赶上(例外前){ MessageBox.Show(ex.Message); } 终于{如果(fwMgr!= NULL)fwMgr = NULL; 如果(fwPolicy!= NULL)fwPolicy = NULL; } } 受保护的内部对象的GetInstance(字符串的typeName){型tpResult = NULL; 开关(typeName的){案INetFwMgr: tpResult = Type.GetTypeFromCLSID(新的GUID({304CE942-6E39-40D8-943A-B913C40C9CD4})); 返回Activator.CreateInstance(tpResult); 案INetAuthApp: tpResult = Type.GetTypeFromCLSID(新的GUID({EC9846B3-2762-4A6B-A214-6ACB603462D2})); 返回Activator.CreateInstance(tpResult); 案INetOpenPort: tpResult = Type.GetTypeFromCLSID(新的GUID({0CA545C6-37AD-4A6C-BF92-9F7610067EF5})); 返回Activator.CreateInstance(tpResult); 默认:返回NULL; } } } } 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-InstallationBefore 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:\Windows\System32\FirewallAPI.dll - OkCalling 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防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 07:01