以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  如何获取已占用的端口号?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=188555)

--  作者:ygg8310
--  发布时间:2023/9/27 7:37:00
--  如何获取已占用的端口号?
如题:如何获取已占用的端口号?
--  作者:有点蓝
--  发布时间:2023/9/27 9:46:00
--  
全局代码

Public Class TestPost
    
    Const PROCESS_ALL_ACCESS As Int32 = &H1F0FFF
    Const PROCESS_QUERY_INFORMATION As Int32 = &H00000400
    Const PROCESS_VM_READ As Int32 = &H0010
    Const PROCESS_VM_WRITE As Int32 = &H0020

    Public Declare Function QueryFullProcessImageNameW Lib "Kernel32.dll"(ByVal hProcess As Int32, ByVal flags As UInt32, ByVal nameList As Char(), ByRef nameLen As UInt32) As Int32

        Public Declare Function OpenProcess Lib "Kernel32.dll"(ByVal DesiredAccess As Int32, ByVal bInheritHandle As Boolean, ByVal ProcessId As Int32) As Int32

            Public Declare Function CloseHandle Lib "Kernel32.dll"(ByVal hObject As Int32) As Boolean

                Public Declare Function GetLastError Lib "Kernel32.dll"() As Int32
                    
                    
                    Public Function GetInfo() As List(Of String)
                        Dim infoList As New List(Of String)
                        Dim procInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
                        Dim pro As System.Diagnostics.Process = New System.Diagnostics.Process()
                        pro.StartInfo.FileName = "cmd"
                        pro.StartInfo.UseShellExecute = False
                        pro.StartInfo.RedirectStandardInput = True
                        pro.StartInfo.RedirectStandardOutput = True
                        pro.StartInfo.RedirectStandardError = True
                        pro.StartInfo.CreateNoWindow = True
                        pro.Start()
                        pro.StandardInput.WriteLine("netstat -anob")
                        pro.StandardInput.WriteLine("exit")
                        Dim reg As New System.Text.RegularExpressions.Regex("\\s+", RegexOptions.Compiled)
                        Dim line As String = Nothing
                        infoList.Clear()
                        
                        While (CSharpImpl.__Assign(line, pro.StandardOutput.ReadLine())) IsNot Nothing
                            line = line.Trim()
                            
                            If line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                            
                            If line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                        End While
                        
                        pro.Close()
                        
                        Return infoList
                    End Function
                    
                    Private Class CSharpImpl
                        < Obsolete("Please refactor calling code to use normal Visual Basic assignment") > 
                        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
                            target = value
                            Return value
                        End Function
                    End Class
                End Class


命令窗口测试

Dim a As New TestPost

Dim lst As List(Of String) = a.GetInfo()

For Each s As String In lst
    Output.Show(s)
Next