随笔 - 4  文章 - 0 评论 - 17 trackbacks - 1
<2008年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

与我联系

搜索

 

常用链接

留言簿(2)

随笔分类

随笔档案

最新评论

阅读排行榜

评论排行榜

     在编程控制防火墙前先要有个前提,就是你必须是管理员权限, 这样本例的程序才能用"Run as administrator"的方式运行,并成功修改. 如果你本身就是用Administrator这个用户登录的话,直接运行就行了. 建议最好在这个用户下来调试程序.
     本程序只是个初始的例子,里面的功能只开发了一部分,各位有兴趣的话可以继续深入运用. 像Vista的防火墙就比较Bt,除了基本设定外,在"Control Panel\Administrative Tools\Windows Firewall with Advanced Security" 还有高级设定,好像用程序都可控制.
    FireWallManager 程序主要功能有
      1. public void FireWallTrigger( bool enable )  //开关防火墙. 貌似在Vista里面有问题,XP sp2好像可以. 但是用INetFwPolicy2.set_FirewallEnabled的方法的话,Vista也能搞定.
      2.  public void FireWallService( string name, bool enable )  //开关防火墙服务程序,一般里面的 File and Printer Sharing 服务比较有用.
      3.  public bool AddPort( string portName, int portNumber, string protocol )   // 开启一个端口.
      4.  public bool RemovePort( int portNumber, string protocol )          //删除开启的端口
      5.  public bool AddAplication( string discriptionName, string fileName )   //开启放行应用程序
      6.  public bool RemoveApplication( string fileName )         // 关闭放行的应用程序.

里面还有个 protected Object getInstance( String typeName ) 本来是用CLSID来实例化那些接口的,后来发现ProgID其实更简单,不需要查,里面有个规律,只需把接口的INet删掉就是ProgID了. 如 INetFwOpenPort port = ( INetFwOpenPort )Activator.CreateInstance( Type.GetTypeFromProgID( "HNetCfg.FwOpenPort" ) ); 中 INetFwOpenPort 与 FwOpenPort.
     首先,创建一个Console程序,在程序中添加引用,在COM对象中找到"NetFwTypeLib" ,添加即可. 防火墙主要是靠这个对象操作的. 貌似不止Vista, Xp也是一样的。核心程序如下:

FireWallManager.cs