Mega Code Archive

 
Categories / Delphi / Examples
 

Interface firewall xp sp2

All the objects lie in %windir%\system32\hnetcfg.dll You'll need to import the type library for that in order to have a .pas file you can work with. With Delphi 6 I had to use the command-line importer tlibimp.exe in the D6\Bin dir, because the interfaces you want are in the 3rd "section" (don't know the proper term) of the DLL file, and the GUI importer wouldn't let me add the \3 after the DLL name in the file browser without choking. After running that, you should have a NetFwTypeLib_TLB.pas file (and a .dcr as well, but I haven't needed that). Check MSDN (msdn.microsoft.com) for the INetFwMgr interface for more info, but the following code will add a port. Note the account in question must be an Administrator, or the calls will fail. I've tested this, and not even Power Users can open/modify ports (a good thing). const FW_MGR_CLASS_NAME = 'HNetCfg.FwMgr'; FW_OPENPORT_CLASS_NAME = 'HNetCfg.FwOpenPort'; function XPFirewallAddPort(APort: Integer; AName: String; AProtocol: NET_FW_IP_PROTOCOL_ = NET_FW_IP_PROTOCOL_TCP; AScope: NET_FW_SCOPE_ = NET_FW_SCOPE_ALL; AEnabled: Boolean = True; AIPVersion: NET_FW_IP_VERSION_ = NET_FW_IP_VERSION_ANY): HRESULT; var FwMgrDisp: IDispatch; FwMgr: INetFwMgr; FwProfile: INetFwProfile; FwOpenPortDisp: IDispatch; FwOpenPort: INetFwOpenPort; begin Result := S_OK; try FwMgrDisp := CreateOleObject(FW_MGR_CLASS_NAME); try FwMgr := INetFwMgr(FwMgrDisp); FwProfile := FwMgr.LocalPolicy.CurrentProfile; FwOpenPortDisp := CreateOleObject(FW_OPENPORT_CLASS_NAME); try FwOpenPort := INetFwOpenPort(FwOpenPortDisp); with FwOpenPort do begin Port := APort; Name := AName; Protocol := AProtocol; Scope := AScope; Enabled := AEnabled; IpVersion := AIPVersion; end; FwProfile.GloballyOpenPorts.Add(FwOpenPort); finally FwOpenPortDisp := Unassigned; end; finally FwMgrDisp := Unassigned; end; except on E:EOleSysError do begin Result := E.ErrorCode; end; end; end;