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:\Windows\System32\FirewallAPI.dll - Ok
Calling program firewall with 3 line code below. You can put this code in the form load of your program:
/* Automating Windows Firewall with C# Support Windows 7 */
I came here via Google looking for a .net method of listing the Windows Firewall open ports. The answers described above did not work. Specifically the firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts always had a count of zero. These answers are old and may well apply to Windows 7. What worked for me on Windows 10 was this code.
I did not need to open or close Ports but it can be done by changing rule status, or creating new rules and enabling them. Hope this saves someone else a few hours.
The accepted answer by Javanese Girl had a C-like implementation with a declare-use-free pattern, and redundant code. Here is a refactored implementation based on that answer. It uses all the same calls into FirewallAPI.dll; it is functionally equivalent, so it solves the original question's problem of not being able to get these sorts of firewall operations to work. This solution has a simple interface that hides all the details: IsPortOpen, OpenPort, and ClosePort.
Example Usage:
Implementation: