Private Function PortInUse(ByVal PortNumber As Integer) As _ Boolean'*********************************************'PURPOSE: Determine if a TCP/IP port is in use'EXAMPLE: 'If PortInUse(21) Then 'MsgBox "The standard FTP port is in use on this machine" 'end if'**********************************************Dim oSocket As ObjectDim bAns As Boolean On Error Resume NextSet oSocket = CreateObject("MSWinsock.Winsock.1") If Err.Number > 0 Then Err.Raise 30000, , "Could not create winsock object" Exit Function End If Err.Clear oSocket.LocalPort = PortNumberoSocket.Listen 'if we get this error, it means'port is busybAns = Err.Number = 10048oSocket.CloseSet oSocket = NothingPortInUse = bAns End Function

评论