2011-04-20 43 views
13

Cách thêm ứng dụng hoặc cổng vào chương trình tường lửa của Windows trên Windows XP theo cách lập trình?Quy tắc tường lửa của Windows cho XP

+0

bản sao có thể có của [Lập trình Thêm ngoại lệ cho tường lửa cửa sổ vista.] (Http://stackoverflow.com/questions/1409896/programatically-add-exception-to-windows-vista-firewall) –

+0

bản sao có thể có của [Thêm vào Danh sách ngoại lệ tường lửa] (http://stackoverflow.com/questions/2384718/add-to-firewall-exception-list) –

+2

Câu trả lời này chỉ hoạt động trong xp. chỉnh sửa tiêu đề kể từ khi OP chấp nhận điều này và thông tin hữu ích của nó, do đó không phải là một bản dupe vì bản sao chỉ hoạt động trong win7 và vista. –

Trả lời

17

Hãy thử mã này được chiết xuất từ ​​nguồn mở của chúng tôi SQlite3UI.pas đơn vị:

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; 

Nó sẽ cho phép bạn thêm một ứng dụng hoặc một cổng vào tường lửa XP. Nên làm việc từ Delphi 6 lên đến XE.

+1

Tôi đã cập nhật nguồn của thiết bị để làm việc trên XP, Vista và Seven, cho một ứng dụng, hoặc cho một cổng. Xem http://synopse.info/forum/viewtopic.php?pid=4652#p4652 –

6

Scripting Windows Firewall có thể, xem Scripting the Windows Firewall

Và mã ví dụ ví dụ here

+0

Tôi sử dụng Delphi 7! –

+0

Delphi 7 hỗ trợ kịch bản dựa trên COM – mjn

+0

Trong trường hợp này, bạn nên thử nhập thư viện kiểu, xem liên kết của tôi, nó đề cập rằng kiểu tệp DLL thư viện 'thường nằm trong" C: \ Windows \ System32 \ hnetcfg.dll " (bài viết này là về XP, tôi đã kiểm tra trong Windows 7 và một tệp có tên này là có) – mjn

Các vấn đề liên quan