public FW_ERROR_CODE AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } bool bEnablePort = true; FW_ERROR_CODE nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only add the port, if it isn't added to the collection if (bEnablePort == false) { // Retrieve the collection of globally open ports INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } // Create an instance of an open port Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); var FWOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort); if (FWOpenPort == null) { return(FW_ERROR_CODE.FW_ERR_CREATE_PORT_INSTANCE); } // Set the port number FWOpenPort.Port = nPortNumber; // Set the IP Protocol FWOpenPort.Protocol = ipProtocol; // Set the registered name FWOpenPort.Name = strRegisterName; try { FWOpenPorts.Add(FWOpenPort); } catch { return(FW_ERROR_CODE.FW_ERR_ADD_TO_COLLECTION); } } else { return(FW_ERROR_CODE.FW_ERR_SAME_PORT_EXIST); } return(FW_ERROR_CODE.FW_NOERROR); }
public FW_ERROR_CODE AddApplication(string strProcessImageFileName, string strRegisterName) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } if (strProcessImageFileName.Length == 0 || strRegisterName.Length == 0) { return(FW_ERROR_CODE.FW_ERR_INVALID_ARG); } // First of all, check the application is already authorized; bool bAppEnable = true; FW_ERROR_CODE nError = IsAppEnabled(strProcessImageFileName, ref bAppEnable); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only add the application if it isn't authorized if (bAppEnable == false) { // Retrieve the authorized application collection INetFwAuthorizedApplications FWApps = m_FirewallProfile.AuthorizedApplications; if (FWApps == null) { return(FW_ERROR_CODE.FW_ERR_AUTH_APPLICATIONS); } // Create an instance of an authorized application Type typeFwApp = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}")); var FWApp = (INetFwAuthorizedApplication)Activator.CreateInstance(typeFwApp); if (FWApp == null) { return(FW_ERROR_CODE.FW_ERR_CREATE_APP_INSTANCE); } // Set the process image file name FWApp.ProcessImageFileName = strProcessImageFileName; FWApp.Name = strRegisterName; try { FWApps.Add(FWApp); } catch { return(FW_ERROR_CODE.FW_ERR_ADD_TO_COLLECTION); } } return(FW_ERROR_CODE.FW_NOERROR); }
public FW_ERROR_CODE RemoveApplication(string strProcessImageFileName) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } if (strProcessImageFileName.Length == 0) { return(FW_ERROR_CODE.FW_ERR_INVALID_ARG); } bool bAppEnable = true; FW_ERROR_CODE nError = IsAppEnabled(strProcessImageFileName, ref bAppEnable); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only remove the application if it is authorized if (bAppEnable) { // Retrieve the authorized application collection INetFwAuthorizedApplications FWApps = m_FirewallProfile.AuthorizedApplications; if (FWApps == null) { return(FW_ERROR_CODE.FW_ERR_AUTH_APPLICATIONS); } try { FWApps.Remove(strProcessImageFileName); } catch { return(FW_ERROR_CODE.FW_ERR_REMOVE_FROM_COLLECTION); } } return(FW_ERROR_CODE.FW_NOERROR); }
public FW_ERROR_CODE RemovePort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } bool bEnablePort = false; FW_ERROR_CODE nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only remove the port, if it is on the collection if (bEnablePort) { // Retrieve the collection of globally open ports INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } try { FWOpenPorts.Remove(nPortNumber, ipProtocol); } catch { return(FW_ERROR_CODE.FW_ERR_REMOVE_FROM_COLLECTION); } } return(FW_ERROR_CODE.FW_NOERROR); }
public FW_ERROR_CODE TurnOffWindowsFirewall() { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } // Check whether the firewall is off bool bFWOn = false; FW_ERROR_CODE ret = IsWindowsFirewallOn(ref bFWOn); if (ret != FW_ERROR_CODE.FW_NOERROR) { return(ret); } // If it is on now, turn it off if (bFWOn) { m_FirewallProfile.FirewallEnabled = false; } return(FW_ERROR_CODE.FW_NOERROR); }