2011-04-20 17 views
13

¿Cómo se agrega programáticamente una aplicación o puerto al Firewall de Windows en Windows XP?Regla de firewall de Windows para XP

+0

posible duplicado de [Programatically Add Exception to windows vista firewall.] (Http://stackoverflow.com/questions/1409896/programatically-add-exception-to-windows-vista-firewall) –

+0

posible duplicado de [Agregar a Lista de excepciones de firewall] (http://stackoverflow.com/questions/2384718/add-to-firewall-exception-list) –

+2

Esta respuesta solo funciona en xp. título editado desde que OP aceptó esto y su información útil, por lo tanto, no es un engaño ya que el duplicado solo funciona en win7 y vista. –

Respuesta

17

Prueba este código extraído de nuestra fuente abierta SQlite3UI.pas unidad:

function GetXPFirewall(var fwMgr, profile: OleVariant): boolean; 
begin 
    Result := (Win32Platform=VER_PLATFORM_WIN32_NT) and 
    (Win32MajorVersion>5) or ((Win32MajorVersion=5) and (Win32MinorVersion>0)); 
    if result then // need Windows XP at least 
    try 
    fwMgr := CreateOleObject('HNetCfg.FwMgr'); 
    profile := fwMgr.LocalPolicy.CurrentProfile; 
    except 
    on E: Exception do 
     result := false; 
    end; 
end; 

const 
    NET_FW_PROFILE_DOMAIN = 0; 
    NET_FW_PROFILE_STANDARD = 1; 
    NET_FW_IP_VERSION_ANY = 2; 
    NET_FW_IP_PROTOCOL_UDP = 17; 
    NET_FW_IP_PROTOCOL_TCP = 6; 
    NET_FW_SCOPE_ALL = 0; 
    NET_FW_SCOPE_LOCAL_SUBNET = 1; 

procedure AddApplicationToXPFirewall(const EntryName, ApplicationPathAndExe: string); 
var fwMgr, profile, app: OleVariant; 
begin 
    if GetXPFirewall(fwMgr,profile) then 
    try 
    if profile.FirewallEnabled then begin 
     app := CreateOLEObject('HNetCfg.FwAuthorizedApplication'); 
     try 
     app.ProcessImageFileName := ApplicationPathAndExe; 
     app.Name := EntryName; 
     app.Scope := NET_FW_SCOPE_ALL; 
     app.IpVersion := NET_FW_IP_VERSION_ANY; 
     app.Enabled :=true; 
     profile.AuthorizedApplications.Add(app); 
     finally 
     app := varNull; 
     end; 
    end; 
    finally 
    profile := varNull; 
    fwMgr := varNull; 
    end; 
end; 

procedure AddPortToXPFirewall(const EntryName: string; PortNumber: cardinal); 
var fwMgr, profile, port: OleVariant; 
begin 
    if GetXPFirewall(fwMgr,profile) then 
    try 
    if profile.FirewallEnabled then begin 
     port := CreateOLEObject('HNetCfg.FWOpenPort'); 
     port.Name := EntryName; 
     port.Protocol := NET_FW_IP_PROTOCOL_TCP; 
     port.Port := PortNumber; 
     port.Scope := NET_FW_SCOPE_ALL; 
     port.Enabled := true; 
     profile.GloballyOpenPorts.Add(port); 
    end; 
    finally 
    port := varNull; 
    profile := varNull; 
    fwMgr := varNull; 
    end; 
end; 

Se le permitirá a agregue una aplicación o un puerto al firewall de XP. Debería funcionar desde Delphi 6 hasta XE.

+1

He actualizado la fuente de la unidad para trabajar en XP, Vista y Seven, ya sea para una aplicación, ya sea para un puerto. Ver http://synopse.info/forum/viewtopic.php?pid=4652#p4652 –

6

Scripting el Firewall de Windows es posible, ver Scripting the Windows Firewall

y el código de ejemplos por ejemplo here

+0

¡Utilizo Delphi 7! –

+0

Delphi 7 admite secuencias de comandos basadas en COM – mjn

+0

En este caso, debe intentar importar la biblioteca de tipos, consulte mi enlace, menciona que el archivo DLL de la biblioteca de tipos 'normalmente se encuentra en" C: \ Windows \ System32 \ hnetcfg.dll "' (Este artículo es sobre XP, lo revisé en Windows 7 y hay un archivo con este nombre) – mjn

Cuestiones relacionadas