線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2146
推到 Plurk!
推到 Facebook!

使用程式開啟和關閉Windows xp 防火牆

尚未結案
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-01-27 07:52:16 IP:220.134.xxx.xxx 訂閱
各位前輩:
我於網路上搜尋到關閉Windows防火牆的指令,我將他改成函數庫呼叫,但是我不知道要如何,增加開啟防火牆的部份,還有類似Bitcome軟體,可以自動去設定
Windows 的Firewall port 不知道要如何完整這組功能,先將我搜尋到的功能分享,懇請大家一起來拼圖!

感謝啦!
procedure Set_WindowsXP_FireWall(Enable: boolean);
// 需引用 winsvc, shellapi
// Set_WindowsXP_FireWall(false); // 關閉 Windows Xp 防火牆
//
var
SCM, hService: LongWord;
sStatus: TServiceStatus;
begin
if Enable = false then
begin
SCM := 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;

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-01-27 08:29:40 IP:220.134.xxx.xxx 訂閱
我找到設定Port 的部份,不過還沒測試!不過我還是希望寫成簡單形式的函數庫呼叫方式,大家一起來測試吧!

出處:http://www-new.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22122056.html
Example 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)

const
NET_FW_PROFILE_DOMAIN = 0;
NET_FW_PROFILE_STANDARD = 1;

const
NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_IP_PROTOCOL_UDP = 17;

const
NET_FW_SCOPE_ALL = 0;

const
NET_FW_IP_VERSION_ANY = 2;

implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var ovMgr: OleVariant;
ovProfile: OleVariant;
ovPort: OleVariant;
begin

// Create manager interface
ovMgr:=CreateOleObject('HNetCfg.FwMgr');

// Resource protection
try
// Get local profile interface
ovProfile:=ovMgr.LocalPolicy.CurrentProfile;
// Resource protection
try
// Create new port interface
ovPort:=CreateOleObject('HNetCfg.FwOpenPort');
try
// Set port properties
ovPort.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 protection
try
// Add to globally open ports
ovProfile.GloballyOpenPorts.Add(ovPort);

////
// .... do whatever ....
////

finally
// Remove from globally open ports
ovProfile.GloballyOpenPorts.Remove(81, NET_FW_IP_PROTOCOL_TCP);
end;
finally
// Release interface
ovPort:=Unassigned;
end;
finally
// Release interface
ovProfile:=Unassigned;
end;
finally
// Release interface
ovMgr:=Unassigned;
end;

end;
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
系統時間:2024-05-17 18:46:41
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!