Automatizando el Firewall de Windows con

Tengo una aplicación C # que usa el PORT 777 para la comunicación asincrónica y el PORT 3306 para la comunicación con My Sql Server. Surgen problemas cuando un firewall bloquea los puertos. Intenté crear un programa para agregar una excepción en la lista de firewall de Windows 7.

Cuando ejecuto el programa, aparece el error de la siguiente manera: "Fallo catastrófico (Excepción de HRESULT: 0x8000FFFF (E_UNEXPECTED))".

No entiendo lo que significan estos errores, cualquier sugerencia es bienvenida, gracias.

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);
    }
}

Respuestas a la pregunta(6)

Su respuesta a la pregunta