2012-02-07 17 views
5

tengo este trozo de código que he encontrado e implementado de acuerdo con http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/windowsfirewall/Delphi 7 Windows Vista/7 Firewall Ubicaciones Excepción red

procedure AddExceptionToFirewall (Caption: String; Executable: String); 
var 
    FirewallMsg: OleVariant; 
    Application: OleVariant; 
    CurrentProfile: OleVariant; 
begin 
    FirewallMsg:= CreateOLEObject ('HNetCfg.FwMgr'); 
    CurrentProfile:= FirewallMsg.LocalPolicy.CurrentProfile; 
    Application:= CreateOLEObject ('HNetCfg.FwAuthorizedApplication'); 
    Application.ProcessImageFileName:= Executable; 
    Application.Name:= Caption; 
    Application.Scope:= FW_SCOPE_ALL; 
    Application.IpVersion:= FW_IP_VERSION_ANY; 
    Application.Enabled:= True; 
    CurrentProfile.AuthorizedApplications.Add (Application); 
end; 

La cosa es, en Windows 7, se añade la excepción sólo como sociedades y no es tan privada como se puede ver un círculo en ROJO aquí

enter image description here

Cuando se pone en público solamente, mi programa tiene problemas para acceder a mi anfitrión a través de un FT P conexión, lo que hace que mi programa sea inútil. Este problema es particular solo para Windows Vista/7; en XP, la configuración actual funciona bien.

Por favor, si tiene alguna pista o consejos útiles, compártalos.

Respuesta

8

Comenzando con Windows Vista, debe utilizar las interfaces INetFwPolicy2 y INetFwRule para obtener acceso a las nuevas características del firewall.

Pruebe esta muestra que agrega una nueva regla en el perfil Público y Privado.

procedure AddExceptionToFirewall(Const Caption, Executable: String); 
const 
NET_FW_PROFILE2_DOMAIN = 1; 
NET_FW_PROFILE2_PRIVATE = 2; 
NET_FW_PROFILE2_PUBLIC = 4; 

NET_FW_IP_PROTOCOL_TCP = 6; 
NET_FW_ACTION_ALLOW = 1; 
var 
    fwPolicy2  : OleVariant; 
    RulesObject : OleVariant; 
    Profile  : Integer; 
    NewRule  : OleVariant; 
begin 
    Profile    := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC; 
    fwPolicy2   := CreateOleObject('HNetCfg.FwPolicy2'); 
    RulesObject   := fwPolicy2.Rules; 
    NewRule    := CreateOleObject('HNetCfg.FWRule'); 
    NewRule.Name  := Caption; 
    NewRule.Description := Caption; 
    NewRule.Applicationname := Executable; 
    NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP; 
    NewRule.Enabled := TRUE; 
    NewRule.Profiles := Profile; 
    NewRule.Action := NET_FW_ACTION_ALLOW; 
    RulesObject.Add(NewRule); 
end; 
+1

¿Por qué el voto a favor? – RRUZ

+0

¡Vaya que funciona bastante bien, incluso hace que las políticas públicas y privadas de Fire Wall sean inamovibles manualmente! Muchas gracias RRUZ! – ziGi

+0

¿y si CreateOleObject devuelve nil? Por cierto, ¿hay una página de MSDN para CreateOleObject con la documentación de lo que puede y no puede devolver? –

Cuestiones relacionadas