Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I turn the windows firewall on and off? 1

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
AU
Is there a way to turn off and on the windows firewall from within my vb6 code?

I want to briefly turn it off to communicate both ways with a client then turn it back on when the communication is finished.
 
this is for win xp

1.) Click: Start -- Control Panel
2.) Double click: -- Network Connection
3.) you should see your net connection now... right click it
4.) select properties
5.) select advanced tab
6.) click settings
7.) choose off


[bigglasses]
 
There is a Firewall API that can be used to do a number of things, but shutting off the firewall probably isn't one of them.

Usually a program's installer would call this API via a helper DLL to add a firewall exception on installation and remove it during any uninstall. This is done during installation actions because it requires admin privileges. The program itself would make calls each time it runs to determine whether its exception is in place and enabled, which does not require admin rights.

The DirectX SDK contains a commonly used wrapper/helper DLL for this purpose (FirewallInstallHelper.dll). You might find Windows Firewall for Game Developers useful information.


Most people would consider any program that disables the firewall entirely to be malware. The firewall exception mechanism is preferable by far.


You seem to have forgotten about another important issue however. A large number of home and small business users operate behind NAT routers that require configuration to map inbound connection requests inward to a server machine. Larger businesses may also use NAT, but it is rarely something a user can have configured to allow inward access.

I have a wrapper Class that uses the UPnP API to make it easier for programs to map ports temporarily on UPnP-enabled NAT routers. I never put in Firewall API support because I expected some people would abuse it by adding exceptions at runtime rather than during installation.
 
Thanks
three57 - I want to do the equivalent of this from within my code not manually

dilantee - I want to do it to the computer I am on, not to someone else's computer.
I can already do it manually anyway in the normal way so there is no issue of security.
I just want to automate something I can legitimately manually do anyway. and keep the firewall active when I am not communicating

Naturally if the router was not enabled then I wouldn't be able to do it either way.

I didn't want to fiddle with exceptions because I could be temporarily communicating with a large number of other computers over a short period.

At present the user just runs the computer without any firewall which is not good.
 
Add 2 command buttons and paste code:

Code:
Public Sub DisableMyFirewall(MyToggle As Boolean)
     
    Set objFirewall = CreateObject("HNetCfg.FwMgr")
    Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
    
    If MyToggle = True Then
        MsgBox "Your Windows Firewall will now be disabled."
        objPolicy.FirewallEnabled = False
    Else
        MsgBox "Your Windows Firewall will now be enabled."
        objPolicy.FirewallEnabled = True
    End If
   
End Sub

Private Sub Command1_Click()
    DisableMyFirewall True
End Sub

Private Sub Command2_Click()
    DisableMyFirewall False
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disable"
    Command2.Caption = "Enable"
End Sub
 
not sure why the code tags didnt format but this code should do what you want.
 
Code:
Public Sub DisableMyFirewall(MyToggle As Boolean)
     
    Set objFirewall = CreateObject("HNetCfg.FwMgr")
    Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
    
    If MyToggle = True Then
        MsgBox "Your Windows Firewall will now be disabled."
        objPolicy.FirewallEnabled = False
    Else
        MsgBox "Your Windows Firewall will now be enabled."
        objPolicy.FirewallEnabled = True
    End If
   
End Sub

Private Sub Command1_Click()
    DisableMyFirewall True
End Sub

Private Sub Command2_Click()
    DisableMyFirewall False
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disable"
    Command2.Caption = "Enable"
End Sub
 
Thanks that is exactly what I needed.
I notice I can also show the current the firewall state by reading the expression.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top