您的位置:首页技术文章
文章详情页

如何透过程序来控制 Windows XP防火墙的开关

【字号: 日期:2023-07-02 15:28:57浏览:25作者:猪猪

Dim;objFW;As;Object;;;Set;objFW;=;CreateObject('HNetCfg.FwMgr').LocalPolicy.CurrentProfileWith;objFW.FirewallEnabled;=;True;';True;开启;,;False;关闭.ExceptionsNotAllowed;=;True;';[;不允许例外;];选项;,;True;勾;,;反之则不勾End;With或procedure;Set_WindowsXP_FireWall(Enable:;boolean);//;需引用;winsvc,;shellapi//;Set_WindowsXP_FireWall(false);;//;关闭Windows;Xp;防火//varSCM,;hService:;LongWord;sStatus:;TServiceStatus;beginif;Enable;=;false;thenbeginSCM;:=;OpenSCManager(nil,;nil,;SC_MANAGER_ALL_ACCESS);hService;:=;OpenService(SCM,;PChar('SharedAccess'),;SERVICE_ALL_ACCESS);ControlService(hService,;SERVICE_CONTROL_STOP,;sStatus);CloseServiceHandle(hService);end;end;设定Port;的部份,不过还没测试!不过我还是希望写成简单形式的函数库呼叫方式,大家一起测试吧!出处:http://www-new.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22122056.htmlExample;of;both;adding;and;removing;a;tcp;port;from;the;globaly;open;ports;list;(in;Windows;XP;firewall)Regards,Russell//;Include;ActiveX;and;ComObj;in;uses;clause;(also;Variants;for;D6;and;up)constNET_FW_PROFILE_DOMAIN;;;;;;=;;0;NET_FW_PROFILE_STANDARD;;;;=;;1;constNET_FW_IP_PROTOCOL_TCP;;;;;=;6;NET_FW_IP_PROTOCOL_UDP;;;;;=;17;constNET_FW_SCOPE_ALL;;;=;;0;constNET_FW_IP_VERSION_ANY;;;;;;=;;2;implementation{$R;*.DFM}procedure;TForm1.Button1Click(Sender:;TObject);var;;ovMgr:;OleVariant;ovProfile:;;;;;OleVariant;ovPort:OleVariant;begin//;Create;manager;interfaceovMgr:=CreateOleObject('HNetCfg.FwMgr');//;Resource;protectiontry//;Get;local;profile;interfaceovProfile:=ovMgr.LocalPolicy.CurrentProfile;//;Resource;protectiontry//;Create;new;port;interfaceovPort:=CreateOleObject('HNetCfg.FwOpenPort');try//;Set;port;propertiesovPort.Port:=81;ovPort.Name:='Whatever';ovPort.Scope:=NET_FW_SCOPE_ALL;ovPort.IpVersion:=NET_FW_IP_VERSION_ANY;ovPort.Protocol:=NET_FW_IP_PROTOCOL_TCP;ovPort.Enabled:=True;//;Resource;protectiontry//;Add;to;globally;open;portsovProfile.GloballyOpenPorts.Add(ovPort);//////;....;do;whatever;....////finally//;Remove;from;globally;open;portsovProfile.GloballyOpenPorts.Remove(81,;NET_FW_IP_PROTOCOL_TCP);end;finally//;Release;interfaceovPort:=Unassigned;end;finally//;Release;interfaceovProfile:=Unassigned;end;finally//;Release;interfaceovMgr:=Unassigned;end;end;

标签: Windows系统