Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do we modify this code to block a port in Windows Firewall?

VB.NET
Imports System
Imports System.Linq
Imports System.Text
Imports NetFwTypeLib

Public Class form1
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim icfMgr As INetFwMgr = Nothing

        Try
            Dim TicfMgr As Type = Type.GetTypeFromProgID("HNetCfg.FwMgr")
            icfMgr = CType(Activator.CreateInstance(TicfMgr), INetFwMgr)
        Catch ex As Exception
            Return
        End Try

        Try
            Dim profile As INetFwProfile
            Dim portClass As INetFwOpenPort
            Dim TportClass As Type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort")
            portClass = CType(Activator.CreateInstance(TportClass), INetFwOpenPort)
            profile = icfMgr.LocalPolicy.CurrentProfile
            portClass.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL
            portClass.Enabled = True
            portClass.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP
            portClass.Name = "111"
            portClass.Port = 8000
            profile.GloballyOpenPorts.Add(portClass)
            Return
            MsgBox("completed" + vbExclamation)
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
End Class


What I have tried:

How do we modify this code to block a port in Windows Firewall?
Posted
Updated 21-Jul-19 0:23am
v2

1 solution

See example in C# here: how to Block and Allow any specific port using c#[^]

Note that in the example the string format is not correctly ended with a ):
String.Format("advfirewall firewall add rule name=\"{0}\" dir=in action=block protocol={1} localport={2} profile={3}",
    "My rule name",
    "TCP",
    4567, // Port
    "Private")
 
Share this answer
 
v2
Comments
Member 14525261 20-Jul-19 8:07am    
The example is not clear where write port numbers
Afzaal Ahmad Zeeshan 20-Jul-19 9:06am    
There is a clear comment indicating the port, please read the code again, slowly.
Dave Kreskowiak 20-Jul-19 11:21am    
Are you serious? Did you not see the comment that says "Port"?
Member 14525261 21-Jul-19 5:13am    
You have converted the code to vb.net
Imports System.Runtime.InteropServices

Public Class Form1


Private Function RunShellCommand(ByVal command As String, ByVal parms As String, <out> ByRef stdout As String, <out> ByRef stderr As String, Optional ByVal waitForCompletion As Boolean = True) As Integer
Dim psi As ProcessStartInfo = New ProcessStartInfo(command)
psi.RedirectStandardInput = True
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.UseShellExecute = False
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.CreateNoWindow = True
Dim proc As Process = Process.Start(psi)
Dim sw As System.IO.StreamWriter = proc.StandardInput
Dim sr As System.IO.StreamReader = proc.StandardOutput
Dim se As System.IO.StreamReader = proc.StandardError
sw.WriteLine(parms)
sw.Close()
stdout = sr.ReadToEnd()
stderr = se.ReadToEnd()
If waitForCompletion Then proc.WaitForExit()
Return proc.ExitCode

End Function

How to do the following code conversion to vb.net

RunShellCommand(
"netsh.exe",
String.Format("advfirewall firewall add rule name=\"{0}\" dir=in action=block protocol={1} localport={2} profile={3}",
"My rule name",
"TCP",
4567, // Port
"Private", // Can be Private, Domain, Public or Any
out stdout,
out stderr);


End Class
RickZeeland 21-Jul-19 13:22pm    
Dim result As Integer = RunShellCommand("netsh.exe", ... stdout, stderr)
there is no equivalent of the out keyword in VB, you don't need to use it in the call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900